Bricktop Posted March 6, 2008 Share Posted March 6, 2008 Hi guys, I have the following code: function menu($location = null) { $sql = mysql_query("SELECT location FROM database ORDER BY location ASC"); $locations = array(); $content .= 'menu1[0]=\'<a href="file.php?fct=showall">Show All</a>\''; while ($a = mysql_fetch_array($sql)) { $num = 0; ++$num; if (!in_array($a['location'],$locations)) { $content .= 'menu1['.$num.']=\'<a href="file.php?fct=showstuff&location='.$a['location'].'">'.$a['location'].'</a>\''; $locations[] = $a['location']; } } return $content; } Basically, it's a menu I've built which takes the locations from a database and lists them. This code works fine. However, in this version you can see I am trying to auto increment $num every time a location is output. I cannot get it to work, all locations come out as the same number - 1. What am I doing wrong please? Each number just needs to output in order everytime a location is listed, e.g 1, 2, 3, 4, 5 etc etc Thanks, hope this makes sense Link to comment https://forums.phpfreaks.com/topic/94767-simple-im-sure-cant-work-it-out-myself/ Share on other sites More sharing options...
roopurt18 Posted March 6, 2008 Share Posted March 6, 2008 You're setting $num to zero each time you enter the loop. Link to comment https://forums.phpfreaks.com/topic/94767-simple-im-sure-cant-work-it-out-myself/#findComment-485249 Share on other sites More sharing options...
Bricktop Posted March 6, 2008 Author Share Posted March 6, 2008 Hi, Thanks, sorry this is a little new for me, how do I fix this please? If you could give a code snippet it would be much appreciated. Thanks Link to comment https://forums.phpfreaks.com/topic/94767-simple-im-sure-cant-work-it-out-myself/#findComment-485258 Share on other sites More sharing options...
Bricktop Posted March 6, 2008 Author Share Posted March 6, 2008 lol sry just worked it out - so obvious! lol thanks again - i need more coffee Link to comment https://forums.phpfreaks.com/topic/94767-simple-im-sure-cant-work-it-out-myself/#findComment-485265 Share on other sites More sharing options...
roopurt18 Posted March 6, 2008 Share Posted March 6, 2008 If you need more coffee then likely you need more sleep. Cure the disease, not the symptoms. Link to comment https://forums.phpfreaks.com/topic/94767-simple-im-sure-cant-work-it-out-myself/#findComment-485294 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.