Jump to content

Can there be 2 while loops in a single code?


genzedu777

Recommended Posts

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.