Jump to content

Loop through JS using php?


Solarpitch

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/79397-loop-through-js-using-php/
Share on other sites

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.

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";
?>

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.