titanya Posted October 27, 2008 Share Posted October 27, 2008 Apologies if this has been asked before... as it has, but I heave read through the different results for my search and being a relative amateur to php, I still can't make head nor tail of the replies, so I would be grateful if someone could tell me where I am going wrong in the simplest way possible! I am desperately trying to get my head around this 'foreach' stuff, as I have only ever really dabbled in php using the odd include and would love to know more, so I am finally sitting down and trying to learn it! My problem is as follows : I have 3 arrays in a separate include file called 'albums-array.php' $person = array("frank_jones","jim_smith","sandy_mitchell") $folder = array("007","002","010") $name = array("Frank","Jim","Sandy") I want to use these arrays in a foreach statement in order to display image galleries on a page in a table as follows <?php include("./includes/albums-array.php"); ?> </div> <div id="centre-panel"> <div class="centre-panel"> <h1>Latest Albums!</h1> <table width="430px" border="0" cellpadding="5"> <tr> <?php foreach($person as $person && $folder as $folder && $name as $name){ echo "<td><div align=\"center\"><span style=\"text-decoration : blink; font-weight: bold; color: red; padding-bottom: 5px;\">NEW!</span></div> <div align=\"center\"><a href=\"albums.php?gallery=" . $person . "/" . $folder . "/\" title=\"" . $linktitle . "\"><img src=\"./thumbs/" . $person . "/" . $folder . "/main.jpg\" width=\"100\" height=\"149\" border=\"2\" alt=\"" . $alt . "\" /></a></div><br /><br /> <a href=\"albums.php?gallery=" . $person . "/" . $folder . "/\" title=\"" . $linktitle ."\">" . $name . "</a></td>"; ?> </tr> </table> </div> Now... I don't know whether all of it is correct at present, as I'm still trying to get past my first error!!! Which is... Parse error: parse error, expecting `')'' in /home/virtual/site1/fst/var/www/html/home2.php on line 135 As far as I can see, there is no ')' missing, so I have absolutely NO idea where I am going wrong!!! Help! Pretty please! From a PHP newbie! Any advice GREATLY appreciated! Quote Link to comment Share on other sites More sharing options...
discomatt Posted October 27, 2008 Share Posted October 27, 2008 I would do it like this <pre><?php $person = array("frank_jones","jim_smith","sandy_mitchell"); $folder = array("007","002","010"); $name = array("Frank","Jim","Sandy"); for( $i = 0, $c = count($person); $i < $c; $i++ ) { echo "Person: {$person[$i]} \n"; echo "Folder: {$folder[$i]} \n"; echo "Name: {$name[$i]} \n"; echo "\n"; } ?></pre> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 27, 2008 Share Posted October 27, 2008 if you want to stick to the foreach, it would be like this: <?php include("./includes/albums-array.php"); ?> </div> <div id="centre-panel"> <div class="centre-panel"> <h1>Latest Albums!</h1> <table width="430px" border="0" cellpadding="5"> <tr> <?php foreach(array_keys($person) as $n){ echo "<td><div align=\"center\"><span style=\"text-decoration : blink; font-weight: bold; color: red; padding-bottom: 5px;\">NEW!</span></div> <div align=\"center\"><a href=\"albums.php?gallery=" . $person[$n] . "/" . $folder[$n] . "/\" title=\"" . $linktitle . "\"><img src=\"./thumbs/" . $person[$n] . "/" . $folder[$n] . "/main.jpg\" width=\"100\" height=\"149\" border=\"2\" alt=\"" . $alt . "\" /></a></div><br /><br /> <a href=\"albums.php?gallery=" . $person[$n] . "/" . $folder[$n] . "/\" title=\"" . $linktitle ."\">" . $name[$n] . "</a></td>"; ?> </tr> </table> </div> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 27, 2008 Share Posted October 27, 2008 more explanation....think of arrays as key/value pairs. for numeric arrays, keys are automatically assigned, starting at 0. so, for each of your arrays, the first value has a key of 0, the second: 1, the third: 2. a foreach statement loops over all the elements of an array, assigning it the variable after the 'as'. a couple of things you did wrong: - You cannot have an && statement in a foreach (that is where your error was coming from) - You cannot use the same variable for the assignment, as that will overwrite your array. so, after the as, you a different variable no, array_keys(). this is a trick for this particular case. normally, you would have one array, and just want the values, and would use something like: foreach($person as $p){ echo $p; } but, since you have 3 arrays, the trick i posted helps around that. array_keys() returns an array of the keys from the passed array. in your case, this would be the array (0,1,2). each time the loop executes, it sets $n to that number. then, you can use $n to access the appropriate value of the array. it might help to look at discomatt's post to see the incrementing going on, but my way is cleaner (in my opinion) Quote Link to comment Share on other sites More sharing options...
.josh Posted October 27, 2008 Share Posted October 27, 2008 Where are your arrays coming from? Is there some kind of guarantee that each array will have the same number of values in them? Personally, I would look into making a multi-dimensional array. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 27, 2008 Share Posted October 27, 2008 BTW: the foreach loop is really meant for dealing with arrays in which you want to make use of the array keys. It has the added benefit of having an open ended condition - that is, you don't necessarily need to know how many times the loop will iterate, kind of like counting to a certain number, but not necessarily knowing what that number is. If you were to re-organize your code to make a multi-dimensional array, a foreach loop might be of some use for you. You could make 'person' 'folder' and 'name' the keys to your array, with each 'row' of info the values, respectively. As it stands now, if you know exactly how many elements are in the arrays, simply make a for loop. I made a tutorial about php loops a while back, that you may or may not find useful. Quote Link to comment Share on other sites More sharing options...
titanya Posted October 27, 2008 Author Share Posted October 27, 2008 Wooooaaaahhh!!!! So much information!!! Thanks! Although I get some of what you have all said (I understand about the keys starting at 0) I think I will need to read a bit more into it all! Thanks ever so much for the help and the quick replies. I will go off and give it a go and report back if I have and problems, then I shall have a look through the links posted and see if I can get my head around it all. I suppose PHP is a bit like Trigonometry at school... You spend ages staring at the screen, then all at once it clicks and you understand it I have soooo many things I would like to do and I can see the potential of PHP to do it all, I just need to learn how to use it properly. What an excellent forum. Thanks a lot. I shall be back! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 27, 2008 Share Posted October 27, 2008 another good learning source: http://devzone.zend.com/node/view/id/627 Section 4 covers arrays... Quote Link to comment Share on other sites More sharing options...
titanya Posted October 27, 2008 Author Share Posted October 27, 2008 if you want to stick to the foreach, it would be like this: <?php include("./includes/albums-array.php"); ?> </div> <div id="centre-panel"> <div class="centre-panel"> <h1>Latest Albums!</h1> <table width="430px" border="0" cellpadding="5"> <tr> <?php foreach(array_keys($person) as $n){ echo "<td><div align=\"center\"><span style=\"text-decoration : blink; font-weight: bold; color: red; padding-bottom: 5px;\">NEW!</span></div> <div align=\"center\"><a href=\"albums.php?gallery=" . $person[$n] . "/" . $folder[$n] . "/\" title=\"" . $linktitle . "\"><img src=\"./thumbs/" . $person[$n] . "/" . $folder[$n] . "/main.jpg\" width=\"100\" height=\"149\" border=\"2\" alt=\"" . $alt . "\" /></a></div><br /><br /> <a href=\"albums.php?gallery=" . $person[$n] . "/" . $folder[$n] . "/\" title=\"" . $linktitle ."\">" . $name[$n] . "</a></td>"; ?> </tr> </table> </div> Right... I have tried the above and it's still not working. I have even put everything in the same page so that I wouldn't get confused with which line was which. I am getting the following error : Parse error: parse error in /home/virtual/site1/fst/var/www/html/home2.php on line 35 But line 35 is the closing </html> tag in the document! ??? See code below (this is the entire test page now, as I have pasted the include into it) : <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Test Albums Page</title> </head> <body> <?php $person = array("frank_jones","jim_smith","sandy_mitchell"); $folder = array("007","002","010"); $name = array("Frank","Jim","Sandy"); $linktitle = "My Latest Album"; $alt = "My Latest Album"; ?> <div> <h1>Latest Albums!</h1> <table width="430px" border="0" cellpadding="5"> <tr> <?php foreach(array_keys($person) as $n){ echo "<td><div align=\"center\"><span style=\"text-decoration : blink; font-weight: bold; color: red; padding-bottom: 5px;\">NEW!</span></div> <div align=\"center\"><a href=\"albums.php?gallery=" . $person[$n] . "/" . $folder[$n] . "/\" title=\"" . $linktitle . "\"><img src=\"./thumbs/" . $person[$n] . "/" . $folder[$n] . "/main.jpg\" width=\"100\" height=\"149\" border=\"2\" alt=\"" . $alt . "\" /></a></div><br /><br /> <a href=\"albums.php?gallery=" . $person[$n] . "/" . $folder[$n] . "/\" title=\"" . $linktitle ."\">" . $name[$n] . "</a></td>"; ?> </tr> </table> </div> </body> </html> I know this has to be something simple, but I just can't see it... ??? Quote Link to comment Share on other sites More sharing options...
titanya Posted October 27, 2008 Author Share Posted October 27, 2008 Right... I just broke it all down and took out the arrays, using 1 value for each variable and putting that into the code, so I know my code syntax is correct, and I hadn't missed out a semi colon or \ anywhere, as it worked. It was only when I put back the arrays and added the foreach bit and it didn't work.... then... wait... what's that? ARGHHH! It was a curly bracket missing! Where is the banging head on wall smiley when you need it! Working now! Ta! Quote Link to comment Share on other sites More sharing options...
titanya Posted October 30, 2008 Author Share Posted October 30, 2008 Right... all is going well and good. I have been doing a bit of reading and have managed to do one or two things, but something is foxing me... I have the table automatically writing itself with the galleries, however I have a table of 3 column across and 6 galleries at present. I managed to overcome this by using the following if/else statement : <?php foreach(array_keys($person) as $n){ if (($n + 1) == 3){ echo "<td><div align=\"center\"><span style=\"text-decoration : blink; font-weight: bold; color: red; padding-bottom: 5px;\">NEW!</span></div> <div align=\"center\"><a href=\"albums.php?gallery=" . $person[$n] . "/" . $folder[$n] . "/\" title=\"" . $linktitle . "\"><img src=\"./thumbs/" . $person[$n] . "/" . $folder[$n] . "/main.jpg\" width=\"100\" height=\"149\" border=\"2\" alt=\"" . $alt . "\" /></a></div><br /><br /> <a href=\"albums.php?gallery=" . $person[$n] . "/" . $folder[$n] . "/\" title=\"" . $linktitle ."\">" . $name[$n] . "</a></td></tr><tr>";} else { echo "<td><div align=\"center\"><span style=\"text-decoration : blink; font-weight: bold; color: red; padding-bottom: 5px;\">NEW!</span></div> <div align=\"center\"><a href=\"albums.php?gallery=" . $person[$n] . "/" . $folder[$n] . "/\" title=\"" . $linktitle . "\"><img src=\"./thumbs/" . $person[$n] . "/" . $folder[$n] . "/main.jpg\" width=\"100\" height=\"149\" border=\"2\" alt=\"" . $alt . "\" /></a></div><br /><br /> <a href=\"albums.php?gallery=" . $person[$n] . "/" . $folder[$n] . "/\" title=\"" . $linktitle ."\">" . $name[$n] . "</a></td>";} } ?> This is great at the moment, as I only have 6 galleries, however that is going to increase, so what I want to know is, how do I write the statement so that the code will put a new row for every 3 entries in the array? I know that there has got to be an easier way than making an if statement for every 3 values like so... if (($n + 1) == 3) if (($n + 1) == 6) if (($n + 1) == 9) etc etc... Would I need some sort of 'count' and then divide by 3 and that would be the number of rows?... I can see this being a problem is there are only two galleries on one row, as it would not be a whole number... or am I thinking about this in entirely the wrong way? Or would there be another foreach? As in... foreach (count ($n == 3)) {echo "blah blah links etc for table cell</tr><tr>";} Nah... that's wrong too... any ideas? Believe it or not, Maths was one of my strong points at school! 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.