2

I have a method which queries top 5 items from selected table and adds each row to a list. When i try to make an ordered list from this method, it lists 10 items not 5. One correct line and one blank line on each iteration. I've tried both a method in class and a function outside of the class but result is same.

Here is my method:

/**
* @return array
*/
public function top_list(): array {
    /* @var mysqli_result $result */
    $sql = "SELECT name FROM users ORDER BY performance DESC LIMIT 5";
    $result = self::$database->query($sql);
    if (!$result) {
        exit("Database query failed.");
    }
    $list = [];
    while ($record = $result->fetch_assoc()) {
        $list[] = $record;
    }
    $result->free();
    return $list;
}

and here is my ordered list:

$user = new User();
foreach ($user->top_list() as $item) {
    echo "<li>";
    echo $item["name"];
    echo "<li />";
}

Result of ordered list

print_r of $user->top_list()

1 Answer 1

6

echo "<li />";

should be

echo "</li>";

You are not closing the list tag correctly, and the HTML renderer is attempting to create a new list item for each <li /> it sees.

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

2 Comments

Thanks for your well explained help and i can't believe myself.
You're welcome! No worries; it happens to the best of us. :-)

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.