Newbie having problems with nested while loops...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rptut
Forum Newbie
Posts: 4
Joined: Tue Aug 07, 2007 9:47 pm

Newbie having problems with nested while loops...

Post by rptut »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Here is the simple version of what I'm trying to do:
Creating a preference center for "favorite cars"...

table1 - id, carname (30 rows)
table 2 - uid, carid (2 rows) - it's just one user right now that I'm testing against and he only "likes" 2 of the 30 possible cars

form with check boxes displays all 30 cars and allows user to check which are their favorites.

Form page is built with a while loop to create all the check boxes

Code: Select all

while ($row = mysql_fetch_array($table1)) {

create all the form fields

}
So, when someone comes back to the form I want to show which boxes should be pre-checked based on the data in table2. I tried a nested loop and only get it to return the first row. I thought I saw something somewhere about resetting the variables but I'm not sure what to do and if there is a better way to go about doing this. I have something like this right now...

Code: Select all

while ($row = mysql_fetch_array($table1)) {

    while ($row2 = mysql_fetch_array($table2)) {
      
       if ($row[id] = $row2[carid]) {
        create all the form fields with checks
       } else {
       create all the other form fields
       }
    }

}
Any suggestions on what I should do, or what I am doing wrong?

Thanks.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

You actually don't need two selects and two loops here, just use joins

Code: Select all

SELECT table_cars.id, table_cars.car_name  FROM table_cars
   INNER JOIN table_favorites ON table_favorites.car_id = table_cars.id
   WHERE table_favorites.user_id = $USER
This will select "favorite cars" along with their names.
rptut
Forum Newbie
Posts: 4
Joined: Tue Aug 07, 2007 9:47 pm

Post by rptut »

Thanks, I think that makes sense... but I thought I tried something like that and only got two rows back. How can I display the other 28 cars that aren't "favorites" in the preference form? I want to give the returning user the ability to select additional cars in the future.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Using a join you will only get back the cars that the user likes, not all the cars.

Select the list of cars like you are doing. Then, as you loop them, you need to check to see if the user has that car is as a favorite, the check the box. Basically you need to check your users car id list against the current id of the main car list loop.

Perhaps array_key_exists() or in_array() might be useful in a situation like this.
rptut
Forum Newbie
Posts: 4
Joined: Tue Aug 07, 2007 9:47 pm

Post by rptut »

Thanks for the input, I think this is helping me figure it out and getting a lot closer. I still think I will run into the problem of having to run through the data in table1 then cross referencing it through all the data in table2... meaning I still have to do two loops... with one inside the other. Won't I run into the same issue even if I use a different array function?

I may be totally confusing the issue at this point in time so I hope I'm not asking a stupid question.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
// This simulates a data return from a query for your items to list
$items = array(
  array(
    'item_id' => 1,
    'item_name' => 'Candy'
  ),
  array(
    'item_id' => 2,
    'item_name' => 'Pizza'
  ),
  array(
    'item_id' => 3,
    'item_name' => 'Garbanzo Beans'
  ),
  array(
    'item_id' => 4,
    'item_name' => 'Humus'
  )
);

// This simulates a return data set for a users selected items
$chosen = array(
  array(
    'item_id' => 1
  ),
  array(
    'item_id' => 2
  ),
);

// Set counts for each data set to limit looping
$item_count = count($items);
$chosen_count = count($chosen);

// Now we are going to make an array of IDs the user has chosen
$selected = array();
for ($i = 0; $i < $chosen_count; $i++) {
  $selected[] = $chosen[$i]['item_id'];
}

Begin looping
for ($i = 0; $i < $item_count; $i++) {
  // Echo out the item name
  echo '<p>' . $items[$i]['item_name'];
  // Check if it is in the selected array
  if (in_array($items[$i]['item_id']), $selected) {
    echo ': CHOSEN BY THE USER';
  }
  echo '</p>';
}
?>
rptut
Forum Newbie
Posts: 4
Joined: Tue Aug 07, 2007 9:47 pm

Post by rptut »

works great!

Thanks!
Post Reply