PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Displaying All Items

mohammed abdi abdulahi warsame
mohammed abdi abdulahi warsame
4,926 Points

the veriabel $item is not defined how can they set it as src for the img

1= HOW can they make . $item["img"] as source for the image while the item is not defined variabel??! 2= what is point of the dots the surrounded on the bigening of the html . "<p>view

foreach($catalog as $item){
           echo "<li><a href='#'><img src='" 
          . $item["img"] . "' alt='"
          . $item["title"] . "' />"
          . "<p>view Details</p>"
          . "</a></li>";

2 Answers

Erik McClintock
Erik McClintock
45,611 Points

Mohammed,

To answer your questions:

1) PHP has a loop known as "foreach" that will iterate over each item in an array or each property of an object and allow you to perform some action on them (whatever code is inside your code block, i.e. the curly braces). The syntax of a "foreach" loop in PHP is as follows:

foreach( $array_or_object_name_here as $some_temporary_variable_name_here ) {
    // whatever code you want executed on each element/property goes here
}

So, if we have an array called $catalog that contains a bunch of images, we could write our foreach loop as follows:

foreach( $catalog as $current_image ) {
    // then again, in here, i write whatever code i want to execute on each element of the $catalog array (which i will be referring to as "$current_image" based on how i wrote the foreach declaration)
}

The key thing to understand here is that you're going to be going over every item in your array when using a foreach loop, and so in order to perform actions on the element that you're on currently, you need to have a temporary variable name declared for yourself to call. You can call it whatever you want; in the code example you provided, they chose the name "$item", in the code I just wrote, I chose the name "$current_image". That can be whatever you want, you just need something to refer to your current item as. Thus, the "$item" variable that they use IS set, and it's updated for EACH item in the array as your loop iterates over them one after the other. Thus, during the first cycle of your loop, the "$item" variable it set to equal the first element in your $catalog array, and has all of its properties available to use. Once the loop finishes with that element, it moves onto the next, and now "$item" refers to that second element in the $catalog array, and so forth until all of the elements have been handled.

2) The dots that you see are how PHP concatenates strings. In JavaScript, it's a "+" sign, which is a little more intuitive, but that's all the dots are doing. They allow you to jump in and out of strings and mash them together into one, which is what is happening in the code example.

Hope this helps to clear some things up!

Erik

Julie Ragnarsson
Julie Ragnarsson
3,320 Points

I love how you explained this. I also had been having trouble understanding what "$category as $item" was doing, and your explanation was clearer by far than the explanation the PHP manual.

Erik McClintock
Erik McClintock
45,611 Points

That's great! I'm glad I was able to help clear some things up for you, Julie Ragnarsson!

The PHP manual (and a LOT of official documentation out there, unfortunately) is not terribly user-friendly, certainly not when you're fresh to these languages and to documentation in general. That said, you'll usually be able to find some decent, plain-English style explanations (with examples like I've provided here) if you do a search online for the concept you want to learn about and the language you're using it in, such as "foreach loop php" (as would be your search for this particular topic). Of course, posting questions on the forums here at Treehouse is also a fantastic way to learn, and you'll definitely get an answer (if not many answers) that can help you on your way! It just may not be as immediate as finding something online and pulling up a few different sites to hunt down an answer as soon as you need it :)

I have always found that it's most helpful to my particular learning style to have a given concept explained and demonstrated to me by a few different people or in a few different ways to really help ground it and give it a solid foundation in my head, so even with finding a working explanation here, it may be beneficial to hop onto Google and find some more definitions/examples, too!

Either way, again, glad I could help, and happy coding!

Erik

mohammed abdi abdulahi warsame
mohammed abdi abdulahi warsame
4,926 Points

thanks a lot buddy, that was more than enough, it was the best answer :) ,but i am facing more than one proplem of understanding the concept of php, even though i am at the middel of the way, but it seems like php is confusing because things goes arround all the time, i dont know how to say it but it needs more focus!!!!! and that makes it hard for the begginers like me to understand.