Jump to content

Help needed with an array loop... please


Solarpitch

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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()">

 

 

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.