Solarpitch Posted November 30, 2007 Share Posted November 30, 2007 Hey Guys, I am having serious trouble trying to get this script to run. I am bascially trying to retrive a set of results from the database and disply them in a drop down menu. The result returned is simply the name of an article, and I want to populate a drop down menu with a list of all the article names in the database. The problem seems to be with the foreach loop as if I take it away the page loads fine at least and the same if I change the loop to a for loop. <?php // This is just the function to get the results, noting major here... function gethistory() { dbconnect(); $rowA = array(); $sql = "SELECT * FROM portal_article WHERE article_cat = 'History'"; $result = mysql_query($sql) or die(mysql_error()); while(($row = mysql_fetch_row($result)) != false) { $rowA[] = $row; } return $rowA; } // call the above function to get the results... $gethistory = gethistory(); echo "<script type=\"text/javascript\">\n"; echo "var menu1=new Array();\n"; // now loop through the result and display a list for the drop down menu... foreach($i=0; $i<count($gethistory); $i++) { echo "menu1[".$i."]='<a href='http://www.johnlockes.com/articles.php?article_id=".$gethistory[$i][0]."'>".$gethistory[$i][2]."</a>'"; } echo "</script>\n"; ?> I've been at this for days so any help would be great, thanks Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 30, 2007 Share Posted November 30, 2007 You don't start a foreach loop in the same was as a for loop - it wouldn't make sense. Try: foreach($gethistory as $k => $v) { echo "menu1[".$k."]='<a href='http://www.johnlockes.com/articles.php?article_id=".$v[0]."'>".$v[2]."</a>'"; } Of course, you could always just leave it as a for loop. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 30, 2007 Author Share Posted November 30, 2007 It doesnt work with the for loop either. Ok, I tried your code there and the page seemed to load just fine but when the link is licked it doesnt seem to pick up that its a drop down menu.. Here's the link for the drop down itself... when the link is clicked it just seems to load index.php instead of popping up with the drop down. This works fine for when the code above code is not in a loop. ie.. hardcoded in. Must be something to do with it not picking up "Menu1". <a href="../index.php" onClick="return dropdownmenu(this, event, menu1, '200px')" onMouseout="delayhidemenu()"> 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.