0

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

            ?>

3 Answers 3

1

For whatever $n number of rows you want to display:

for($i = 0; $i < $n; $i++) {
    $row_know = mysqli_fetch_array($result_know);

    ... rest of code ...

}

When putting it in a for loop, it means you're doing it only from $i = 0 to $i < $n, and when you use a while loop, you're doing it forever, that is until you issue a break statement somewhere. If the number of rows is definite, you can use for.

EDIT: I meant to say for, not foreach.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use break to exit a while loop.

Comments

0

do this condition check in the else part and an increment of $i++ inside the else {}

  else if($i <=2) {

        ?>

        <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  $i++;
        }
        }

Comments

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.