0
Good evening, I’m having a hard time 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 them 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 to 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 in an array that would be
[0] => [img_title] - [img_url]
[1] => [img_title] - [img_url]
Why I want to do this is so I can 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 just by changing the key number [X]
I sort of got it with this code
$img_page_name[] = array($img_link['img_url']);
inside the foreach
then with this
<?php echo $img_page_name[0][0];?>
in the place I wish the image to appear.
Is there a better way to do that? a cool thing would also be to run an if at the time of picking up the image, each image has an img_page in the case, in img_page = home would only display the last img_url that has the img_page equal to home
I will be as soon as I get back from work and give you the feedback, but it seems to work.
– flourigh