0

I am trying to run while loop in a while loop, but only single row is displayed.

  <?php    
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT u.id, u.firstname, u.lastname, u.email, c.fullname, ro.shortname, c.id as courseid
from mdl_context cx 
Left join mdl_course c ON cx.instanceid = c.id
Left join mdl_role_assignments r  on  r.contextid = cx.id
Left join mdl_user u on u.id = r.userid
Left join mdl_role ro on ro.id = r.roleid
WHERE r.roleid IN (11)
AND cx.contextlevel =50 GROUP BY u.id ORDER BY u.id ASC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    echo "<table>"."<tr>"."<td width='100px;'><h4>Name</h4></td>"."<td width='50px;'>"."</td>"."<td width='230px;'><h4>Email</h4></td>"."<td width='100px;'><h4>Role</h4></td>"."<td width='100px;'>"."</td>"."<td><h4>Course</h4></td>"."</table>"."<br>";
    while($tmg = $result->fetch_assoc()) {
    echo "<table>"."<tr>"."<td width='100px;'><a href='http://localhost/user/profile.php?id=".$tmg["id"]."'>".$tmg["firstname"]." ".$tmg["lastname"]."</a></td>"."<td width='50px;'>"."</td>"."<td width='230px;'>".$tmg["email"]."</td>"."<td width='100px;'>".$tmg["shortname"]."</td>";
    while($row = $result->fetch_assoc()) {
    echo    "<td width='100px;'>"."</td>"."<td>".$row["fullname"]."</td>"."</table>"."<br>";
    }
    }
} else {
    echo "0 results";
}
$conn->close();
 ?>

Any guidance or help will be much appreciated.

7
  • 2
    What about executing another query $sqlt Commented May 29, 2015 at 11:23
  • No definition for this "$sqlt" or it just a typo :P Commented May 29, 2015 at 11:24
  • What about the another query? Commented May 29, 2015 at 11:24
  • you are not executing your second query. check that. Commented May 29, 2015 at 11:27
  • I've corrected the code. Now outer while loop is executing once! Why? Commented May 29, 2015 at 11:36

1 Answer 1

1

A typo need to execute another query $sqlt as

$sqlt = "SELECT u.firstname, u.lastname, c.id, c.fullname FROM mdl_course AS c JOIN mdl_context AS ctx ON c.id = ctx.instanceid JOIN mdl_role_assignments AS ra ON ra.contextid = ctx.id JOIN mdl_user AS u ON u.id = ra.userid WHERE u.id IN ($newarray)";
$result2 = $conn->query($sqlt);//forgot to execute
while($row = $result2->fetch_assoc()) {
Sign up to request clarification or add additional context in comments.

3 Comments

I've corrected the code, now outer while loop is executing once! Why?
Why you were fetching same array twice
Because at 1st i want unique records and in second i want to display all the courses related to 1st array id.

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.