i'm trying to build a Media Library website using php. i have 3 categories (books , movies and music ) that i included in the same array like this :
<?php
$catalog = [];
$catalog[101] = [
"title" => "Book1",
"img" => "img/img1.jpg",
"genre" => "genre1",
"year" => "2016",
"category" => "Books",
"author" => "someone"
];
$catalog[102] = [
"title" => "Book2",
"img" => "img/img1.jpg",
"genre" => "genre1",
"year" => "2016",
"category" => "Books",
"author" => "someone"
];
$catalog[103] = [
"title" => "Book3",
"img" => "img/img1.jpg",
"genre" => "genre1",
"year" => "2016",
"category" => "Books",
"author" => "someone"
];
$catalog[104] = [
"title" => "Book4",
"img" => "img/img1.jpg",
"genre" => "genre1",
"year" => "2016",
"category" => "Books",
"author" => "someone"
];
$catalog[105] = [
"title" => "Book5",
"img" => "img/img1.jpg",
"genre" => "genre1",
"year" => "2016",
"category" => "Books",
"author" => "someone"
];
$catalog[201] = [
"title" => "Movie1",
"img" => "img/img2.png",
"genre" => "genre1",
"year" => "2016",
"category" => "Movies",
"actor" => "someone"
];
$catalog[202] = [
"title" => "Movie2",
"img" => "img/img2.png",
"genre" => "genre1",
"year" => "2016",
"category" => "Movies",
"actor" => "someone"
];
$catalog[203] = [
"title" => "Movie3",
"img" => "img/img2.png",
"genre" => "genre1",
"year" => "2016",
"category" => "Movies",
"actor" => "someone"
];
$catalog[204] = [
"title" => "Movie4",
"img" => "img/img2.png",
"genre" => "genre1",
"year" => "2016",
"category" => "Movies",
"actor" => "someone"
];
$catalog[301] = [
"title" => "Song1",
"img" => "img/img3.png",
"genre" => "genre1",
"year" => "2016",
"category" => "Music",
"singer" => "someone"
];
$catalog[302] = [
"title" => "Song2",
"img" => "img/img3.png",
"genre" => "genre1",
"year" => "2016",
"category" => "Music",
"singer" => "someone"
];
$catalog[303] = [
"title" => "Song3",
"img" => "img/img3.png",
"genre" => "genre1",
"year" => "2016",
"category" => "Music",
"singer" => "someone"
];
$catalog[304] = [
"title" => "Song4",
"img" => "img/img3.png",
"genre" => "genre1",
"year" => "2016",
"category" => "Music",
"singer" => "someone"
];
?>
and then i displayed in the home page just the books category
"category" => "Books",
using this code :
<ul class="item">
<?php
foreach($catalog as $id => $tem){
if ($tem["category"] == "Books"){
echo get_item_html($id,$tem);
}
}
?>
</ul>
the get_item_html function is this :
<?php
function get_item_html($id,$tem){
$output = "<li><a href='#'><img class='img' src='" . $tem["img"] . "'
alt='" . $tem["title"] . "' />" . "<p>view details</p>" . "</a></li>";
return $output;
}
?>
Now all the books items will be displayed in the Home page but i want Just four of them to be displayed randomly.
so what i want is to display 4 items of the array randomly. but they all should have the key and value of : "category" => "Books",.
thank you in advance .