Jump to content

Adding PHP to javascript


wmguk

Recommended Posts

Hey,

 

I have a news scroller, and I would like the content taken from a database...

 

Currently the content is set using a js page containing:

 

var pausecontent=new Array()

pausecontent[0]='<a href="http://www.javascriptkit.com">JavaScript Kit</a><br />Comprehensive JavaScript tutorials and over 400+ free scripts!'

pausecontent[1]='<a href="http://www.codingforums.com">Coding Forums</a><br />Web coding and development forums.'

pausecontent[2]='<a href="http://www.cssdrive.com" target="_new">CSS Drive</a><br />Categorized CSS gallery and examples.'

 

Is there a way to replace this javascript with the content from a database?

Link to comment
https://forums.phpfreaks.com/topic/177526-adding-php-to-javascript/
Share on other sites

ok, I tried:

 

<?php
include ("http://www.wicked-websites.co.uk/admin/scripts/connection.php");
$qry1 = "SELECT * FROM news";
$result1 = mysql_query($qry1);
?>
<script type="text/javascript">
/*Example message arrays for the two demo scrollers*/
var pausecontent=new Array()
<?php
while($news = mysql_fetch_array($result1)) 
{ ?>
pausecontent[0]='<a href="news.php#<?php echo $news['id']; ?>"><?php echo $news['news']; ?></a><br /><?php echo $news['news']; ?>!'
<?php }
?></script>

 

but I just see undefined... any thoughts?

try this and see how you go.

 

<?php
include ("http://www.wicked-websites.co.uk/admin/scripts/connection.php");
$qry1 = "SELECT * FROM news";
$result1 = mysql_query($qry1);
?>
<script type="text/javascript">
/*Example message arrays for the two demo scrollers*/
var pausecontent=new Array()
<?php
while($news = mysql_fetch_array($result1)) 
{ 
    echo 'pausecontent[0]="<a href=\"news.php#'.$news['id'].'\">'.$news['news'].'</a><br />'.$news['news'].'!";';
}
?></script>

 

of course you do realise that this will just replace the value of pausecontent[0] each iteration of the loop.

 

ok, i just ran this exact piece of code and i get a value.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Schedule</title>
<script type="text/javascript">
/*Example message arrays for the two demo scrollers*/
var pausecontent=new Array()
<?php
$news['id'] = 12;
$news['news'] = 'test';

while($i<4) 
{ 
    echo 'pausecontent[0]="<a href=\"news.php#'.$news['id'].'\">'.$news['news'].'</a><br />'.$news['news'].'!";';
$i++;
}
?>

alert(pausecontent[0]);
</script>

</head>

<body>
</body>
</html>

 

i would check that you are getting values back from the database (if you havent already)

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.