plutomed Posted August 15, 2007 Share Posted August 15, 2007 I have this bit of code and it seems to only echo the first one in te array, why? <?php function main_nav() { $main_nav = array( "names" => array("Home", "Pictures", "Templates", "Website Coding"), "pages" => array("index.php", "pictures.php", "templates.php", "website_coding.php") ); $count_main = count($main_nav['names']); $i_main = 0; $i_main2 = 10; $links .= " <a href=\"".$main_nav['pages'][$i_main]."\" class=\"glink\" id=\"g".$i_main2."\">".$main_nav['names'][$i_main]."</a> "; echo $links; while($i_main < $count_main) { $i_main++; $i_main2++; } } ?> Link to comment https://forums.phpfreaks.com/topic/65138-solved-arrays-and-echoing-all/ Share on other sites More sharing options...
lemmin Posted August 15, 2007 Share Posted August 15, 2007 Your loop just increments variables, your echo statement is outside te loop. Try changing this: echo $links; while($i_main < $count_main) { $i_main++; $i_main2++; } to: while($i_main < $count_main) { echo $links; $i_main++; $i_main2++; } Link to comment https://forums.phpfreaks.com/topic/65138-solved-arrays-and-echoing-all/#findComment-325117 Share on other sites More sharing options...
BlueSkyIS Posted August 15, 2007 Share Posted August 15, 2007 because your function echos $links, then loops over some numbers doing nothing: echo $links; // You echo one time, but then.... while($i_main < $count_main) { // Nothing is happening here. $i_main++; $i_main2++; } Link to comment https://forums.phpfreaks.com/topic/65138-solved-arrays-and-echoing-all/#findComment-325122 Share on other sites More sharing options...
plutomed Posted August 15, 2007 Author Share Posted August 15, 2007 Thanx I changed it to that and it now works except the increment dosn't seem to be working as it is echoing the same link 4 times ??? Link to comment https://forums.phpfreaks.com/topic/65138-solved-arrays-and-echoing-all/#findComment-325131 Share on other sites More sharing options...
BlueSkyIS Posted August 15, 2007 Share Posted August 15, 2007 while($i_main < $count_main) { $links .= " <a href=\"".$main_nav['pages'][$i_main]."\" class=\"glink\" id=\"g".$i_main2."\">".$main_nav['names'][$i_main]."</a> "; $i_main++; $i_main2++; } echo $links; Link to comment https://forums.phpfreaks.com/topic/65138-solved-arrays-and-echoing-all/#findComment-325134 Share on other sites More sharing options...
plutomed Posted August 15, 2007 Author Share Posted August 15, 2007 Yes! thanx guys, I know the fist mistake was a stupid one Link to comment https://forums.phpfreaks.com/topic/65138-solved-arrays-and-echoing-all/#findComment-325140 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.