Link to home
Start Free TrialLog in
Avatar of G-Force
G-Force

asked on

Retrieving values from 2 db tables and selecting the checkboxes.

Hi,
I currently have a problem and I've been working on it the whole day but I'm still getting no where.
The situation is...

I have 2 db tables: Tyres and Customers

Tyres
---------------------------
|     Brand     | tyreId |
---------------------------
| Yokohama   |   10     |
| GoodYear    |   11     |
| Michelin      |   12     |


Customers
---------------------------
| Name | custId | tyreId |
---------------------------
| John     |    J1     |  10     |
| John     |    J1     |  11     |
| Tom     |    T2     |  10     |




I'm displaying the Tyres table using checkboxes on a page.
Then, for a customer, say John, he can have more than 1 tyre order.
How do I select 2 checkboxes out of the 3?
I've tried using nested while loop but it seems that it only loop through once and selects one checkbox and that's it.
This was what I did:


<?  
$query2 = "SELECT * FROM Customer where custID='J1'";
$result2 = mysql_query($query2) or die( mysql_error() );
while ($r = mysql_fetch_array($result2)){
          $tyreID[ ] = $r["tyreID"];
}

echo "<tr>";
     echo "<td>";
        echo "Tyres:";
     echo "</td>";
     echo "<td>";
     $query = "SELECT * FROM Tyres";
     $result = mysql_query($query) or die( mysql_error() );
     while ($r = mysql_fetch_array($result)){
          $Brand = $r["Brand"];
          $tyreId = $r["tyreId"];

          if (is_array($tyreID)){
               while(list($key, $ID) = each($tyreID)){

                    echo "<tr>";
                         echo "<td>";
                         echo "</td>";
                         echo "<td>";
                              if ($tyreId == $ID){
                                   echo "<input type=checkbox name='custOrder' Value=$typeId selected> $Brand";
                              }
                              else{
                                   echo "<input type=checkbox name='custOrder' Value=$typeId> $Brand";
                              }
                         echo "</td>";
                    echo "</tr>";
               }
          }
     }
     echo "</td>";
echo "</tr>";
?>




I'm not sure if this logic is the right one to use.
If you have better suggestions, feel free to comment.
I need help as soon as possible. Thanks!
Avatar of beauty_fool
beauty_fool

try replacing the nested while with this:

/* code */
foreach($tireID as $ID){
/* end code */

just this one line, nothing else
ASKER CERTIFIED SOLUTION
Avatar of jausions
jausions
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of G-Force

ASKER

Oh, thanks to you both, but I've found a way to solve this problem here.
jausions, I'll give you the 30 points 'cause I see that you do put in effort.
Anyway, thanks!