March 9th, 2003, 11:16 AM
-
Javascript Ticker to Show Query
PHP Code:
var newsText = new Array();
newsText[0] = " <info from database>"
I have the above code, and I would like the ticker to display info displayed from a select inquiry on a database shown. Can someone please show me how I would setup up the select inquiry so that the info would be displayed in the ticker.
Thanks
Tim
March 9th, 2003, 11:24 AM
-
So you basically need to pass PHP information that is pulled from a database to JavaScript? It's done the same way you would put database output into HTML.
PHP Code:
<script language="javascript">
var newsText = new Array();
newsText[0] = <?php echo $newsText[0] ?>;
</script>
March 9th, 2003, 11:33 AM
-
Ok now how would I display the contents of an include file?
PHP Code:
<script language="javascript">
var newsText = new Array();
newsText[0] = <?php echo $newsText[0] ?>;
</script>
Would it be like
PHP Code:
<script language="javascript">
var newsText = new Array();
newsText[0] = <?php include "release1.php" ?>;
</script>
March 9th, 2003, 11:54 AM
-
Yes.
No matter where you are in HTML, once you break into <?php ?> PHP tags, PHP syntax remains the same and you can do anything you want there that you can normally do in PHP. You can echo, you can include, you could even open a db connection, run the query, and get the results.
March 9th, 2003, 12:13 PM
-
PHP Code:
<html>
<body>
<form name="news">
<textarea name="news2" cols=40 rows=4 wrap=virtual></textarea>
</form>
<script language=JavaScript>
var newsText = new Array();
newsText[0] = <?PHP
$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("stores",$db);
$sql = "SELECT title, artist, price from item";
$result = mysql_query($sql) or die("Couldn't execute query") ;
while ($title = mysql_fetch_array($result))
{
echo "Title = " . $title['title'] . "\n" ;
echo "Artist = " . $title['artist'] . "\n" ;
echo "Price = " . $title['price'] . "\n" ;
}
?>;
var ttloop = 1; // Repeat forever? (1 = True; 0 = False)
var tspeed = 50; // Typing speed in milliseconds (larger number = slower)
var tdelay = 1000; // Time delay between newsTexts in milliseconds
var dwAText, cnews=0, eline=0, cchar=0, mxText;
function doNews() {
mxText = newsText.length - 1;
dwAText = newsText[cnews];
setTimeout("addChar()",1000)
}
function addNews() {
cnews += 1;
if (cnews <= mxText) {
dwAText = newsText[cnews];
if (dwAText.length != 0) {
document.news.news2.value = "";
eline = 0;
setTimeout("addChar()",tspeed)
}
}
}
function addChar() {
if (eline!=1) {
if (cchar != dwAText.length) {
nmttxt = ""; for (var k=0; k<=cchar;k++) nmttxt += dwAText.charAt(k);
document.news.news2.value = nmttxt;
cchar += 1;
if (cchar != dwAText.length) document.news.news2.value += "_";
} else {
cchar = 0;
eline = 1;
}
if (mxText==cnews && eline!=0 && ttloop!=0) {
cnews = 0; setTimeout("addNews()",tdelay);
} else setTimeout("addChar()",tspeed);
} else {
setTimeout("addNews()",tdelay)
}
}
doNews()
</script>
</body>
</html>
I have the above code, and as far as I know I am adding the query correct, but the ticker is not working why?
Thanks
Tim
March 9th, 2003, 12:30 PM
-
Do you get any error messages? Probably a JavaScript error of some kind?
You need to work on your debugging skills. Let the PHP page run in the browser and then look at the resulting code. You'll notice that you're not providing JavaScript with a string value for that array element.
Also, passing new lines to JavaScript from PHP is a little strange. You actually need to pass the string \n, meaning \n is what you need to see in the echo()ed PHP result.
PHP Code:
<?php
// note the double quotes around your PHP tags
// and the single quotes around the new line ?>
var newsText = new Array();
newsText[0] = "<?PHP
$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("stores",$db);
$sql = "SELECT title, artist, price from item";
$result = mysql_query($sql) or die("Couldn't execute query") ;
while ($title = mysql_fetch_array($result))
{
echo "Title = " . $title['title'] . '\n' ;
echo "Artist = " . $title['artist'] . '\n' ;
echo "Price = " . $title['price'] . '\n' ;
}
?>";
March 9th, 2003, 12:41 PM
-
I am getting the following error, I am sorry but I do not know what this error is.
Parse error: parse error, unexpected $end in e:\inetpub\wwwroot\store\shopnewfoundland\ticker.php on line 78
March 9th, 2003, 01:02 PM
-
What line is line 78? And what are the lines above and below it?
Probably missing a semi-colon (;) somewhere prior to line 78.
March 9th, 2003, 01:14 PM
-
PHP Code:
<form name="news">
<textarea name="news2" cols=40 rows=4 wrap=virtual></textarea>
</form>
<script language=JavaScript>
var newsText = new Array();
newsText[0] = "var newsText = new Array();
newsText[0] = "<?PHP
$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("stores",$db);
$sql = "SELECT title, artist, price from item";
$result = mysql_query($sql) or die("Couldn't execute query") ;
while ($title = mysql_fetch_array($result))
{
echo "Title = " . $title['title'] . '\n' ;
echo "Artist = " . $title['artist'] . '\n' ;
echo "Price = " . $title['price'] . '\n' ;
}
?>";
newsText[1] = "";
newsText[2] = "";
newsText[3] = "";
newsText[4] = "";
var ttloop = 1; // Repeat forever? (1 = True; 0 = False)
var tspeed = 50; // Typing speed in milliseconds (larger number = slower)
var tdelay = 1000; // Time delay between newsTexts in milliseconds
// ------------- NO EDITING AFTER THIS LINE ------------- \\
var dwAText, cnews=0, eline=0, cchar=0, mxText;
function doNews() {
mxText = newsText.length - 1;
dwAText = newsText[cnews];
setTimeout("addChar()",1000)
}
function addNews() {
cnews += 1;
if (cnews <= mxText) {
dwAText = newsText[cnews];
if (dwAText.length != 0) {
document.news.news2.value = "";
eline = 0;
setTimeout("addChar()",tspeed)
}
}
}
function addChar() {
if (eline!=1) {
if (cchar != dwAText.length) {
nmttxt = ""; for (var k=0; k<=cchar;k++) nmttxt += dwAText.charAt(k);
document.news.news2.value = nmttxt;
cchar += 1;
if (cchar != dwAText.length) document.news.news2.value += "_";
} else {
cchar = 0;
eline = 1;
}
if (mxText==cnews && eline!=0 && ttloop!=0) {
cnews = 0; setTimeout("addNews()",tdelay);
} else setTimeout("addChar()",tspeed);
} else {
setTimeout("addNews()",tdelay)
}
}
doNews()
</script>
Ok the error message is gone, but nothing is showing the textbox, what is any possible reasons?
Thanks again
Tim
March 9th, 2003, 01:25 PM
-
newsText[0] is being defined twice. Take out the first definition, which is probably just a cut/paste typo?
What does the newsText[0] = line look like after the PHP has parsed?
March 9th, 2003, 01:35 PM
-
PHP Code:
newsText[0] = "Title = sing-a-long
Artist = tim sharpe
Price = $25
";
This is how it looks
March 9th, 2003, 01:46 PM
-
Okay, needs to look like
Code:
newsText[0] = "Title = sing-a-long\nArtist = tim sharpe\nPrice = $25";
PHP Code:
echo "Title = " . $title['title'] . '\n' ;
echo "Artist = " . $title['artist'] . '\n' ;
echo "Price = " . $title['price'] . '\n' ;
// change to
echo "Title = " . $title['title'] . '\\\n' ;
echo "Artist = " . $title['artist'] . '\\\n' ;
echo "Price = " . $title['price'] . '\\\n' ;
March 9th, 2003, 02:12 PM
-
Thanks for your help that worked...