PHP Basics

Displaying Dynamic Data Exercise

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "Displaying Dynamic Data Exercise" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to modify the markup inside the foreach loop so the information for each exhibit is dynamically generated from the data.

Preview
Close

Transcript from the "Displaying Dynamic Data Exercise" Lesson

[00:00:00]
>> Maximiliano Firtman: So do you feel enough confidence to finish this lab yourself? I can give you five minutes.
>> Maximiliano Firtman: So what was the answer to this? So we are looping through the exhibits and we are getting different objects. Somehow we need to spread the object within the HTML. So something that I will do initially, I will add a paragraph that's going to reach out for debugging and I will say I will open php tag, close php tag, and I will say var_dump $object.

[00:00:46]
So it's not going to be nice, but if I refresh, now I can see on every article, HML article, at the top, I'm seeing what I have, okay? So then I say, yeah, I have a title. I have then a description, it's an array. But remember that when you see array, don't think just as a normal array in other languages.

[00:01:12]
Here, the array can also be kind of a map or a dictionary or a hash table. So it can contain also string keys. And then I just need to try to spread that here, how? Well, here, in the h2, instead of h2, I can just remove that and add a php tag with an echo that will take the object.

[00:01:36]
And because it's an array, I use square brackets, and in a string, the key. Remember, we also have this shortcut that is equals instead of php echo, okay? It's up to you if you wanna use that shortcut. For the description, something similar. So within the p, I will delete the hard code paragraph, and I will open php tag.

[00:02:03]
And within the php tag, I'm going to echo from object to description. And for the image, if we look at the image values, they don't have the full path. It's just the image name, the name of the file, right, the filename. But here, we see that all the images are in the gallery.

[00:02:24]
Question for you. Can I open php tag in the middle of an HTML attribute value? What do you think?
>> Student: Yes.
>> Maximiliano Firtman: Yes, why? Because PHP treats HTML as just a string output. It's just characters. It doesn't matter, right? Anywhere in the HTML, you can break the HTML and add PHP.

[00:02:51]
So that means that, yeah, I can do that here. Just have in mind to keep the HTML that will be result of this consistent and compatible. So it's object, image. So now I'm saving, reloading. Now I'm seeing the text, but not the image. So let's debug the image first.

[00:03:21]
>> Maximiliano Firtman: If we look at the image, we have sea-monsters.png. So let's see, do I have in gallery sea-monsters.png? I do have. So let's see if we have any typo somewhere, and let's remove the debug so it looks better, okay, like that. So I'm going to right-click here, inspect on the image and look what's going on.

[00:03:49]
>> Maximiliano Firtman: It's important to see the output. It's gallery only, gallery only, and say, why? Do I have an echo here?
>> Student: No.
>> Maximiliano Firtman: No, but it's not giving me any errors. No, it's not. It's just accessing the value and doing nothing with it. That's why you don't get an error, okay?

[00:04:09]
So be careful with that. So I don't have an echo, so it's actually not rendering that to the output.
>> Maximiliano Firtman: Makes sense? So not every time you have a PHP tag open, it's going to render something if you don't say so. So now, if I refresh now, I'm getting all the images.

[00:04:29]
And this is a server side generated app.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now