Jump to content

[SOLVED] Help - For loop Array menu duplication


rahish

Recommended Posts

This should be easy for someone. I am trying to populate a simple html list menu with urls and link text using an array and a for loop.

 

I cannot work out what I am doing wrong but I know I am only guessing, I have got my for loops mixed up and it is duplicating the output.

 

 

Here is the example



<?php
$menu_link = array (0 =>'index.php', 'info.php', 'contact.php');
$menu_text = array (0 =>'Home', 'Information', 'Contact');


?>


<h3> Menu Example</h3>

<?php  


print_r($menu_link);
print_r($menu_text);
?>
<?php



echo "<ul>";
for ($text=0; $text < sizeof($menu_text); $text++) {


for ($i=0; $i < sizeof($menu_link); $i++) {
echo  "<li><a href='", $menu_link[$i], "'>$menu_text[$text] "; //$i,


//echo  " ",$menu_text[$text], "</li>";

	}	

}


echo "</a></li>";

echo "</ul>";

?>

Link to comment
Share on other sites

$menu_link = array (0 =>'index.php', 'info.php', 'contact.php');
$menu_text = array (0 =>'Home', 'Information', 'Contact');
$items=count($menu_link);
for ($i=0;$i<$items;$i++) {
  echo '<a href="'.$menu_link[$i].'">'.$menu_text[$i].'</a>';
}

Link to comment
Share on other sites

Just thought - probably a better way is this:

  $arrLinks=array('index' => 'Home Page',
                  'info' => 'Information',
                  'contact' => 'Contact');
  foreach ($arrLinks as $key => $val) {
    echo '<a href="'.$key.'.php">'.$val.'</a>';
  }

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.