Page 1 of 1

confusion about WHILE within a WHILE

Posted: Sat Nov 11, 2006 11:31 pm
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

Posted: Sat Nov 11, 2006 11:42 pm
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?

Posted: Sat Nov 11, 2006 11:45 pm
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.

Posted: Sat Nov 11, 2006 11:49 pm
by John Cartwright
Ah true, I forgot about the internal pointer. Good call.

Posted: Sun Nov 12, 2006 3:37 am
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...

Posted: Mon Nov 13, 2006 1:10 am
by RobertGonzalez
I agree with timvw. Grab the information once, loop the result arrays.