SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript, php news scroller

    Hi,

    I'm looking for a way to build a vertical news scroller which will scroll an array of news items. I have found many examples of this using just javascript but I need to use php in order to retrieve the news from the database.

    Does anyone know of a solution or willing to point me in the right direction?

    Thanks,

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Hi Matt
    you can use php to generate the javascript code that gives the arrays.

    Consider the following:
    PHP Code:
    <script> 
    function correct(){ 
        if (finished){ 
            setTimeout("begin()",3000) 
        } 
        return true 


    window.onerror=correct 
        function begin(){ 
            if (!document.all) 
            return 
            if (maxheight==null) 
            maxheight=temp.offsetHeight 
            whatsnew.style.height=maxheight 
            temp.style.display="none" 
            c=1 
            finished=true 
            change() 
            } //--> 
    </script> 
    <style> 
    #whatsnew a{color:730073;text-decoration:none} 
    #whatsnew, 
    #temp {font-weight:bold; font-family:Arial} 
    #whatsnew a:hover{color:red} 
    </style>
    <body onLoad="begin()">
    <?php 
    $sql 
    mysql_query("select * from news");

    echo 
    "
    <script language=\"javascript1.2\"> 
        var head='' 
        var top=0 
        var maxheight 
        var finished=false 
        var tickers=new Array() "
    ;
        
    $i=0;
        while(
    $rows mysql_fetch_assoc($sql)) {
            echo 
    'tickers['$i .'] = '$rows['headline'];
            
    $i++;
        }
    echo 
    "    
        for (i=1;i<=tickers.length-1;i++){ 
            if (tickers[i].length>=tickers[top].length) 
            top=i 
        } 
            if (document.all) document.write('<span id=\"whatsnew\" style=\"width:100%;height=10;filter:revealTrans(duration=2,transition=19)\">'+tickers[0]+'</span>') 

        function change(){ 
            whatsnew.innerHTML='' 
            whatsnew.filters.revealTrans.apply() 
            whatsnew.filters.revealTrans.play() 
            whatsnew.innerHTML=tickers[c] 
            if (c==tickers.length-1) 
                c=0;
                else 
                c+=1;
            setTimeout(\"change()\",5000) 
        } 
        
        if (document.all) document.write('<span id=ns style=\"display:none;\">') 
        </script>
        "
    ?>
    <span id=temp style="position:absolute;left:0;top:0"> <script> 
    if (document.all){ temp.style.width=whatsnew.style.width document.write(tickers[top]) } 
    </script> 
    </span>
    </body>
    This is based on a news ticker from hypergurl.com (but fixed )
    it originally had a javascript array:
    Code:
    	tickers[0]='<a href=\"easyhtml.html\">Get more great javascripts here!!</a>' 
    	tickers[1]='<a href=\"tutorials.html\">Paint shop pro tutorials</a>' 
    	tickers[2]='<a href=\"about.html\">If you have problems give us an email.</a>' 
    	tickers[3]='<a href=\"index.html\">Yes we will answer!</a>' 
    	tickers[4]='<a href=\"hosting.html\">This is message 4.</a>'
    but by using php to echo the javascript, you can substitute it for the while loop and display the database records.

    NOTE: ONLY WORK IN IE AS IT USES MS FILTERS - MEANT AS EXAMPLE ONLY!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

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