genzedu777 Posted February 14, 2011 Share Posted February 14, 2011 Hi all, Just to check if it is possible to have 2 while loops in a single code? Below are my codes. SQL code $tutor_id = mysqli_real_escape_string($dbc, $_GET['tutor_id']); $query = "SELECT tl.level_id, tl.level_name, ts.subject_id, ts.subject_name, tsl.subject_level_id, IF(tosl.tutor_id='{$tutor_id}', 1, 0) as checked FROM tutor_level AS tl INNER JOIN tutor_subject_level AS tsl USING (level_id) INNER JOIN tutor_subject AS ts USING (subject_id) LEFT JOIN tutor_overall_level_subject AS tosl ON tosl.subject_level_id = tsl.subject_level_id AND tosl.tutor_id = '{$tutor_id}' ORDER BY tl.level_id, ts.subject_name"; $sql = mysqli_query($dbc, $query) or die(mysqli_error($dbc)); $query1 = "SELECT tl.level_id, tl.level_name, IF(tslvl.tutor_id='{$tutor_id}', 1, 0) as checked FROM tutor_level AS tl LEFT JOIN tutor_selected_level AS tslvl ON tslvl.level_id = tl.level_id AND tslvl.tutor_id='{$tutor_id}' ORDER BY tl.level_id, tl.level_name"; $sql1 = mysqli_query($dbc, $query1) or die(mysqli_error($dbc)); PHP code (2 while loops) //Process the results $checkboxes = ''; //Create variable for output $current_level_id = false; //Flag to check when records change level while($data = mysqli_fetch_array($sql)) //Iterate throug the DB results { if($current_level_id != $data1['level_id']) //Determine if this level is different from the last { print_r ($data); $checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); $current_level_id = $data['level_id']; $subject_data = array(); } //Add the current record to the $level_data array $subject_data[] = $data; } //$checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); while($data1 = mysqli_fetch_array($sql1)) { print_r ($data1); $checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); $level_data = array(); $level_data[] = $data1; } //Call the createLevelCheckboxes() function to generate the HTML for the LAST level records $checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); Quote Link to comment https://forums.phpfreaks.com/topic/227601-can-there-be-2-while-loops-in-a-single-code/ Share on other sites More sharing options...
Psycho Posted February 14, 2011 Share Posted February 14, 2011 Yes, you can have 2 while loops in the same code - BUT you cannot do a 2nd while loop to fetch the results of a db query if the results have been exhausted by the first while loop unless you reset the pointer to the beginning of the db results. But, there should never be a need to run through the results of a db query twice. Do everything you need to do with the results in the first loop. Quote Link to comment https://forums.phpfreaks.com/topic/227601-can-there-be-2-while-loops-in-a-single-code/#findComment-1173931 Share on other sites More sharing options...
genzedu777 Posted February 15, 2011 Author Share Posted February 15, 2011 Hi mjdamato, So sorry to trouble you again, didn't expect you to reply to this thread, as I didn't want to tie you down by all my questions. I don't quite get you. But, there should never be a need to run through the results of a db query twice. Do everything you need to do with the results in the first loop. How can I achieve it? Since I have 2 different queries ($query & $query1) while($data = mysqli_fetch_array($sql)) //Iterate throug the DB results { if($current_level_id != $data['level_id']) //Determine if this level is different from the last { print_r ($data); $checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); $current_level_id = $data['level_id']; $subject_data = array(); } //Add the current record to the $level_data array $subject_data[] = $data; } //$checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); while($data1 = mysqli_fetch_array($sql1)) { print_r ($data1); $checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); $level_data = array(); $level_data[] = $data1; } Quote Link to comment https://forums.phpfreaks.com/topic/227601-can-there-be-2-while-loops-in-a-single-code/#findComment-1174325 Share on other sites More sharing options...
Psycho Posted February 15, 2011 Share Posted February 15, 2011 At first glance I thought you were trying to process the results of the same query twice. But, the answer is still the same - you can have two while loops on a page. Quote Link to comment https://forums.phpfreaks.com/topic/227601-can-there-be-2-while-loops-in-a-single-code/#findComment-1174563 Share on other sites More sharing options...
genzedu777 Posted February 18, 2011 Author Share Posted February 18, 2011 Hi mjdamato, Is it possible to help me out with these 2 while loops? I am still trying to figure out what had happened. I realised what is left in $subject_data = array(); is only the last value. For example, in subject_data array, there should be 3 values...Eng, Math and Sci However at the end day, subject_data array only keeps 'Sci', the other 2 subjects are missing - 'Eng', 'Math'. It seems like somehow it has been overwritten. I have done some troubleshooting, and realised it is only when I execute the 2nd while loop, while ($data1 = mysqli_fetch_array($sql1)), it will overwrite the values in subject_data array Do I need to store the results/values in subject_data array somewhere in another variable, before executing the 2nd while loop? while($data = mysqli_fetch_array($sql)) //Iterate throug the DB results { if($current_level_id != $data['level_id']) //Determine if this level is different from the last { $levelCheckboxes .= createLevelCheckboxes($subject_data, $level_data 5); $current_level_id = $data['level_id']; $subject_data = array(); } //Add the current record to the $level_data array $subject_data[] = $data; } //$checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); while($data1 = mysqli_fetch_array($sql1)) { $levelCheckboxes .= createLevelCheckboxes($subject_data, $level_data, 5); $level_data = array(); $level_data[] = $data1; } //Call the createLevelCheckboxes() function to generate the HTML for the LAST level records $levelCheckboxes .= createLevelCheckboxes($subject_data, $level_data, 5); Quote Link to comment https://forums.phpfreaks.com/topic/227601-can-there-be-2-while-loops-in-a-single-code/#findComment-1175986 Share on other sites More sharing options...
Psycho Posted February 18, 2011 Share Posted February 18, 2011 That code makes no sense. I've alredy provided code in a separate thread. This is the wrong approach to what you are trying to accomplish. Quote Link to comment https://forums.phpfreaks.com/topic/227601-can-there-be-2-while-loops-in-a-single-code/#findComment-1176361 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.