View Full Version : correct syntax for putting a PHP variable in the middle of a javascript string

12-28-2007, 02:38 PM
I'm trying to implement this Script (http://www.dynamicdrive.com/dynamicindex2/mikescroll.htm). However, I need to be able to use php variables as the text string that this script accepts. Is that possible? If so what is the correct syntax?

For example:

myScroller1.addItem("<b><?php echo $sold_items1 ?></b>");
myScroller1.addItem("<b><?php echo $sold_items2 ?></b>");

I also tried the following variations.

myScroller1.addItem("<b>" . <?php echo $sold_items1 ?> . "</b>");
myScroller1.addItem <?php echo $sold_items1 ?>;
myScroller1.addItem <?php echo "(\"" . $sold_items1 . "\");" ?>

The php variable holds a string similar to this:

<a href="rs_o1.php">Link:</a>Item : $00.00

If this isn't possible with this script, Do you have any suggestions? Basically, I'm pulling 3 random rows from my DB and then I want to display them in some kind of scrolling element on the page.

Thanks in advance.

12-28-2007, 03:01 PM
In your line
myScroller1.addItem("<b>" . <?php echo $sold_items1 ?> . "</b>"); you are mixing php and javascript string concatenation. The php segment will simply echo the string into the html/javascipt code. Try:

myScroller1.addItem("<b>" + <?php echo $sold_items1 ?> + "</b>"); Or possibly:
myScroller1.addItem("<b>" + "<?php echo $sold_items1 ?>" + "</b>");

Could you not write the Javascript from the php?


Bill Posters
12-28-2007, 03:04 PM
or even…

myScroller1.addItem("<b><?php echo $sold_items1 ?></b>");

12-28-2007, 03:44 PM
Thanks I wasn't sure how javascript concatenation worked. I also found a php issue that was screwing with me but I do have it working now. I went with your last solution.