Jump to content

Help needed setting up foreach with multiple arrays


titanya

Recommended Posts

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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Wooooaaaahhh!!!!

 

So much information!!!  :o  ;D

 

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  ;D

 

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!

Link to comment
Share on other sites

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... ???

Link to comment
Share on other sites

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!  ;D

 

Working now! Ta!  ;D

Link to comment
Share on other sites

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!  ::)  ;D

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.