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. Quote Link to comment 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"; ?> Quote Link to comment 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"; ?> 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.