Good evening, I'm having a difficulty with arrays where I'd like them to stay in order so I can use them as follows.
In my database I have many images that I call with this code below.
<?php $image_search = 00001::conect_sql();
$image_search = $image_search -> prepare("SELECT img_page, img_title, img_url FROM images ORDER BY img_id ASC");
$image_search -> execute();
$image_search = $image_search -> fetchAll();
foreach($image_search as $key => $img_link) { } ?>
With this code I can list all the images without any problems, but what I would like to do is create an array with the structure below.
id | title | link
0 | título da imagem 1 | link da imagem 1
1 | título da imagem 2 | link da imagem 2
and so on, as in the case of an array that would be
[0] => [img_title] - [img_url]
[1] => [img_title] - [img_url]
The reason I want to do this is to be able to use the $ img_link variable in an echo anywhere in the script using the following formatting.
echo $img_link[0][img_title] - $img_link[0][img_title]
echo $img_link[1][img_title] - $img_link[1][img_title]
echo $img_link[2][img_title]
echo $img_link[3][img_title]
where I will choose which image to use only by changing the key number [X]
I got more or less with this code
$img_page_name[] = array($img_link['img_url']);
inside foreach
then with this
<?php echo $img_page_name[0][0];?>
where I want the image to appear.
Is there any better way to do this? one img_page = home would only display the last img_url that has img_page equal to home