genzedu777 Posted February 17, 2011 Share Posted February 17, 2011 Hi all, I am desperately in need of some help here. I have been troubleshooting these codes for days, and I have posted this similar question quite a couple of times, and there is no answer from anyone... I am really in a bad shape now. Could someone please enlighten me to my problem? I have created 2 while loops...apparently the 2nd while loop overwrites the 1st while loop record. Meaning to say...1st while loop generates values of 'Math' 'Eng' 'Chi' 'Sci' AND store it in $subject_data, createLevelCheckboxes($subject_data, $level_data, 5); Apparently, the 2nd while loop takes only the last record 'Sci' from $subject_data and continues the looping. Which gives me results like Level 1 Sci Level2 Sci Level3 Sci I would like to attain something like.... Level 1 Math Level2 Eng Level3 Chi Level4 Sci Is there any reset which I need to do? while($data = mysqli_fetch_array($sql)) { if($current_level_id != $data['level_id']) { $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; } while($data1 = mysqli_fetch_array($sql1)) //Iterate throug the DB results { $checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); $level_data = array(); $level_data[] = $data1; } $checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); Quote Link to comment https://forums.phpfreaks.com/topic/227979-having-issues-with-2-while-loops/ Share on other sites More sharing options...
harristweed Posted February 17, 2011 Share Posted February 17, 2011 if($current_level_id != $data['level_id']) { $checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); $current_level_id = $data['level_id']; $subject_data = array(); } The array $subject_data will be initialised every loop!. So will only contain the last entry. Move outside the loop! $subject_data = array(); while($data = mysqli_fetch_array($sql)) { if($current_level_id != $data['level_id']) { $checkboxes .= createLevelCheckboxes($subject_data, $level_data, 5); $current_level_id = $data['level_id']; } //Add the current record to the $level_data array $subject_data[] = $data; } Quote Link to comment https://forums.phpfreaks.com/topic/227979-having-issues-with-2-while-loops/#findComment-1175573 Share on other sites More sharing options...
genzedu777 Posted February 17, 2011 Author Share Posted February 17, 2011 Hi harristweed, It's even worse after applying outside the loop. It is showing everything Initially it was... Level 1 Sci Level2 Sci Level3 Sci Now it is... Level 1 Math, Eng, Sci Level2 Math, Eng, Sci Level3 Math, Eng, Sci Correct example should be Level 1 Math Level 2 Eng Level 3 Sci And I realised you can't remove the $subject_data = array();, as it helps to store the correct value (math) into the correct level (level1) Any other advice? Can someone please enlighten me! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/227979-having-issues-with-2-while-loops/#findComment-1175577 Share on other sites More sharing options...
ansharma Posted February 17, 2011 Share Posted February 17, 2011 please explain createLevelCheckboxes() function. it might be possible that if it is a user defined function then it contain some conceptual flaw Quote Link to comment https://forums.phpfreaks.com/topic/227979-having-issues-with-2-while-loops/#findComment-1175589 Share on other sites More sharing options...
genzedu777 Posted February 17, 2011 Author Share Posted February 17, 2011 Hi ansharma, This is my function () function createLevelCheckboxes($subjectArray, $levelArray, $columns) { if(count($levelArray)==0) { return false; } $htmlOutput = ''; //Display level header row $levelID = $levelArray[0]['level_id']; $levelName = $levelArray[0]['level_name']; foreach($levelArray as $data1) { //Display level header row $levelID = $levelArray[0]['level_id']; $levelName = $levelArray[0]['level_name']; $checked = ($data1['checked'] == 1) ? ' checked="checked"' : ''; $htmlOutput .= "<tr>\n"; $htmlOutput .= "<th colspan=\"{$columns}\" style=\"text-align:left;padding-top:15px;\">"; $htmlOutput .= "<input name=\"level[]\" type=\"checkbox\" id=\"level_{$levelID}\" type=\"checkbox\" {$checked} value=\"{$levelID}\">"; $htmlOutput .= "<span class=\"zone_text_enlarge\"><label for=\"level_{$levelID}\">{$levelName}</label></span>"; $htmlOutput .= "</th>\n"; $htmlOutput .= "</tr>\n"; } //Display each subject $recordCount = 0; foreach($subjectArray as $data) { //Increment counter $recordCount++; //Start new row if needed, 1/5 = R1 --> So create a new row if ($recordCount % $columns == 1) { $htmlOutput .= "<tr>\n"; } //Display the record $subjectID = $data['subject_level_id']; $subjectName = $data['subject_name']; $checked = ($data['checked'] == 1) ? ' checked="checked"' : ''; $htmlOutput .= " <td>\n"; $htmlOutput .= " <input name=\"subject_level[]\" class=\"subject_a\" type=\"checkbox\" {$checked}"; $htmlOutput .= " id=\"subject_level_{$subjectID}\" value=\"{$subjectID}\"/>\n"; $htmlOutput .= " <label for=\"subject_level_{$subjectID}\" class=\"subject_1\">{$subjectName}</label>\n"; $htmlOutput .= "</td>\n"; //Close row if needed, 5/5 = 0 --> So close the row if ($recordCount % $columns == 0) { $htmlOutput .= "</tr>\n"; } } //Close last row if needed if ($recordCount % $columns != 0) { $htmlOutput .= "</tr>\n"; } return $htmlOutput; } Quote Link to comment https://forums.phpfreaks.com/topic/227979-having-issues-with-2-while-loops/#findComment-1175594 Share on other sites More sharing options...
KevinM1 Posted February 17, 2011 Share Posted February 17, 2011 If $subject_data is defined before your loop, then you're overwriting it when you turn it into a blank array. In other words: $subject_data = array(); Is overwriting whatever you have. Also, where is $sql1 coming from? Quote Link to comment https://forums.phpfreaks.com/topic/227979-having-issues-with-2-while-loops/#findComment-1175597 Share on other sites More sharing options...
genzedu777 Posted February 17, 2011 Author Share Posted February 17, 2011 Hi Nightslyr, I understand that in the 1st while loop, somehow the array will only keep the last value...so I am thinking if there is anyway that I can work it out to what I want to achieve. This is my query $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die(mysqli_error($dbc)); //Run query $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)); Quote Link to comment https://forums.phpfreaks.com/topic/227979-having-issues-with-2-while-loops/#findComment-1175603 Share on other sites More sharing options...
genzedu777 Posted February 18, 2011 Author Share Posted February 18, 2011 Hi all, It always happens, after posting a few more follow-up answers, everyone stops replying me. It really makes me wonder, if my question is too complex or my explanation is vague. And if it is vague, I really don't mind explaining further...but please reply me. I need to submit my first phase project very soon. Thank you!!! Quote Link to comment https://forums.phpfreaks.com/topic/227979-having-issues-with-2-while-loops/#findComment-1176075 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.