#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    910
    Rep Power
    0

    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
  2. #2
  3. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1100
    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>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    910
    Rep Power
    0
    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>
  6. #4
  7. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1100
    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    910
    Rep Power
    0
    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
  10. #6
  11. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1100
    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' ;
    }

    ?>";
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    910
    Rep Power
    0
    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
  14. #8
  15. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1100
    What line is line 78? And what are the lines above and below it?

    Probably missing a semi-colon (;) somewhere prior to line 78.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    910
    Rep Power
    0
    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
  18. #10
  19. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1100
    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?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    910
    Rep Power
    0
    PHP Code:
    newsText[0] = "Title = sing-a-long
    Artist = tim sharpe
    Price = $25
    "

    This is how it looks
  22. #12
  23. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1100
    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' 
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Posts
    910
    Rep Power
    0
    Thanks for your help that worked...

IMN logo majestic logo threadwatch logo seochat tools logo