SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2000
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    loading mysql data into javascript array

    i have build litte site(still under construction) for my department, one of this aspects is to provide newest information about new books in our library(new entry) so i use javascript code from dynamicdrive which is scroling down,
    here the code:
    <script language="JavaScript1.2">
    //configure the below five variables to change the style of the scroller
    var scrollerwidth=170
    var scrollerheight=200
    var scrollerbgcolor='#006699'
    var scrollerbackground=''

    //configure the below variable to change the contents of the scroller
    var messages=new Array()
    messages[0]="Todays Bookbr>"

    messages[1]="BOOK TITLEAUTHOR)<br><br>DESCRIPTION_OF_BOOK</font><br><br>PUBLISHER<hr width=90% color=white><br>"

    messages[2]="
    awal = "<font face='Arial' size='2'><b>";
    akhir = "</b></font>";

    if (messages.length>1)
    i=2
    else
    i=0

    function move1(whichlayer){
    tlayer=eval(whichlayer)
    if (tlayer.top>0&&tlayer.top<=5){
    tlayer.top=0
    setTimeout("move1(tlayer)",5000)
    setTimeout("move2(document.main.document.second)",5000)
    return
    }
    if (tlayer.top>=tlayer.document.height*-1){
    tlayer.top-=5
    setTimeout("move1(tlayer)",100)
    }
    else{
    tlayer.top=scrollerheight
    tlayer.document.write(awal+messages[i]+akhir)
    tlayer.document.close()
    if (i==messages.length-1)
    i=0
    else
    i++
    }
    }

    function move2(whichlayer){
    tlayer2=eval(whichlayer)
    if (tlayer2.top>0&&tlayer2.top<=5){
    tlayer2.top=0
    setTimeout("move2(tlayer2)",5000)
    setTimeout("move1(document.main.document.first)",5000)
    return
    }
    if (tlayer2.top>=tlayer2.document.height*-1){
    tlayer2.top-=5
    setTimeout("move2(tlayer2)",100)
    }
    else{
    tlayer2.top=scrollerheight
    tlayer2.document.write(awal+messages[i]+akhir)
    tlayer2.document.close()
    if (i==messages.length-1)
    i=0
    else
    i++
    }
    }
    function move3(whichdiv){
    tdiv=eval(whichdiv)
    if (tdiv.style.pixelTop>0&&tdiv.style.pixelTop<=5){
    tdiv.style.pixelTop=0
    setTimeout("move3(tdiv)",5000)
    setTimeout("move4(second2)",5000)
    return
    }
    if (tdiv.style.pixelTop>=tdiv.offsetHeight*-1){
    tdiv.style.pixelTop-=5
    setTimeout("move3(tdiv)",100)
    }
    else{
    tdiv.style.pixelTop=scrollerheight
    tdiv.innerHTML=awal+messages[i]+akhir
    if (i==messages.length-1)
    i=0
    else
    i++
    }
    }

    function move4(whichdiv){
    tdiv2=eval(whichdiv)
    if (tdiv2.style.pixelTop>0&&tdiv2.style.pixelTop<=5){
    tdiv2.style.pixelTop=0
    setTimeout("move4(tdiv2)",5000)
    setTimeout("move3(first2)",5000)
    return
    }
    if (tdiv2.style.pixelTop>=tdiv2.offsetHeight*-1){
    tdiv2.style.pixelTop-=5
    setTimeout("move4(second2)",100)
    }
    else{
    tdiv2.style.pixelTop=scrollerheight
    tdiv2.innerHTML=awal+messages[i]+akhir
    if (i==messages.length-1)
    i=0
    else
    i++
    }
    }

    function startscroll(){
    if (document.all){
    move3(first2)
    second2.style.top=scrollerheight
    second2.style.visibility='visible'
    }
    else if (document.layers){
    document.main.visibility='show'
    move1(document.main.document.first)
    document.main.document.second.top=scrollerheight+5
    document.main.document.second.visibility='show'
    }
    }

    window.onload=startscroll
    </script>
    <ilayer id="main" width=&{scrollerwidth}; height=&{scrollerheight}; bgColor=&{scrollerbgcolor}; background=&{scrollerbackground}; visibility=hide>
    <layer id="first" left=3 top=1 width=&{scrollerwidth};>
    <script language="JavaScript1.2">
    if (document.layers)
    document.write(awal+messages[0]+akhir)
    </script>
    </layer>
    <layer id="second" left=3 top=0 width=&{scrollerwidth}; visibility=hide>
    <script language="JavaScript1.2">
    if (document.layers)
    document.write(awal+messages[1]+akhir)
    </script>
    </layer>
    </ilayer>

    <script language="JavaScript1.2">
    if (document.all){
    document.writeln('<span id="main2" style="position:relative;width:'+scrollerwidth+';height:'+scrollerheight+';overflow:hiden;background-color:'+scrollerbgcolor+' ;background-image:url('+scrollerbackground+')">')
    document.writeln('<div style="position:absolute;width:'+scrollerwidth+';height:'+scrollerheight+';clip:rect(0 '+scrollerwidth+' '+scrollerheight+' 0);left:5;top:0">')
    document.writeln('<div id="first2" style="position:absolute;width:'+scrollerwidth+';left:0;top:1;">')
    document.write(awal+messages[0]+akhir)
    document.writeln('</div>')
    document.writeln('<div id="second2" style="position:absolute;width:'+scrollerwidth+';left:0;top:0;visibility:hidden">')
    document.write(awal+messages[1]+akhir)
    document.writeln('</div>')
    document.writeln('</div>')
    document.writeln('</span>')
    }
    </script>


    MY QUESTION ARE:
    messages[1]="BOOK TITLEAUTHOR)<br><br>DESCRIPTION_OF_BOOK</font><br><br>PUBLISHER<hr width=90% color=white><br>"

    i get BOOK TITLE, AUTHOR, DESCRIPTION_OF_BOOK AND PUBLISHER are tahen from mysql database, i use while loop to parse mysql data into this array, but the problem show up when new entry come( i use limit 0,5) for newest book entry in this scroller message.

    messages[1] change into messages [id number fromdatabase], it's ok add this manual tobe
    messages[1]......;
    messages[2].....;
    and so on
    but if i quite bussy i cant handle it manually, is there any solution to make it automatically retrieve data from mysql database and special for 'messages[]' is automatically insert from 0 to number of entry.

    plz help me
    thank you for your help

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had a little trouble defining what it is you are looking for. I think this is it.

    PHP Code:
    <?

    $result 
    mysql_query("......");
    ?>
    var messages=new Array(<?=mysql_num_rows($result)?>) ;
    <?
    $i 
    0;
    while(
    $row mysql_fetch_array($result)) {
        
    extract($row);
        
    ?>
        messages[<?=$i?>] = <?=$contentfromdb?>
        <?
        $i
    ++;
        }
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Member
    Join Date
    Oct 2000
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    it works

    tahnks for reply freddie, but i have another question to ask. Last night i got the idea and it's same as yours:

    <?
    include("dbik.php");
    $conn = mysql_connect(localhost,$user,$pass);
    $select =@mysql_select_db("$db")or die ("unable to select db");
    $result=mysql_query("select * "."from pesan ". "order by id desc limit 20 ");

    $jumlah=mysql_query("select id from pesan");
    $total=mysql_num_rows($jumlah);


    $i=0;
    while ($data=mysql_fetch_array($result))
    {

    $pesan=ucfirst($data[pesan]);
    //$kataku=ereg_replace("[ \n\r\t]+",",",trim($kataku));
    ?>

    messages[<? echo$i;?>]="Untuk <? echo ucfirst($data[untuk]);?><br><br><? echo trim($pesan);?></font><br><br>Dari :&nbsp;<a href='mailto? echo$data[email];?>'><? echo ucfirst($data[nama]);?><hr width=95% color=brown><br>"
    <?
    $i++;

    }
    ?>

    i'm then expand it to give my visitor leaving their message then it scroll.
    but i found problem with user input when they use double qutes in their and if they hit "enter" button:
    i use ereg to escape double qutes in:
    $kata=ereg_replace(char(34),$message);
    but nothing change, it still double qutes in there.
    and i use :
    $kataku=ereg_replace("[ \n\r\t]+",",",trim($kataku));
    to escape any newline and others , cause this message need in one line to get scroll. but nothing change too, any suggestion???


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
  •