Solarpitch Posted November 29, 2007 Share Posted November 29, 2007 Hey guys, I posted a topic on thi sthe other day but It wasnt very clear. I am trying to loop through a JS script as below using php to get the contents for a drop down list but I cant seem to get the loop to work. Here's what I have been trying.. // Heres the JS for the drop down list. I cant have this static as it is here as there could be 10-20 results in the drop down list so I need to loop through my DB result and loop the below script also var menu1=new Array() menu1[0]='<a href="http://www.mysite.com/articles.php?article_id=<?php echo $gethistory[0][0]; ?>"><?php echo $gethistory[0][2]; ?></a>' menu1[1]='<a href="http://www.mysite.com/articles.php?article_id=<?php echo $gethistory[1][0]; ?>"><?php echo $gethistory[1][2]; ?></a>' menu1[2]='<a href="http://www.mysite.com/articles.php?article_id=<?php echo $gethistory[2][0]; ?>"><?php echo $gethistory[2][2]; ?></a>' ... Here's what I was trying.... 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; } // Get the results and loop through the js $gethistory = gethistory(); foreach($i=0; $i<count($gethistory ); i++) { echo "menu1[$i]='<a href="http://www.mysite.com/articles.php?article_id=".$gethistory[$i][0]."><".$gethistory[$i][2]."</a>"; } I think I am going wrong somewhere with this. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 29, 2007 Author Share Posted November 29, 2007 Can anyone help with this? :'( Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 29, 2007 Share Posted November 29, 2007 Sorry, but it's still not clear what you are trying to do. It seems that you have PHP embedded within your JavaScript which is in your database, and you are trying to retrieve it from your database to display it? You either need to dynamically generate the PHP within the JavaScript records, or you need to eval() the JavaScript strings as you pull them from your database in order to get the PHP within them to be parsed. I don't know if this helps, but based on your post, it seems like this is what you are needing. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 29, 2007 Author Share Posted November 29, 2007 Yeah I think thats it alright. So in theory I should be parsing the Javascript with php and using echo to output it as Javascript. This might work alright. Thanks, I'll give it a shot. Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted November 29, 2007 Author Share Posted November 29, 2007 Would this be parsed correctly? <?php echo "<script type=\"text/javascript\">\n"; echo "var pausecontent=new Array();\n"; foreach($i=0; $i<count($gethistory); $i++) { echo "menu1[".$i."]='<a href='http://www.mysite.com/articles.php?article_id=".$getnews[$i][0]."'>".$getnews[$i][2]."</a>'"; } echo "</script>\n"; ?> Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 29, 2007 Share Posted November 29, 2007 Would this be parsed correctly? Looks good at first glance. Try it out and see what you get 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.