Page 1 of 1

Using scrooler to display data?

Posted: Tue Mar 16, 2004 9:15 am
by tiggertim
I got the following code from another website, how can I alter it to read data from mysql using php. What I need altered in the message number will come from the $id field and the url be $partner_course.php and link title be $course_title.

Just like:

msg[$id]="<A HREF='$url'>$course_title</A>"

But its not working, please help.

Thanks
Tim

Code: Select all

<html>
<head>
<SCRIPT LANGUAGE="javascript">
/* ********************************************************************************** 
    * This code was created by Jim Young of http://www.requestcode.com .                *
    * You may use this code at no charge.  All I ask is that you give me             *
    * credit.  Please visit my site for more scripts like this one.                          *
    **********************************************************************************
    Enter your links or text in the array below that you want to scroll.  Mak sure that you use single quotes within 
    the double quotes 
*/
var msg=new Array()
msg[0]="<FONT FACE='Arial'><B><I><A HREF='http://www.requestcode.com'>Visit Request Code for more great scripts like this one!</A><I></B></FONT>"

/* The variables below are the ones you want to change  */

speed=150                      // Sets the speed in milliseconds at which the link or text scrolls.
var divwidth=600            // Sets the width of the div or layer in pixels.  You will have to play with this to get it set correctly.
var divheight=20            // Sets the height of the div or layer in pixels.  Same as above.

/*
   The variables divincbt, divinctb, divincrl and divinclr set the increment in pixels that the div that contains the text
    or links is moved.  Which one is used is determined by the direction that you pick in the variable whichscroll.
    The variable divinctb would be used if you set whichscroll to 1, divinctb if you set whichscroll to 2, divincrl if
     you set whichscroll to 3 and divinclr if you set whichscroll to 4.  You can change these variables to speed up
     or slow down the movement of the scroll.
*/

var divincbt=1                 
var divinctb=-1
var divincrl=10
var divinclr=-10

var divpause=3000        // Sets how long to pause in milliseconds before scrolling the next link or text. 

var divtop="50"              // divtop and divleft set the top and left positions of the container div or layer in pixels. 
var divleft="10"               

var divcolor="white"      // Sets the background color of the div or layer

var divname1="scroll2"  // Sets the name of your div or layer for the first link or text that is displayed
var divname2="scroll3"  // Sets the name of your div or layer for the next link or text to be displayed

/*
   The variable whichscroll is used to set the direction of the scrolling.  It is currently set to 1.
    1 = Bottom to Top
    2 = Top to Bottom
    3 = Right to Left
    4 = Left to Right
*/
var whichscroll="1"

/* END OF CONFIGURATION VARIABLES */

/* THERE IS NO NEED TO CHANGE THE VARIABLES OR SCRIPTS BELOW */

var divinc
var stopposa
var stopposb
var scrollmsga
var scrollmsgb 
var thediva
var thedivb
var posa
var posb
var i=msg.length
var firsttime="y"
var divprop
var whichdiv=divname1
if(whichscroll=="1")
  {
   stopposa=0
   stopposb=divheight*-1
   divprop="top"
   divinc=divincbt
  }
if(whichscroll=="2")
  {
   stopposa=0
   stopposb=divheight
   divprop="top"
   divinc=divinctb
  }
if(whichscroll=="3")
  {
   stopposa=0
   stopposb=divwidth*-1
   divprop="left"
   divinc=divincrl
  }
if(whichscroll=="4")
  {
   stopposa=1
   stopposb=divwidth+2
   divprop="left"
   divinc=divinclr
  }

function Scrollboxa(obja)
     {
         savobja=obja
         posa=posa-divinc
         if(((whichscroll=="1" || whichscroll=="3")&&(posa<=stopposa))||
           ((whichscroll=="2" || whichscroll=="4")&&(posa>=stopposa)))
           {
            clearTimeout(scrollmsga)
            setTimeout("scrollit()",divpause)
           } 
         else
             {
              if(document.getElementById) // IE5 and NS6
                {
                 eval("elma.style."+divprop+"=posa")
                 elma.style.visibility="visible"
                }
              if(document.layers)    // NS4+
                {
                 eval("obja."+divprop+"=posa")
                 document.main.visibility="show"
                }
              scrollmsga=setTimeout("Scrollboxa(savobja)",speed)
             }
     }
