簡體   English   中英

數組和每個循環問題

[英]Array and for each loop issues

代碼如下,如果我在數組中運行一個值,結果是正確的,如果我運行多個值,結果是價格是不正確的,就像它已經弄亂了某些值? 幫助贊賞

$dido=array('42204131','22204131'); 
    foreach($dido as $did):

    $query = "select * from dispatch,link where lid=dlid and did=$did";
    $result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){

    $vanc1=$row['vanc1'];
    $vanc2=$row['vanc2'];
    $vanc3=$row['vanc3'];
    $vanc4=$row['vanc4'];
    $vanc5=$row['vanc5'];

    $anc1=$row['anc1'];
    $anc2=$row['anc2'];
    $anc3=$row['anc3'];
    $anc4=$row['anc4'];
    $anc5=$row['anc5'];

    // price anc1
    $querypanc1 = "select pprice from products where pid=$anc1";
    $resultpanc1 = mysql_query($querypanc1);
    while($row = mysql_fetch_array($resultpanc1))
    {
        $priceanc1=$row[pprice];
        $tpriceanc1=$vanc1*$priceanc1;

    }
    // price anc2
    $querypanc2 = "select pprice from products where pid=$anc2";
    $resultpanc2 = mysql_query($querypanc2);
    while($row = mysql_fetch_array($resultpanc2))
    {
        $priceanc2=$row[pprice];
        $tpriceanc2=$vanc2*$priceanc2;

    }
    // price anc3
    $querypanc3 = "select pprice from products where pid=$anc3";
    $resultpanc3 = mysql_query($querypanc3);
    while($row = mysql_fetch_array($resultpanc3))
    {
        $priceanc3=$row[pprice];
        $tpriceanc3=$vanc3*$priceanc3;

    }
    // price anc4
    $querypanc4 = "select pprice from products where pid=$anc4";
    $resultpanc4 = mysql_query($querypanc4);
    while($row = mysql_fetch_array($resultpanc4))
    {
        $priceanc4=$row[pprice];
        $tpriceanc4=$vanc4*$priceanc4;

    }
    // price anc5
    $querypanc5 = "select pprice from products where pid=$anc5";
    $resultpanc5 = mysql_query($querypanc5);
    while($row = mysql_fetch_array($resultpanc5))
    {
        $priceanc5=$row[pprice];
        $tpriceanc5=$vanc5*$priceanc5;

    }


    $gtprice=$tpriceanc1+$tpriceanc2+$tpriceanc3+$tpriceanc4+$tpriceanc5;

        $qrygt="UPDATE dispatch SET gtprice=$gtprice WHERE did=$did";
        $resultgt=@mysql_query($qrygt);

        }
        endforeach;

您的第一個也是最大的問題是代碼的復制面食性質。 讓我們試着打破你正在做的事情:

  • 設置ID列表
  • 對這些ID運行查詢
  • 將結果放入數組中
  • 對每個結果運行單獨的查詢

您還使用了一些非常笨拙的語法。 (即foreach($foo as $bar):

把這些東西分解成方法。 什么是方法? 它需要輸入並將其轉換為輸出。

//returns an array of price information
public function getPrices($idArray) { //note the good method and parameter names!
  //do stuff
}

現在我們知道我們在做什么,我們可以開始填寫實現細節:

public function getPrices($idArray) {
  foreach($idArray as $id) {
    //somehow get the gross-scale information
    //then put it in a data object
    //then call a function to get specific information
  }
}

該子方法應該做什么? 讓我們看看你當前的代碼片段:

 // price anc1
 $querypanc1 = "select pprice from products where pid=$anc1";//sets up sql query
 $resultpanc1 = mysql_query($querypanc1);                    //runs the query
 while($row = mysql_fetch_array($resultpanc1)) {             //for each result
   $priceanc1=$row[pprice];                                  //gets the price
   $tpriceanc1=$vanc1*$priceanc1;                            //calculates some other price
 }

最后兩行真的暗示了一個對象,但也許這對你來說太重了。 前兩行是鍋爐板,你無休止地重復。 讓我們寫一個函數!

public function getPrices($name, $pid, $multiplier) {
  $sqlQuery = "SELECT pprice FROM products WHERE pid=$pid";
  $result = mysql_query($sqlQuery);
  $prices = array();
  while ($row = mysql_fetch_array($result) {
    $key = "price".$name;//$key will be something like 'priceanc1'
    $prices[$key] = $row[pprice];
    $tkey = "tprice".$name;
    $prices[$tkey] = $prices[$key] * $multiplier;
  }
}

現在,這個函數有點不干凈,因為它試圖一次做兩件事(查詢數據庫,然后將數據按摩到一個可用的數組),但我希望它類似於你正在做的事情。 有了這個函數,我們可以回到我們的更高級函數調用它:

public function getPrices($idArray) {
  foreach($idArray as $id) {
    $sqlQuery = "SELECT * from dispatch, link WHERE lid=dlid and did=$id";
    $prices = array();
    while ($row = mysql_fetch_array($result) {
      for ($idx = 1; $idx <= 5; $idx++) {
        $name = "anc".$idx;
        $pid = $row[$name];
        $multiplier = $row["vanc".$idx];
        $priceArray = getPrices($name, $pid, $multiplier);
        $prices = array_merge($prices, $priceArray);
      }
    }
  }

  //put a var_dump here to check to see if you're getting good results!

  return $prices;//Should be the aggregated prices you've gotten from the db
}

現在,這就是你想要做的,但我承認我不明白你的數據庫是如何設置的,或者你的變量實際意味着什么。 按下! 我們還注意到,不必要的數據按摩就會消失。

您可以這樣調用:

$ids = array();
$ids[] = 42204131;
$ids[] = 22204131;
$prices = getPrices($ids);
var_dump($prices);//shows the result of your work

既然有了價格,您可以將它們傳遞給另一個函數來運行更新:

updatePrices($prices);

我會讓你自己寫那部分。 但要記住; 打破你正在做的事情,並通過相同的功能處理重復的元素。 這里要學到的真正教訓是編程實際上是在溝通:你的代碼沒有任何通信,因為有太多重復的噪音。 使用好的變量名稱。 通過單個任務加強您正在進行的功能。 這樣,任何人閱讀你的代碼(包括你!)都會知道你要做什么以及你哪里出錯了。

1)我可以在您的代碼中發現的唯一可能的問題是,當您select pprice from products where pid ...查詢的select pprice from products where pid ...某些select pprice from products where pid ...不返回任何數據時,您保留了之前迭代的$tpriceancX值。

2)另外(超出主題)你可以用for循環替換5塊重復代碼。

$gtprice = 0;
for ($i = 1; $i <= 5; $i++)
{
    $querypanc = "select pprice from products where pid=".$row["anc$i"];
    $resultpanc = mysql_query($querypanc);
    while($pancrow = mysql_fetch_array($resultpanc))
    {
        $priceanc=$pancrow[pprice];
        $tpriceanc=$row["vanc$i"]*$priceanc;
        $gtprice += $tpriceanc;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM