0

I am new to php and want to create a simple gallery using an extensive file and implementing it in the index.php: I create an array and use a foreach loop to add pictures with some additional info. Something is obviously wrong with the logic or/and the syntax : ( All I get so far is a blank red rectangle.

<?php>

$landscape = array(
'pic1'=> array('name'=>'picture1.jpg','label'=>'p1', 'text'=>''),
'pic2'=> array('name'=>'picture2.jpg','label'=>'p2', 'text'=>''),
'pic3'=> array('name'=>'picture3.jpg','label'=>'p3', 'text'=>''),
'pic4'=> array('name'=>'picture4.jpg','label'=>'p4', 'text'=>''),
);


$galleriya='';


foreach ($landscape as $key => $value) {

$galleriya .= "<div>
    <div style='float:left;height:140px;width:200px;border:1px solid red;'>
        <img src=$value['name'] alt=''>
    </div>"
    ."<div style='font-size:50px;'>$value['label']</div>"
    ."<div style='font-size:50px;'>$value['text']</div>
    </div>"
};

echo($galleriya);


?>
  • I'm surprised its getting as far as rectangles. There are multiple syntax errors within the foreach loop. That aside, you seem to be creating an array, then building a set of divs within a variable using a foreach loop, just to echo them all out again? Where is the image source? What exact errors are you getting? Why have you chosen this approach? – Stephen May 27 '18 at 19:15
  • Hi, Stephen. It is for educational purpose, i learn and experiment. And when the things go out of control i ask for help ( : My idea is to create a gallery with pictures which contain also label and discription. That is why i use a div for container. The images are in the same dir as the php file (the names of the jpegs coincide with the name of the elements of the array). Maybe i should not use src attribute in that case? The error is 'not found' 404. – Kiril Mladenov May 27 '18 at 19:50

1 Answers1

0

I've tidied up the syntax stuff within the foreach loop. You need to have a read of this article, which has a brilliant explanation of when to use single and double quotes.

What is the difference between single-quoted and double-quoted strings in PHP?

$landscape = array(
    'pic1' => array('name' => 'picture1.jpg', 'label' => 'p1', 'text' => ''),
    'pic2' => array('name' => 'picture2.jpg', 'label' => 'p2', 'text' => ''),
    'pic3' => array('name' => 'picture3.jpg', 'label' => 'p3', 'text' => ''),
    'pic4' => array('name' => 'picture4.jpg', 'label' => 'p4', 'text' => ''),
);


$galleriya = '';

foreach ($landscape as $key => $value) {

    '<div style="float:left">'.
    '<div style="height:140px;width:200px;border:1px solid red;">
        <img src="' . $value["name"] . '">
    </div>'

    . '<div style="font-size:50px;">' . $value["label"] . '</div>'
    . '<div style="font-size:50px;">' . $value["text"] . '</div>
    </div>';
}

echo($galleriya);

For the images to display, they will need to be in the same folder as this script. If you wanted to move them to another folder such as images, then the images folder must be in the same place as this script and the source would be modified to (for example):

<img src="images/' . $value["name"] . '"
Stephen
  • 395
  • 1
  • 15
  • Thank you for the correction and for the very useful link! I am still getting this rectangle with 404 'not found' error. But the images are in the same dir and the names of the jpegs are respectively 'picture1', 'picture2', etc : ( – Kiril Mladenov May 27 '18 at 19:41
  • My bad. I missed a > from the end of the – Stephen May 27 '18 at 19:54
  • If you are specifying a height for the divs, you will have to make sure your images are the same size as the divs they are going into. Alternatively, you can specify the height and width of the image inside the image tags. That is starting to veer outside of the scope of the original question though. – Stephen May 27 '18 at 20:01