I would like to display all categories randomly along with collections also randomly.
$array_category[]=array("category"=>"MIEJSCA","collection"=>"Miasta","collection"=>"Uzdrowiska polskie");
$array_category[]=array("category"=>"SZTUKA","collection"=>"Łódzkie Witraże");
$array_category[]=array("category"=>"KRAJOBRAZ","collection"=>"Karkonosze");
$array_category[]=array("category"=>"MIASTA","collection"=>"Łódź Nocą");
$array_category[]=array("category"=>"ZAMKI","collection"=>"Szlakiem Orlich Gniazd","collection"=>"Polskie Zamki Gotyckie");
shuffle( $array_category );
$randNumKat = rand( 1, count( $array_category ) );
foreach ( $array_category as &$thiskat)
{
echo '<div class="kat">
<a href="?kategoria='.$thiskat["category"].'">'
.$thiskat["category"].
'</a>
<kol>
<kol_naz>
<a href="?kolekcja='.$thiskat["collection"].'">
'.$thiskat["collection"].'
</a>
</kol_naz>
</kol>
</div>';
}
unset( $thiskat );
This code displays the categories correctly, but I do not know how to display the collections correctly by category. Please help.
Using the proper structure
Rather than just an array, you likely need an array of objects. Each object would then contain both a category and a nested array of collections.
I won't do all the work for you as it is better for you to learn, but try running the following code to see how it works:
$array = [
(object)[
'category' => 'MIEJSCA',
'collections' => [
'Miasta',
'Uzdrowiska polskie'
]
],
(object)[
'category' => 'SZTUKA',
'collections' => [
'Łódzkie Witraże'
]
],
(object)[
'category' => 'KRAJOBRAZ',
'collections' => [
'Karkonosze'
]
],
(object)[
'category' => 'MIASTA',
'collections' => [
'Łódź Nocą'
]
],
(object)[
'category' => 'ZAMKI',
'collections' => [
'Szlakiem Orlich Gniazd',
'Polskie Zamki Gotyckie'
]
]
];
$random = rand(0, count($array) - 1);
$category = $array[$random]->category;
$collections = $array[$random]->collections;
echo "Category is $category
";
foreach($collections as $collection){
echo " Collection is $collection
";
}
An indexing issue with rand()
One other issue was with your rand() function as follows:
You do not seem to be using your rand function in the above code, but thought I would mention it.
Displaying all categories and all collections in random order
If you are wanting to display all categories and all collections, just in random order you will need to change my code as follows:
Inside the outer loop do the following:
Inside the inner loop do the following: