Jump to content

getting php info in javascript array


shepps_

Recommended Posts

ok i have a dynamic drop-down menu going on.  However, the links that i want to appear in the drop-down list (javascript array) are in the php database.

 

here's the javascript code:

var menu1=new Array()

menu1[0]='<a href="link.php">name</a>'

menu1[1]='<a href="link.php">name</a>'

menu1[2]='<a href="link.php">name</a>'

menu1[3]='<a href="link.php">name</a>'

 

and i want to do basically get info from my database, so i tried this but failed:

<? while ($d = mysql_fetch_object($res)) { ?>

        menu1[<?=$x?>]='<a href="products.php?de=<?=$d->did?>"><?=$d->dname?></a>'

<?  } ?>

 

any ideas?

 

all replies are appreciated :)

Link to comment
Share on other sites

what i meant by this part..

and i want to do basically get info from my database, so i tried this but failed:

<? while ($d = mysql_fetch_object($res)) {  ?>

        menu1[<?=$x?>]='<a href="products.php?de=<?=$d->did?>"><?=$d->dname?>[/url]'

<?  } ?>

is, i want to build a javascript array by cycling through the enteries in the database.  so say if there were 5 enteries in there, the while loop would generate menu1[0]...menu1[4], each containing a link in them.

 

really need this urgently, any advice would be a great help

Link to comment
Share on other sites

Assuming that your SQL is correct, etc, I would try:

 

<?php
for($i=0;$i<mysql_num_rows($res);$i++) {
  $row = mysql_fetch_assoc($res);
  echo "menu1[".$i."] = '<a href=\"products.php?de=".$row['did']."\">".$row['dname']."</a>";
}
?>

 

Does this help?

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.