Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

<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; [] => [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

share|improve this question
Oh my talk about spaghetti code! – Daniel Williams Mar 1 at 20:30
whats ?? sorry my english please – Alejandro Beltran Mar 1 at 20:32
1  
Your english is a little too confusing to understand exactly what you are trying to ask. – Applehat Mar 1 at 20:37
i want SELECT from that array with that output (i cannot show QUANT) value of array associate with value of SELECT register, when this register is a query from BD. In the example [1254 is code of product] => 325 is quantity – Alejandro Beltran Mar 1 at 20:40
"quant" field there arenot in BD, this field i want ADD to each register, values of "quant are value in ARRAY $rr" [1254 is code of product] => 325 is quantity i want ASSOCIATE – Alejandro Beltran Mar 1 at 20:45

2 Answers

up vote 0 down vote accepted

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.

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>
share|improve this answer
THANKS @APPLEHAT THIS LINE IS EXACTLY THE SOLUTION foreach ($rr as $name => &$arr) – Alejandro Beltran Mar 1 at 21:10
No problem! Glad I could help. – Applehat Mar 1 at 21:13

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.

First try to make flow chart, then code.

Suggest you to go through data structure books and try to code and solve problems there.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.