【问题标题】:Array and for each loop issues数组和每个循环问题
【发布时间】:2013-04-09 11:01:06
【问题描述】:

如果我在数组中运行一个值,则代码在下面如果我运行多个值,结果是正确的,结果是价格不正确,就像它在某处弄乱了值一样?帮助表示赞赏

$dido=array('42204131','22204131'); foreach($dido 作为 $did): $query = "select * from dispatch,link where lid=dlid and did=$did"; $result = mysql_query($query) 或 die(mysql_error()); 而($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']; // 价格 anc1 $querypanc1 = "从 pid=$anc1 的产品中选择 pprice"; $resultpanc1 = mysql_query($querypanc1); 而($row = mysql_fetch_array($resultpanc1)) { $priceanc1=$row[pprice]; $tpriceanc1=$vanc1*$priceanc1; } // 价格 anc2 $querypanc2 = "从 pid=$anc2 的产品中选择 pprice"; $resultpanc2 = mysql_query($querypanc2); 而($row = mysql_fetch_array($resultpanc2)) { $priceanc2=$row[pprice]; $tpriceanc2=$vanc2*$priceanc2; } // 价格 anc3 $querypanc3 = "从 pid=$anc3 的产品中选择 pprice"; $resultpanc3 = mysql_query($querypanc3); 而($row = mysql_fetch_array($resultpanc3)) { $priceanc3=$row[pprice]; $tpriceanc3=$vanc3*$priceanc3; } // 价格 anc4 $querypanc4 = "从 pid=$anc4 的产品中选择 pprice"; $resultpanc4 = mysql_query($querypanc4); 而($row = mysql_fetch_array($resultpanc4)) { $priceanc4=$row[pprice]; $tpriceanc4=$vanc4*$priceanc4; } // 价格 anc5 $querypanc5 = "从 pid=$anc5 的产品中选择 pprice"; $resultpanc5 = mysql_query($querypanc5); 而($row = mysql_fetch_array($resultpanc5)) { $priceanc5=$row[pprice]; $tpriceanc5=$vanc5*$priceanc5; } $gtprice=$tpriceanc1+$tpriceanc2+$tpriceanc3+$tpriceanc4+$tpriceanc5; $qrygt="更新调度 SET gtprice=$gtprice WHERE did=$did"; $resultgt=@mysql_query($qrygt); } 结束;

【问题讨论】:

    标签: php


    【解决方案1】:

    您的第一个也是最大的问题是代码的复制粘贴性质。让我们试着分解一下你在做什么:

    • 设置 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
      }
    }
    

    该子方法应该做什么?让我们看看你当前的代码 sn-p:

     // 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);
    

    我会让你自己写那部分。但要记住;分解你正在做的事情,并让重复的元素由同一个函数处理。这里要学到的真正教训是编程实际上是在交流:你的代码没有交流任何东西,因为有太多重复的噪音。使用好的变量名。将您正在做的事情收紧到具有单个任务的功能。这样,任何阅读您的代码的人(包括您!)都会知道您要做什么以及哪里出错了。

    【讨论】:

      【解决方案2】:

      1) 我可以在您的代码中发现的唯一可能的问题是,当您的某些 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;
          }
      }
      

      【讨论】:

      • 非常感谢。那是 pid 返回空数据的时候。尚未创建子循环。
      • 不客气。请注意,我的答案中的循环(以及@Nathaniel Ford 的解决方案)既解决了您的问题,又使代码变得更好。另外,请不要忘记接受和/或支持对您有帮助的答案:)
      猜你喜欢
      • 2015-09-12
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 2020-03-09
      • 2011-07-15
      相关资源
      最近更新 更多