Jump to content

[SOLVED] HELP - Looping Arrays


bronzemonkey

Recommended Posts

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
Share on other sites

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
Share on other sites

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