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
https://forums.phpfreaks.com/topic/63134-getting-php-info-in-javascript-array/
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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.