function Scrollboxb(objb)
     {
         savobjb=objb
         posb=posb-divinc
         if(((whichscroll=="1" || whichscroll=="3")&&(posb<=stopposb))||
            ((whichscroll=="2" || whichscroll=="4")&&(posb>=stopposb)))
           {
            clearTimeout(scrollmsgb)
           }
         else
              {
               if(document.getElementById) // IE5 and NS6
                 {
                  eval("elmb.style."+divprop+"=posb")
                 }
               if(document.layers)  // NS4+
                 {
                  eval("objb."+divprop+"=posb")
                  objb.visibility="show"
                 }
               scrollmsgb=setTimeout("Scrollboxb(savobjb)",speed)
              }
     }
function scrollit()
   {
    i++
    if(i>=msg.length)
      {i=0}
    posa=stopposb*-1
    posb=0
    if(firsttime=="y")
      {
       firsttime= "n"
       if(document.getElementById)      // IE5 and NS6
         {
          elma=document.getElementById(divname1)
          elma.innerHTML=msg[i]
         }
      if(document.layers)   // NS4+
        {
         thediva=eval("document.main.document."+divname1)
         thediva.document.write(msg[i])
         thediva.document.close()
        }
       Scrollboxa(thediva)
      }
    else
      {
       if(whichdiv==divname1)
         {
          if(document.getElementById)  // IE5 and NS6
            {
             elmb=document.getElementById(divname1)
             elma=document.getElementById(divname2)
             elma.innerHTML=msg[i]
            }
          if(document.layers)  //  NS4+
            {
             thedivb=eval("document.main.document."+divname1)
             thediva=eval("document.main.document."+divname2)  
             thediva.document.write(msg[i])
             thediva.document.close()
            }
          Scrollboxb(thedivb)
          whichdiv=divname2
          Scrollboxa(thediva)
         }
       else
         {
          if(document.getElementById)   // IE5 and NS6
            {
             elmb=document.getElementById(divname2)
             elma=document.getElementById(divname1)
             elma.innerHTML=msg[i]
             }
          if(document.layers)  // NS4+
            {
             thedivb=eval("document.main.document."+divname2)
             thediva=eval("document.main.document."+divname1)
             thediva.document.write(msg[i])
             thediva.document.close()
             }
          Scrollboxb(thedivb)
          whichdiv=divname1
          Scrollboxa(thediva)
         }
      }
  }
</SCRIPT>
<STYLE>
     A{text-decoration:none}
     a:link {color:blue}
     a:visited{color:00CC00}
     a:active{color:blue}
     a:hover{color:red}
</STYLE>
</head>
<body onLoad="scrollit()">
<CENTER><FONT FACE="Arial"><B> Scroll Along</B></FONT></CENTER>
<SCRIPT LANGUAGE="JavaScript">
 /* You should no have to change the scripts below */
if(document.getElementById)  // IE5 and NS6
  {
   document.write('<DIV STYLE="position:relative;top:'+divtop+';left:'+divleft+';width:'+divwidth+';height:'+divheight+';visibility:visible;background-color:'+divcolor+';overflow:hidden;clip:rect(0,'+divwidth+','+divheight+',0)">')
   document.write('<DIV STYLE="position:relative;top:0;left:0;width:'+divwidth+';height:'+divheight+'">')
   document.write('<DIV ID='+divname1+' STYLE="position:absolute;top:0;left:0;width:'+divwidth+';height:'+divheight+'"></DIV>')
   document.write('<DIV ID='+divname2+' STYLE="position:absolute;top:0;left:0;width:'+divwidth+';height:'+divheight+';visibility:hidden"></DIV>')
   document.write('</DIV></DIV>')
  }
