How should I do pagination for foreach?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
24 views
In bd, the recorded code:

// Get all articles get_all_bookmarks function() { global $db; $bookmarks = $db->query("SELECT * FROM entries"); return $bookmarks; }


The content of the recorded code
<?php$bookmarks = get_all_bookmarks();foreach ($bookmarks as $bookmarks): ?><?php $short_category_name = get_category_by_id($bookmarks["category_id"]); ?><?php $book = get_book_by_id($bookmarks["id_of_book"]); ?>"class="adiv"> <?php echo $bookmarks["title"];?><?php echo $bookmarks["structure"];?><?php echo $bookmarks["example"];?>"class="sect"> <?php echo $book["short_book_name"];?>"class="cat"> <?php echo $short_category_name["short_category_name"];?>#Comments';"><?php echo $bookmarks["comments"];?><?php echo date("m.d.Y в H:i", strtotime($bookmarks["date"]));?><?php endforeach ?>
by | 24 views

1 Answer

0 like 0 dislike
$bookmarks = $db->query("SELECT * FROM entries"); foreach ($bookmarks as $bookmark) { ... get_category_by_id() get_book_by_id() ... }

Suppose you have a database of 100 bookmarks. Then you first query out all of them, and then start iterating (foreach) and for EACH bookmark, make get_category_by_id() and get_book_by_id().
In the end you 201 request. The server is not dead, most likely, but he already felt sick.
If bookmarks will be several thousands - will die (or privyet this request by timeout, for example).

All this can be replaced by a single query with a join-AMI (random guide) to get bookmarks with category and book.
And you can select * and specify the limit and offset (random guide).

In the end, the sample with pagination looks something like this:
select something from table left join the other table on something left join another table on that limit 10 -- ten records offset 30 -- after the thirtieth

that is, will the selected records 31 to 40
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
0 comments
40,796 users