I have a query that selects all the users from my table. Then my while loop loops through it. If the current user is friends with the user in the queried row the row of data is skipped, if not, then the row is displayed. How can I make this while loop stop after 3 successful rows are display? Just to be clear, not just 3 rows can be queried.
Here is my code:
$query_know="SELECT * FROM users ORDER BY RAND()";
$result_know= mysqli_query($connect, $query_know);
$i= 0;
while ($row_know= mysqli_fetch_array($result_know)) {
$query_friend_test= "SELECT * FROM relations WHERE user1= '".$user_id."' AND user2= '".$row_know['user_id']."'";
$result_friend_test= mysqli_query($connect, $query_friend_test);
if (mysqli_num_rows($result_friend_test) > 0) {
continue;
}
else {
?>
<div class="user-mini">
<div class="user-mini-left">
</div>
<div class="user-mini-right">
<div class="story-user-mini">
<p><a href="<?php echo "profile.php?id=" . $row_know['user_id']; ?>"> <?php echo $row_know['fname'] . " " . $row_know['lname']; ?> </a></p>
</div>
<div class="story-content-mini">
<p> Ohio University </p>
</div>
</div>
</div>
<?php
}
}
?>
For whatever
$nnumber of rows you want to display:When putting it in a
forloop, it means you’re doing it only from$i = 0to$i < $n, and when you use awhileloop, you’re doing it forever, that is until you issue abreakstatement somewhere. If the number of rows is definite, you can usefor.EDIT: I meant to say
for, notforeach.