简体   繁体   English

PHP MySQL选择和回声数组多维

[英]Php mysql select and echo array multidimensional

i have a prblem of 5 hours aprox... I can Print an mysql select to a table from one array before i combine... my code is: in Cookies there are this array Array( [] => [1254] => 325 [2112] => 77 [354] => 2 ) where [1254 is cod of product] => 325 is quant 我有大约5个小时的问题...我可以在合并之前将mysql select从一个数组打印到一个表中...我的代码是:Cookies中有此数组Array([] => [1254] => 325 [2112] => 77 [354] => 2),其中[1254是产品的鳕鱼] => 325是定量

<table width="100%" border="0" cellpadding="3" cellspacing="2">
            <tr><!--prepare row of head cols html static--> 
              <td> COD</td>
              <td> PROD</td>
              <td>CAT</td>
              <td>**CANT**</td>
          </tr>
     <?php  
          ////array in cookie to variable php $rr
          $rr = $_COOKIE['coo'];
          foreach ($rr as &$arr){////for each element of $rr generate one register
            $SQL="SELECT cod_pro,name,cod_cat,('$arr') AS quant FROM products 
               WHERE cod_pro=".$arr." 
               ORDER BY cod_cat asc";
          $result=mysql_query($SQL,$lnk) ;

if (mysql_num_rows($result)>0){  //if exist anyone 

while($registers=mysql_fetch_array($result,MYSQL_ASSOC)){
    ////print each row here all right
 echo "<tr><td>".$registers["cod_pro"]."</td>"."<td>".$registers["name"]."/td>"
 ."<td>".$registers["cod_cat"]."/td><td>".$registers["quant"]."</td></tr>";
       ////$registers["quant"] i want add one col with this value from cookie
  ?>
    <?php }?>
    <?php }     
    } ?>
  </table>

The problem is $registers["cant"] how i can do the SELECT my sql, for give this output; 问题是$ registers [“ cant”]我如何执行SELECT我的sql,以给出此输出; [] => [1254] => 325 [2112] => 77 [354] => 2 [] => [1254] => 325 [2112] => 77 [354] => 2

      COD (pk)   NAME         COD_CAT       **quant** This i can´t show
      --------------------------------------------
      1254      Car (bd)         1 (bd)      **325**
      2112      Cicles(bd)       2 (bd)       **77** 
      354       toys (in bd)     3 (bd)        **2**

Any idea Thanks 任何想法谢谢

after doing the select, get the results and then do the for each loop with the results 选择之后,获取结果,然后对结果进行每个循环

Now you are going the reverse way 现在,您将以相反的方式

Also you are having a single tr against multiple tr in your loop. 同样,您在循环中只有一个tr而不是多个tr。

First try to make flow chart, then code. 首先尝试制作流程图,然后编写代码。

Suggest you to go through data structure books and try to code and solve problems there. 建议您阅读数据结构书籍,并尝试在那里编码和解决问题。

I think I understand what you are asking. 我明白您的要求。 I cleaned up your code, and made the fix I think your wanting. 我清理你的代码,并提出了修正,我认为你的希望。 There is no reason to try and pass data into that SQL query, just to pull it back out. 没有理由尝试将数据传递到该SQL查询中,只是将其撤回。

Just reference the data directly in the loop. 只需直接在循环中引用数据即可。

I dont have any idea what this code actually does, so what I wrote below may not work... 我不知道这段代码实际上是做什么的,所以我在下面写的内容可能不起作用...

But its the best I can do from what I understand. 但是据我所知,这是我能做的最好的事情。

<table width="100%" border="0" cellpadding="3" cellspacing="2">
    <tr>
       <td> COD</td>
       <td> PROD</td>
       <td>CAT</td>
       <td>**CANT**</td>
    </tr>
 <?php  

    $rr = $_COOKIE['coo'];
    foreach ($rr as $name => &$arr)
    {
        $SQL = "SELECT cod_pro,name,cod_cat FROM products 
                WHERE cod_pro=".$arr." 
                ORDER BY cod_cat asc";

        $result=mysql_query($SQL,$lnk) ;

        if (mysql_num_rows($result)>0)
        {  

           while($registers=mysql_fetch_array($result,MYSQL_ASSOC))
           {

                echo "<tr><td>".$registers["cod_pro"]."</td>"."<td>".$registers["name"]."/td>"."<td>".$registers["cod_cat"]."/td><td>".$name."</td></tr>";

           } 
         }      
    } 
 ?>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM