Reputation: 371
I have a very simple PHP function, fetching all results (from two tables "Items" and "Categories" and displaying them on a single site (directly on index.php)
function fetchAllItems($pdo)
{
$statement = $pdo->prepare('select Items.*, Categories.*
from Items
INNER JOIN Categories ON Items.ItemCategoryID = Categories.id
ORDER BY Items.ItemName ASC'
);
$statement->execute();
return $statement->fetchAll(PDO::FETCH_CLASS, 'Item');
}
Now I want to add an icon to the left with css to the newest (= 10 last sql database entries / rows), but I don't know how to do.
As a PHP newbie my logic goes like this:
But I don't really know where to start / go from here and googling for the last 3.15 hours did not bring me any further?!
Thank you.
Upvotes: 0
Views: 73
Reputation: 781004
You can't simply subtract 10 from the highest ID to get the 10th highest, because there can be gaps in the ID sequence.
To get the 10 highest IDs, use:
SELECT id
FROM Items
ORDER BY id DESC
LIMIT 10
Put these into an array $first_10
, and then when you're displaying the results of fetchAllItems
you can do:
if (in_array($row->id, $first_10) {
$class = "newest";
} else {
$class = "";
}
Upvotes: 2