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

Link to comment
Share on other sites

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

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.