PDA

View Full Version : Script: George's Expandable Ticker



gpigate
07-16-2007, 03:12 PM
http://www.dynamicdrive.com/dynamicindex2/expandticker2.htm

I am trying to modify the ticker that George provided to pull the ticker contents from a mysql database. I have done this before in other tickers but for some reason I just can't get this one to work. I am not the best with javascript so feel free to laugh.

top of the page

<?php
$ticker = mysql_query("SELECT * FROM table_ticker WHERE display =1 ORDER BY display_order");
if (mysql_num_rows($ticker)>0) {

then body and style... then script


<script language="JavaScript1.2">

/***********************************************
* George's Expandable Ticker- Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

//configure tickercontents[] to set the messges you wish be displayed (HTML codes accepted)

var tickercontents=new Array()
<?php
$i = 0;
while ($tick = mysql_fetch_array($ticker)) {
?>
tickercontents[<?php echo $i?>]='<?php echo $tick['content']?>'
<?php
$i++;
}
?>



This is how it is originally done in the ticker



var tickercontents=new Array()
tickercontents[0]='enter text here'
tickercontents[1]='more text here'


any help or ideas would be greatly appreciated.

djr33
07-16-2007, 03:37 PM
The output of that script is a bit vague to me, so I'd rewrite it a bit to make it clearer:

var tickercontents=new Array()
<?php
for ($i = 0; $tick = mysql_fetch_array($ticker); $i++) {
echo " tickercontents[$i]='".$tick['content']."'\n";
} ?>

gpigate
07-16-2007, 03:45 PM
are you saying to try this?




var tickercontents=new Array()
<?php
$i = 0;
while ($tick = mysql_fetch_array($ticker)) {
echo "tickercontents[$i]='".$tick['content']."'\n";
$i++;
}
?>


just tried it and it didnt work either. i moved the php code out of the javascript block to make sure it was returning values and it does return what is to be displayed. appears for some reason I can not get the php and jscript to communicate.

djr33
07-16-2007, 04:13 PM
appears for some reason I can not get the php and jscript to communicate.
There is no communication. PHP is server side and parsed when the page is requested. The text output after that no longer has any PHP involved. It is simply plain text, with a mix of html, javascript, css, etc.
PHP can output javascript just as it outputs html, but you must do so correctly.

To help fix the problem, please post what it is outputting.

A while loop and a for loop are the same (if the while loop has the third for loop parameter in it and the first before it), but I thought it was more readable to put them in the same line. That's your choice.

The only real guess I have at this point is that $tick['content'] isn't set correctly... that the database isn't outputting it, or something along those lines.

gpigate
07-16-2007, 04:32 PM
sorry about that I did not notice your use of the for loop instead of the while.

the output here isnt really bothering me. should the below not work. all that is in the db table is some html tags for images and other rows with text. to make things simple, i removed the db from the picture and I am simply trying with variables and text.



<script language="JavaScript1.2">
var tickercontents=new Array()

<?php
$i = 'abcd';
$j = 'efgh';

?>

tickercontents[0]='<?php echo $i;?>'
tickercontents[1]='<?php echo $j;?>'
............
</script>

gpigate
07-16-2007, 04:39 PM
i also moved the php block out of the script block and it was able to load the sql, the contents from the table and echo them to the page.

gpigate
07-16-2007, 05:33 PM
ok I finally got it to work. not sure what i did different. code is this


var tickercontents=new Array()
<?php
for ($i=0;$tick = mysql_fetch_array($ticker);$i++)
{
?>
tickercontents[<?php echo $i;?>]='<?php echo $tick['content']?>'
<?php } ?>

now the thing is, I am using TinyMCE to manage the back end and create content in the table this reads form.

If I update the row in the table using TinyMCE it causes the ticker to stop working. even though in the database there are no differences. It is not adding tags or anything, just straight text "Hello World".

if i insert hello world via the db it shows

if I insert hello world via the admin panel using TinyMCE it quits working. Really odd, does anyone have any ideas?

djr33
07-16-2007, 07:39 PM
I'm glad the code is working now.
This may have something to do with not ending your lines with semicolons, though I'm not sure why, then, the new script does work.

gpigate
07-17-2007, 01:36 PM
well the lines without the semicolons are in the script block and not the php block.

but hey its working, so im leaving it alone. :)

the only other thing that is bugging me is I would like to have 2 of them, and if I put in another script it works but the first one doesnt. does this have anything to do with double declaration in jscript?