Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Loading multiple sets of data from mysql using javascript

    Hi,
    I'm struggling to write a javascript which will run a php file and retrieve the results. The php file will product the results in xml format and the javascript needs to retrieve them and then show them on the page.

    Basically I want to retrieve data from a database, the script needs to run the php file every 15 seconds and automatically update the current page. The data needs to be taken from the xml and update elements on the current page. The problem I am having is I need to update multiple php variables which are all called the same things.

    Below is a example of the code on the first page, it first retrieves data from the database and turns it into a number of progress bars and shows however many progress bars are needed depending on other variables before on the page.


    Code:
    while($rows=mysql_fetch_array($result)){
    $id=$rows['id'];
    $prog1=$rows['prog1'];
    $prog2=$rows['prog2'];
    $name1=$rows['name1'];
    $name2=$rows['name2'];
    $progress1=($prog1/20)*100;
    $progress2=($prog2/20)*100;
    ?>
    
    <!---------- progress bars ----------->
    <div id="<?php echo $id."left"; ?>" 
    <div id="progress" style="width:<?php echo $progress1; ?>%"><?php echo $name1; ?></div>
    </div>
    
    <div id="<?php echo $id."right"; ?>" 
    <div id="progress" style="width:<?php echo $progress2; ?>%"><?php echo $name2; ?></div>
    </div>
    <!------------------------------------>
    
    <?php
    }
    I would then like these progress bars to be updated every 15 seconds, but I have no idea how to do this, would anybody be able to help please.

    P.s the php file that will be run every 15 seconds to produce the xml, I guess will look something like this:

    Code:
    while($rows=mysql_fetch_array($result)){
    $id=$rows['id'];
    $prog1=$rows['prog1'];
    $prog2=$rows['prog2'];
    $name1=$rows['name1'];
    $name2=$rows['name2'];
    $progress1=($prog1/20)*100;
    $progress2=($prog2/20)*100;
    ?>
    <progress> 
    	<id><?php echo $id; ?></id> 
    	<name1><?php echo $name1; ?></name1> 
    	<name2><?php echo $name2; ?></name2> 
    	<progress1><?php echo $progress1; ?></progress1>
    	<progress2><?php echo $progress2; ?></progress2>
    </progress>
    <?php
    }

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can easily accomplish this with jQuery. Use the jquery.post function:

    http://api.jquery.com/jQuery.post/

    As for calling every x seconds, I prefer this simple plugin called "timers":

    http://plugins.jquery.com/project/timers

    The jQuery UI also has a progressbar widget you may find useful.

    http://jqueryui.com/demos/progressbar/

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that, thats helpful but I'm still struggling a little.

    I don't know how to post all of the data retrieved by $id=$rows['id']; to the php script via jQuery. Would it have to be sent in an array? and how would that be extracted on the other side in the php script?

    Also when the php script query's the database and returns the result in xml form, how do I look at the xml file in jQuery and select the <id></id> which matches the id of the progress bars and then update the information $progress1 & $progress2, then also update the information in the other progress bars on the page which have different id's.

    There will not always be a set amount of progress bars. Sometimes there may be 10 sets of the progress bars other times less, depending on how many results are initially brought from the database.

    Thanks


  •  

    Tags for this Thread

    Posting Permissions

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