confusion about WHILE within a WHILE

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
indepthmedia
Forum Newbie
Posts: 10
Joined: Tue Oct 31, 2006 10:39 pm

confusion about WHILE within a WHILE

Post by indepthmedia »

I'm going crazy! Can someone please point out how to make this code work properly... I am simply trying to display a list of all items from 1 table with checkboxes next to each item,and then pre-check some of those boxes based on values from a lookup table.

Code: Select all

<?php //Display all cases via checkboxes and pre-check the current values

while ($selectedcase = mysql_fetch_array($selectedcases)) {
	$caseid = $selectedcase['caseid'];

	while ($case = mysql_fetch_array($cases)) {
		$case_prodid = $case['prodid'];
		$prodname = htmlspecialchars($case['prodname']);
		
		if ($case_prodid == $caseid) { ?>
			<input name="case_prodids[]" type="checkbox" value="<?php echo $case_prodid; ?>" checked> <?php echo $prodname; ?><br /><?php
		}else{ ?>
			<input name="case_prodids[]" type="checkbox" value="<?php echo $case_prodid; ?>"> <?php echo $prodname; ?><br /><?php
		}
	}
}
Currently, this is only pre-checking one box correctly (the last value in the table) even when there should be multiple boxes checked so there must be something wrong with my first WHILE or maybe just where it is???

Thank You.

Jcart | Please show of the curtosy of properly indenting your code next time, thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

while ($selectedcase = mysql_fetch_array($selectedcases)) {
        $caseid = $selectedcase['caseid'];

        while ($case = mysql_fetch_array($cases)) {
                $case_prodid = $case['prodid'];
                $prodname = htmlspecialchars($case['prodname']);
               
                //debug 
                echo '<pre>';
                var_dump($case_prodid == $caseid);
                var_dump($case_prodid); 
                var_dump($caseid); 
                echo '</pre>';

                if ($case_prodid == $caseid) { ?>
                        <input name="case_prodids[]" type="checkbox" value="<?php echo $case_prodid; ?>" checked> <?php echo $prodname; ?><br /><?php
                }else{ ?>
                        <input name="case_prodids[]" type="checkbox" value="<?php echo $case_prodid; ?>"> <?php echo $prodname; ?><br /><?php
                }
        }
}
Debugging 101, lets find out what our values are actually holding. What does this return?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The inner while will only execute through entirely once. You will need to call mysql_data_seek() to reset the record set back to the first record, or run that loop before the $selectedcase loop storing up the information necessary to output your desired display.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Ah true, I forgot about the internal pointer. Good call.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I would probably fetch the cases only once (and buffer them in an array)... And then foreach selectedcase use that array to build the list...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I agree with timvw. Grab the information once, loop the result arrays.
Post Reply