wmguk Posted October 13, 2009 Share Posted October 13, 2009 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 More sharing options...
pastcow Posted October 13, 2009 Share Posted October 13, 2009 As far as i am aware you can either generate the Javascript file using PHP (and so put dynamic content straight into your javascript file) or you can use AJAX to dynamically request new content from a PHP script. Link to comment https://forums.phpfreaks.com/topic/177526-adding-php-to-javascript/#findComment-936006 Share on other sites More sharing options...
wmguk Posted October 13, 2009 Author Share Posted October 13, 2009 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? Link to comment https://forums.phpfreaks.com/topic/177526-adding-php-to-javascript/#findComment-936011 Share on other sites More sharing options...
Alt_F4 Posted October 13, 2009 Share Posted October 13, 2009 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. Link to comment https://forums.phpfreaks.com/topic/177526-adding-php-to-javascript/#findComment-936061 Share on other sites More sharing options...
wmguk Posted October 13, 2009 Author Share Posted October 13, 2009 Hi, I've tried this but I still get undefined... also, I'm guessing i can set pausecontent[ ] to increment +1 each time Link to comment https://forums.phpfreaks.com/topic/177526-adding-php-to-javascript/#findComment-936065 Share on other sites More sharing options...
Alt_F4 Posted October 13, 2009 Share Posted October 13, 2009 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) Link to comment https://forums.phpfreaks.com/topic/177526-adding-php-to-javascript/#findComment-936068 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.