March 11, 201214 yr Hi All, Just wondering if anyone can help me to do the following: I am getting the links from the wordpress database and now would like to group 2 links at a time in <ul>'s Not sure how I can do a count with a foreach foreach ($links as $link) { $linkurl=$link->link_url; $linkname=$link->link_name; echo "<ul><li><a href='$linkurl' target='_blank'><h5>$linkname</h5></a></li>"; echo "</ul>"; } Thanks Edited March 11, 201214 yr by ayoungh
March 11, 201214 yr Use modulus to check for groups of two. D'oh. I feel really thick now - I've always done it by if(is_int($count / 2)).
March 12, 201214 yr Author D'oh. I feel really thick now - I've always done it by if(is_int($count / 2)). I'm not sure how I would do this to be honest. in the foreach loop how do I get every second item with that?
March 12, 201214 yr foreach ($links as $key => $link) { $linkurl=$link->link_url; $linkname=$link->link_name; echo (($key % 2 == 0) ? '<ul><li><a href='.$linkurl.' target="_blank"><h5>' : '$linkname</h5></a></li></ul>'); }
March 14, 201214 yr Author The only problem with the above is that I dont get 2 list items to each ul?
March 14, 201214 yr Sorry, I totally misunderstood what you meant in the original post. Not quite sure what I was thinking with that last bit of code! It should have been: foreach ($links as $key => $link) { $linkurl=$link->link_url; $linkname=$link->link_name; echo (($key % 2 == 0) ? '<ul><li><a href="'.$linkurl.'" target="_blank"><h5>'.$linkname.'</h5></a></li>' : '<li><a href="'.$linkurl.'" target="_blank"><h5>'.$linkname.'</h5></a></li></ul>'); } However, that's a very quick and dirty way of doing it which will go wrong if there isn't an even number of links in the array. Something like this would be better: $total = count($links); foreach ($links as $key => $link) { $linkurl=$link->link_url; $linkname=$link->link_name; if($key % 2 == 0){ echo "<ul>\n"; } echo "<li><a href=\"".$linkurl."\" target=\"_blank\"><h5>".$linkname."</h5></a></li>\n"; if($key % 2 == 1 || ($key + 1) == $total){ echo "</ul>\n"; } } Edited March 14, 201214 yr by Spitfire
March 17, 201214 yr Author Sorry, I totally misunderstood what you meant in the original post. Not quite sure what I was thinking with that last bit of code! It should have been: foreach ($links as $key => $link) { $linkurl=$link->link_url; $linkname=$link->link_name; echo (($key % 2 == 0) ? '<ul><li><a href="'.$linkurl.'" target="_blank"><h5>'.$linkname.'</h5></a></li>' : '<li><a href="'.$linkurl.'" target="_blank"><h5>'.$linkname.'</h5></a></li></ul>'); } However, that's a very quick and dirty way of doing it which will go wrong if there isn't an even number of links in the array. Something like this would be better: $total = count($links); foreach ($links as $key => $link) { $linkurl=$link->link_url; $linkname=$link->link_name; if($key % 2 == 0){ echo "<ul>\n"; } echo "<li><a href=\"".$linkurl."\" target=\"_blank\"><h5>".$linkname."</h5></a></li>\n"; if($key % 2 == 1 || ($key + 1) == $total){ echo "</ul>\n"; } } Perfect, Appreciate it
Create an account or sign in to comment