chibola Posted August 15, 2014 Share Posted August 15, 2014 Hi, this is my first time posting here. I am just delving into PHP and I am learning about foreach loops. I have written code in Notepad++ EXACTLY the way I saw it in a tutorial video I watched (I wish I could show the tutorial video to you, but it is on Lynda.com and you have to pay to watch) I attached the file with my code. The example 1 code works just fine. The example 2 code is the one that is not working for some reason. However, it worked for the guy that wrote it in the video, so I am not sure where I am going wrong? *The comments in green are mainly for myself, I explain things to myself so that I don't forget what the code does forloops.php I would appreciate some help. Thank you!!! Quote Link to comment Share on other sites More sharing options...
requinix Posted August 15, 2014 Share Posted August 15, 2014 Next time, please just put the code in your post. Attachments are awkward. <!DOCTYPE html> <html lang="en"> <head> <title> For Loops </title> </head> <body> <!-- EXAMPLE 1 --> <!-- Below is the example that we used for the while loop code, and now we are going to write for loop code that will do the exact same thing that the while loop accomplishes --> <!-- while loop --> <p> While Loop </p> <?php $count = 0; while ($count <= 10) { //while count is less than or equal to 10, execute the below statement echo $count . ", "; // Echo out the variable count with the string ", " $count++; //increment by 1 } ?> <br /> <p> For Loop </p> <!-- For loop --> <!-- General Format for the for loops for (initial; test; each) { statement; } --> <?php for ($count = 0; $count <= 10; $count++) { /* Initialize $count to 0 check the test condition that $count must be less than or equal to 10 At the end of each loop, that is after executing the code in each loop, add 1 to $count */ echo $count . ", "; } ?> <br /> <br /> <!-- EXAMPLE 2 --> <!-- The following code identifies numbers as being either even or odd --> <?php for ($count = 20; $count > 0; $count--) { if ($count % 2 == 0) { echo "{$count} is even. <br />"; } else { echo "{$count} is odd. <br />"; } } /* Explanation of above code. Initialize $count to value of 20. As long as $count is greater than 0 then the for loop code should execute. Once the for loop code has executed, substract 1 from $count In the for loop: If the $count, when divided by 2, gives a remainder of 0, then the number is even. Otherwise, the number is odd. */ ?> </body> </html>It looks fine. How is it not working? What do you see when you do a View Source of the page? Quote Link to comment Share on other sites More sharing options...
chibola Posted August 15, 2014 Author Share Posted August 15, 2014 Hello, thank you very much for your prompt response. Before I get into your reply, I was wondering how do you post code on here? I have tried to make it work, but everything just showed up as uncolored and disorganized text, so I just opted for an attachment of my code instead. Now, I am getting the feeling that maybe the server just can't read the code or something? I agree the code is right. It worked in the video, but for some reason it is not working for me when I try to see it on the browser. The error message that I see says: Example 2: Associative Array ( ! ) Notice: Undefined variable: attribute in C:\wamp\www\sandbox\foreach_loop.php on line 72 Call Stack # Time Memory Function Location 1 0.0020 236872 {main}( ) ..\foreach_loop.php:0 : Kevin ( ! ) Notice: Undefined variable: attribute in C:\wamp\www\sandbox\foreach_loop.php on line 72 Call Stack # Time Memory Function Location 1 0.0020 236872 {main}( ) ..\foreach_loop.php:0 : Skoglund ( ! ) Notice: Undefined variable: attribute in C:\wamp\www\sandbox\foreach_loop.php on line 72 Call Stack # Time Memory Function Location 1 0.0020 236872 {main}( ) ..\foreach_loop.php:0 : 123 Main Street ( ! ) Notice: Undefined variable: attribute in C:\wamp\www\sandbox\foreach_loop.php on line 72 Call Stack # Time Memory Function Location 1 0.0020 236872 {main}( ) ..\foreach_loop.php:0 : Beverly Hills ( ! ) Notice: Undefined variable: attribute in C:\wamp\www\sandbox\foreach_loop.php on line 72 Call Stack # Time Memory Function Location 1 0.0020 236872 {main}( ) ..\foreach_loop.php:0 : CA ( ! ) Notice: Undefined variable: attribute in C:\wamp\www\sandbox\foreach_loop.php on line 72 Call Stack # Time Memory Function Location 1 0.0020 236872 {main}( ) ..\foreach_loop.php:0 : 90210 Quote Link to comment Share on other sites More sharing options...
chibola Posted August 15, 2014 Author Share Posted August 15, 2014 Oh, I'm sorry, I uploaded the wrong file to check: <!DOCTYPE html> <html lang="en"> <head> <title> Foreach loop </title> </head> <body> <!-- Foreach loops are a little different from the previous two types of loops. Foreach loops are going to take an array and loop through each item of the array until it gets to the end. If you think back, while loops and for loops know whether to quit or to keep looping by testing if a condition has been met. Foreach loops, on the other hand, are going to know whether to quit or whether there are items left in the array to loop over. The end of the array is what ends the looping. There's really just two approaches to looping. Let's use a real-world scenario for a second. Imagine that I have a classroom with 20 kids in it. And I have to put name tags on all of them. Now, I could just go around the room. And I could put a name tag on each kid and keep track of the numbers as I went. And knowing that there are 20 kids, when I had gotten to the number 20, I'd know I was done. Or, since I know that I have a finite set, I could just line them all up, start at the beginning and move down the line. And when I got to the end of the line, I would know I was done. I wouldn't have to keep track of the count as I was going. I mean, I could count. I could certainly know that I was on the fourth kid, but I wouldn't have to. I could just start at the first kid, go to the second kid, and then the third kid, until I got to the end of the line. That's how a foreach loop works. The syntax is also a little bit different. --> <!-- FOREACH LOOP GENERAL FORMAT FOR ARRAYS: foreach ($array as $value) { statement; } --> <!-- FOREACH LOOP GENERAL FORMAT FOR ASSOCIATIVE ARRAYS: foreach ($array as $key => $value) { statement } --> <!-- EXAMPLE 1 --> <p> Example 1: Regular Array </p> <?php $ages = array(4,8,15,16,23,42); foreach ($ages as $age) { echo "Age: {$age} <br />"; } /* Explanation of above code We have an $ages array with the numbers you see in orange contained within the array. The foreach function will execute a command on each of the items in the array. The new variable $age that you see in the ($ages as $age) parentheses is the placeholder for the items of the array. That is to say, when the loop runs, the $age variable will sequentially become 4, then 8, then 15, then 16, and so on, so the variable $age temporarily becomes each number in the array so that the code in the foreach loop can be executed on each item of the $ages array. The output will be a list with the string "Age: " followed by the different numbers in the array. A FOREACH LOOP ENDS BY ITSELF, BECAUSE IT ONLY LOOPS FOR AS MANY TIMES AS THERE ARE ITEMS IN THE ARRAY!!!!!!!!!!!!!!!!! */ ?> <br /> <br /> <p> Example 2: Associative Array </p> <?php $person = array( //Don't forget the commas after each index-object pair "first_name" => "Kevin", // Let's just call the index the "attribute" and the object the "data" "last_name" => "Skoglund", // since that is what they represent for the contents of this example "address" => "123 Main Street", "city" => "Beverly Hills", "state" => "CA", "zip_code" => "90210" ); foreach($person as $attribrute => $data) { $attr_nice = ucwords(str_replace("_", " ", $attribute)); echo "{$attr_nice}: {$data} <br />"; } /* Explanation of above code For each of the items in the array: the string replace (str_replace) function will take the part of the variable $attribute (the index) that has an underscore "_" , and replace it with an open space " " . So for example, we have first_name. The function will take first_name and replace the "_" with an " " in order to get "first name" ucwords function capitalizes each word in a string. So it will take "first name" and make it "First Name" The result of the rearrangements done by these two functions will be stored in the variable $attr_nice (attributes made to look nice for output display) The foreach loop will then output the $attr_nice variable with the corresponding $data. */ ?> </body> </html> (just in case the above is unreadable, i attached the file) foreach_loop.php Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted August 15, 2014 Solution Share Posted August 15, 2014 “attribrute” in line 71. I recommend that you start using an IDE (integrated development environment) like Netbeans or Eclipse. It will detect obvious mistakes while you write the code and save you a lot of time. The code highlighter in the forum is behind the “<>” button. Quote Link to comment Share on other sites More sharing options...
chibola Posted August 15, 2014 Author Share Posted August 15, 2014 (edited) Hello, thank you very much for your reply. I figured that the problem was the undefined variable $attribute, but it didn't make sense to me because according to the tutorial, that is the syntax for a foreach loop of an associative array. The guy in the tutorial never said anything about defining $attribute and his code worked just fine. FOREACH LOOP GENERAL FORMAT FOR ASSOCIATIVE ARRAYS: foreach ($array as $key => $value) { statement } <?php $person = array( //Don't forget the commas after each index-object pair "first_name" => "Kevin", // Let's just call the index the "attribute" and the object the "data" "last_name" => "Skoglund", // since that is what they represent for the contents of this example "address" => "123 Main Street", "city" => "Beverly Hills", "state" => "CA", "zip_code" => "90210" ); foreach($person as $attribrute => $data) { $attr_nice = ucwords(str_replace("_", " ", $attribute)); echo "{$attr_nice}: {$data} <br />"; } ?> the $attribute would be the $key in my code. So I'm not sure what went wrong. (and thank you very much, I finally figured out how to input colored code into the post!) Edited August 15, 2014 by chibola Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 15, 2014 Share Posted August 15, 2014 (edited) So I'm not sure what went wrong. But now you know, right? Edited August 15, 2014 by Jacques1 Quote Link to comment Share on other sites More sharing options...
chibola Posted August 15, 2014 Author Share Posted August 15, 2014 Ooooh, brain autofilling! I see what you mean. Thank you! I appreciate your time. Have a great day! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.