Jump to content

chibola

New Members
  • Posts

    5
  • Joined

  • Last visited

Everything posted by chibola

  1. Ooooh, brain autofilling! I see what you mean. Thank you! I appreciate your time. Have a great day!
  2. 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!)
  3. 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
  4. 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
  5. 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!!!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.