bronzemonkey Posted November 11, 2007 Share Posted November 11, 2007 I'm looking to store a list of links and associated text in a php array and have my web pages display a small number of them at any given time. I need some help with the php needed to display these. Here is my code that is stored in a separate php file: <?php $page = $_SERVER['PHP_SELF']; $article = array(); $article[] = array("http://www.domain1.com/article/topic/","Link Title","Link Text","Link Description"); $article[] = array("http://www.domain2.com/article/topic/","Link Title","Link Text","Link Description"); $article[] = array("http://www.domain3.com/article/topic/","Link Title","Link Text","Link Description"); $article[] = array("http://www.domain4.com/article/topic/","Link Title","Link Text","Link Description"); $article[] = array("http://www.domain5.com/article/topic/","Link Title","Link Text","Link Description"); $article[] = array("http://www.domain6.com/article/topic/","Link Title","Link Text","Link Description"); $article[] = array("http://www.domain7.com/article/topic/","Link Title","Link Text","Link Description"); if($page == '/links.php') { echo " <h3>Articles</h3>\n"; echo " <ul>\n"; foreach ($article as $part) { echo " <li>\n"; echo " <h4><a href=\"$part[0]\" title=\"$part[1]\">$part[2]</a></h4>\n"; echo " <p>$part[3]</p>\n"; echo " </li>\n"; } echo " </ul>\n"; } else { } ?> If the page is my "links page" then all the links get displayed. But I am having trouble writing some php that works for the else{} part, which must only display the first 5 links (and their associated text) in the main array. Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/76865-solved-help-looping-arrays/ Share on other sites More sharing options...
kopytko Posted November 11, 2007 Share Posted November 11, 2007 Try this in your else block <?php echo "<h3>5 first articles</h3>\n"; echo "<ul>\n"; for($i=0; $i<5;$i++) { $part = $article[$i]; echo "<li>\n"; echo "<h4><a href=\"".$part[0]."\" title=\"".$part[1]."\">".$part[2]."</a></h4>\n"; echo "<p>".$part[3]."</p>\n"; echo "</li>\n"; } echo "</ul>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/76865-solved-help-looping-arrays/#findComment-389175 Share on other sites More sharing options...
bronzemonkey Posted November 11, 2007 Author Share Posted November 11, 2007 Thanks for your help, here's the final form of the code. <?php $page = $_SERVER['PHP_SELF']; $article = array(); $article[] = array("1","2","3","4"); $article[] = array("1","2","3","4"); $article[] = array("1","2","3","4"); $article[] = array("1","2","3","4"); $article[] = array("1","2","3","4"); $article[] = array("1","2","3","4"); $article[] = array("1","2","3","4"); $article[] = array("1","2","3","4"); echo " <h3>Articles</h3>\n"; echo " <ul>\n"; if($page == '/links.php') { foreach ($article as $part) { echo " <li>\n"; echo " <h4><a href=\"$part[0]\" title=\"$part[1]\">$part[2]</a></h4>\n"; echo " <p>$part[3]</p>\n"; echo " </li>\n"; } } else { for($i=0; $i<5;$i++) { $part = $article[$i]; echo " <li>\n"; echo " <h4><a href=\"$part[0]\" title=\"$part[1]\">$part[2]</a></h4>\n"; echo " <p>$part[3]</p>\n"; echo " </li>\n"; } } echo " </ul>\n"; ?> Link to comment https://forums.phpfreaks.com/topic/76865-solved-help-looping-arrays/#findComment-389359 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.