if(document.layers)  // NS4+
  {
   document.write('<ILAYER ID="main" TOP='+divtop+' LEFT='+divleft+' WIDTH='+divwidth+' HEIGHT='+divheight+' BGCOLOR='+divcolor+' VISIBILITY=hide>')
   document.write('<LAYER ID='+divname1+' LEFT=0 TOP=0 WIDTH='+divwidth+'></LAYER>')
   document.write('<LAYER ID='+divname2+' LEFT=0 TOP='+divheight+' WIDTH='+divwidth+'>')
   document.write('</LAYER></ILAYER>')
  }
</SCRIPT>
</body>
</html>

Posted: Tue Mar 16, 2004 9:25 am
by Goowe
I'm not sure what the javascript has to do with it, but judging off those first few lines of your post I'll give the best reply I can :wink:

Code: Select all

<?php

# mySQL Connection Information
$config['mysql host'] = 'localhost'; // mySQL hostname
$config['mysql username'] = ''; // mySQL username
$config['mysql password'] = ''; // mySQL password
$config['mysql database'] = ''; // mySQL database to be used

$link = mysql_connect($config['mysql host'], $config['mysql username'], $config['mysql password'])
	or die("<P>Could not connect: ".mysql_error());
mysql_select_db($config['mysql database']) or die("<P>Could not select database</P>");

// Assuming the table where the information is stored is called url_information
// and you want to order the list by the title of the course ascending, you can
// use this query.
$query = "SELECT * FROM url_information ORDER BY course_title ASC";
$result = mysql_query($query) or die("<P>Query failed: ".mysql_error());
while($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
	// Assuming that the column that holds the id number for each "course" is
	// entitled 'id' and
	$msg[$line['id']] = "<A HREF='".$line['partner_course']."'>".$line['course_title']."</A><BR>\n";
}

mysql_close($link);

// You can even do a foreach statement to echo everything above
// if you're (since you're) not wanted to do it during the
// while() statement
foreach($msg as $k => $v)
{echo $v;}
?>
Looking back in my stupidity, I see your main goal was the scroller? Sorry if I totally missed your question and made a fool of myself :roll:

Posted: Tue Mar 16, 2004 9:29 am
by tiggertim
ok how do I insert that into the javascript?

Tim

Posted: Tue Mar 16, 2004 9:34 am
by TheBentinel.com
tiggertim wrote:ok how do I insert that into the javascript?

Tim
This site uses a scroller which you could certainly steal:

http://www.uwayfairfieldco.org

Posted: Tue Mar 16, 2004 10:08 am
by tiggertim
ok the first post that I made was the scroller script, now how do I insert your script into that?

Posted: Tue Mar 16, 2004 10:20 am
by TheBentinel.com
tiggertim wrote:ok the first post that I made was the scroller script, now how do I insert your script into that?
Geez, I'm so sorry. Talk about being a dufus. I thought you were looking for a scroller. Maybe I should start reading the posts before I reply to them, ya think?

The script says:

Code: Select all

/*     Enter your links or text in the array below that you want to scroll.  Mak sure that you use single quotes within 
    the double quotes 
*/ 
var msg=new Array() 
msg[0]="<FONT FACE='Arial'><B><I><A HREF='http://www.requestcode.com'>Visit Request Code for more great scripts like this one!</A><I></B></FONT>"
After the var msg=... put the PHP code to connect to your db and create the array elements for your stuff to scroll:

Code: Select all

var msg=new Array() 
<?php
  // CONNECT TO DB
  // RETRIEVE ResultSet
  // Flip through your results
  var i = -1;
  foreach ($resultset as $result)
  {
    var = var + 1;
    print ("msg[" . i . '] = "');
    print ("THE TEXT YOU WANT TO SCROLL, PULLED FROM YOUR DB");
    print ('"\n');
  }
?>
/* The variables below are the ones you want to change  */ 
speed=150                      // Sets the speed in milliseconds at which the link or text scrolls.
So you're using PHP to write javascript.