0

I have a database containing a list of students with their names and availed books (list of books, serialized, stored in one row). I and now trying to display all of the students and the list of books they availed, I created a loop within a loop, but the thing is it just display the first record. It displays fine, but just the first record.

mysql_select_db("MYDB", $con);
$result = mysql_query("Select * From EnrolleeList where Gradelvl = 2 Order By Lname ASC");
while($row=mysql_fetch_array($result))
    {
    echo $row['name'] . " " . $row['lastname'];
    $glvl = $row['Gradelvl'];
    $getbooks = $row['BookList']; //data containing book list

    $getbooks = unserialize($getbooks);
    $arrlength = count($getbooks);  

    for($actr = 0; $actr < $arrlength; $actr++) 
        {
        $result = mysql_query("Select * From  ELEMBooks where GradeLevel = '$glvl' and BID = '$getbooks[$actr]' ");
        while($row = mysql_fetch_array($result))
            {
            echo $row['Subject'];
            echo " - ";
            echo $row['Book'];
            echo " - ";
            echo $row['Weight'];
            echo " - ";
            }
        }       
}
mysql_close($con);
1
  • Just use different variable name for inner loop $row, $result etc. else your upper var values will be overwrite and your upper loop will be failed to get correct Commented Jun 16, 2016 at 6:06

1 Answer 1

2

For outer loop use $row and for inner row use different variable ex. $rowInner and same for all duplicate variables

while($row=mysql_fetch_array($result)) // $row here
    {
    echo $row['name'] . " " . $row['lastname'];
    $glvl = $row['Gradelvl'];
    $getbooks = $row['BookList']; //data containing book list

    $getbooks = unserialize($getbooks);
    $arrlength = count($getbooks);  

    for($actr = 0; $actr < $arrlength; $actr++) 
        {
        $resultInner = mysql_query("Select * From  ELEMBooks where GradeLevel = '$glvl' and BID = '$getbooks[$actr]' ");
        while($rowInner = mysql_fetch_array($resultInner)) // $rowInner here
            {
            echo $rowInner['Subject'];
            echo " - ";
            echo $rowInner['Book'];
            echo " - ";
            echo $rowInner['Weight'];
            echo " - ";
            }
        }       
}
Sign up to request clarification or add additional context in comments.

1 Comment

Would be better if you Explain why use diifer var name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.