1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Horizontal Scrolling Text From Database

Discussion in 'Programming' started by tlshaheen, Jan 11, 2010.

  1. #1
    Hi guys,
    I've been searching around a bit and can't seem to find a script to display text from a database and scroll it across the page, like a news ticker. I'd like to it display horizontally across the page. I assume that it'll be a mixture of Javascript and PHP, and I've found some versions of it in my search, but nothing that seems to work very well. Any ideas?
    I'm not stuck on PHP or JS, I just need SOMETHING to take the info from my DB and scroll it across the page like a news ticker.

    Thanks!
     
    tlshaheen, Jan 11, 2010 IP
  2. tures

    tures Peon

    Messages:
    172
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yeah, this should be rather easy to accomplish using something like jQuery or Scriptaculous. Basically, grab your titles or headers from your database:

    PHP:

    <?

    $query = "SELECT title FROM news WHERE CONDITION";
    $resultSet = mysql_query($query);
    ?>
    <marquee id="newticker">
    while($row = mysql_fetch_array($result)){
    ?>
    <li class="newHeader"><?=$row['title']?></li>
    <?
    }
    </marquee>
    }

    Javascript (Need jQuery and Marquee plug in):

    $(document).ready(function(){
    $('#newticker').marquee();
    });

    Your li would obviously need some styling done to it to make it inline, get rid of the middots, etc, but this would be the basic idea.
     
    tures, Jan 11, 2010 IP
  3. tures

    tures Peon

    Messages:
    172
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Whoops. Messed up:

    Should be:

    while($row = mysql_fetch_array($resultSet))
     
    tures, Jan 11, 2010 IP
  4. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you for your quick response! I have jQuery, but which Marquee plug-in were you referring to?
     
    tlshaheen, Jan 11, 2010 IP
  5. tlshaheen

    tlshaheen Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    For those interested, after some more searching, I came up with the following. It uses jScroller2 for the scrolling effect (that way, I don't have to worry about the compatability of <marquee> plus I get more styling effects (speed mostly)).
    I've got the styling done in a css file. But this returns the list of Vendor names from the DB, and displays them in a random order (so each time someone visits the page, they don't see the same starting Vendors - there are currently over 240, so this way all vendors get displayed at some point).

    If anyone's got any suggestions for cleaning up any of the code, please let me know! Thanks!

    <div id="vendorTicker">
    <?php
    mysql_connect(//CONNECTION INFO) or die(mysql_error());
    mysql_select_db(//DATABASE NAME) or die(mysql_error());
    
    $countquery = "SELECT COUNT(Name) FROM Vendors";
    $resultSet = mysql_query($countquery) or die(mysql_error());
    $row =  mysql_fetch_array($resultSet);
    $totalcount =  $row['COUNT(Name)'];
    
    $query = "SELECT Name FROM Vendors ORDER BY RAND() LIMIT " .$totalcount;
    $resultSet = mysql_query($query);
    ?>
    <div class="jscroller2_left jscroller2_speed-100 jscroller2_mousemove">
    <?php
    while($row = mysql_fetch_array($resultSet)) {
    ?>
    <div id="vendorDisplay">
    <?php print($row['Name']) ?>[/PHP]
    </div>
    <?php
    }
    mysql_close();
    ?>
    </div>
    Code (markup):
     
    tlshaheen, Jan 14, 2010 IP