Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    po3
    po3 is offline
    New Coder
    Join Date
    Apr 2007
    Posts
    75
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Arrow correct syntax for putting a PHP variable in the middle of a javascript string

    I'm trying to implement this Script. 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:
    PHP Code:
    myScroller1.addItem("<b><?php echo $sold_items1 ?></b>");
    myScroller1.addItem("<b><?php echo $sold_items2 ?></b>");
    I also tried the following variations.
    PHP Code:
    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:
    Code:
    <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.
    Last edited by po3; 12-28-2007 at 02:41 PM.

  • #2
    Regular Coder Actinia's Avatar
    Join Date
    Feb 2007
    Location
    Essex, UK
    Posts
    250
    Thanks
    1
    Thanked 23 Times in 22 Posts
    In your line
    Code:
    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:
    Code:
    myScroller1.addItem("<b>" + <?php echo $sold_items1 ?> + "</b>");
    Or possibly:
    Code:
    myScroller1.addItem("<b>" + "<?php echo $sold_items1 ?>" +  "</b>");
    Could you not write the Javascript from the php?

    John

  • Users who have thanked Actinia for this post:

    po3 (12-28-2007)

  • #3
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    or even…
    Code:
    myScroller1.addItem("<b><?php echo $sold_items1 ?></b>");

  • #4
    po3
    po3 is offline
    New Coder
    Join Date
    Apr 2007
    Posts
    75
    Thanks
    16
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •