Refresh multiple classes every n secs

I have some code in jquery that connects to php and refreshes the class with latest data. This is working ok. However, I need to update 3 classes and when it refreshses the values are empty.

If I use just one query and just one class it works fine. Obviously it getting confused what to do with the data.

Is there a way I can query db and update 3 classes with fresh data every n sec. I realise that I may need to use JSON but I am fairly new to this level of coding so need some help. Many thanks


// Update server with latest actions,destructions and return requests
    setInterval(function() {
      $.get('/domain/admin/refreshBox.php', function(data) {
    }, 10000);


    $nisql= mysqli_query($conn,"SELECT count(*) as ni FROM act WHERE activity='New Intake' AND new = '1'"); 
    $niintknum_row = mysqli_fetch_assoc($nisql);
    $niintknum = $niintknum_row['ni'];
    //echo $num;
    $niintk_num = $niintknum;

    if($niintk_num < 1) {
      $niintk_num = '0';

    echo $niintk_num;

    $brtvsql= mysqli_query($conn,"SELECT count(*) as rtrv FROM act WHERE activity='Box Retrieval' AND new = '1'"); 
    $brtv_row = mysqli_fetch_assoc($brtvsql);
    $brtvnum = $brtv_row['rtrv'];
    //echo $num;
    $brtv_num = $brtvnum;

    if($brtv_num < 1) {
      $brtv_num = '0';

    echo $brtv_num;

    $brtnsql= mysqli_query($conn,"SELECT count(*) as brtn FROM act WHERE activity='Box Return' AND new = '1'"); 
    $brtn_row = mysqli_fetch_assoc($brtnsql);
    $brtnnum = $brtn_row['brtn'];
    //echo $num;
    $brtn_num = $brtnnum;

    if($brtn_num < 1) {
      $brtn_num = '0';
    echo $brtn_num;


    <a href="/domain/admin/test.php" title="Add">New Intake <span style="float: right;" class="notification ni"><?php echo $niintk_num; ?></span></a>
    <a href="/" title="Retrievals">Retrievals <span style="float: right;" class="notification retrievals"><?php echo $brtv_num; ?></span></a>
   <a href="javascript:void(0);" title="Returns">Returns <span style="float: right;" class="notification returns"><?php echo $brtn_num; ?></span></a>

Looks fine to me, other than what you’re going to end up doing is setting all three of your span fields to the same value. The code as presented here could be simplified to

    setInterval(function() {
      $.get('/domain/admin/refreshBox.php', function(data) {
    }, 10000);

Consider what your PHP page is returning. (In fact, use breakpoints and examine what data actually IS, because the jquery .get() function attempts to interpret the result it sees…)

Hi Problem with that is the code puts the data in the notification class which is just used for styling. Each piece of returned data needs to go in its own class. ie, .actions or .retrievals. If I just do one query and one $.get then it works fine. It seems to be getting confused where to place the code.

It seems that the data from php after the first query dosen’t get retunred. Thanks


If I were you, i’d go one of two ways:
1: have PHP formulate it’s output as a json string (hint: [fphp]json_encode[/fphp]). Then have .get() specifically expect a JSON, and use that object to populate the fields.
2: have PHP return a comma separated data field, tell .get() to explicitly expect text, split it, and then use .text()'s ability to use an indexed function to populate the “.notification” class spans.

So the problem is in your PHP code at the moment. Let’s work on that.

You should really be condensing your SQL queries down into a single query, something along the lines of

SELECT activity, count(activity) FROM act WHERE new = '1' GROUP BY activity

But each query is testing activity for a certin string. For example I need to test if the value being being passed is testted for the correct results. Thanks

Querying a database is a lot slower than comparing data that you’ve already retrieved.

Instead of querying 3 times for a single piece of information, you can query for all of the information you need in one go.

   $out = array();
    $sql= mysqli_query($conn,"SELECT activity,count(activity) as cnt FROM act WHERE new = '1' GROUP BY activity"); 
    while($brtv_row = mysqli_fetch_assoc($brtvsql)) {
       $out[$brtv_row['activity']] = $brtv_row['cnt'];
    //Output as JSON
    //Output as CSV
    echo($out['New Intake'].",".$out['Box Retrieval'].",".$out['Box Return']);

ok. that makes sense. How do i update my classes with this new data based on the code i posted. Do i just compare data using say if statement. Many thanks

So if you bounce it back as a JSON (option 1), your javascript can check for the existance of a key and use it.

      $.get('/domain/admin/refreshBox.php', function(data) { 
             $(".ni").text(('New Intake' in data) ? ""+data['New Intake'] : "0");
             $(".retrievals").text(('Box Retrieval' in data) ? ""+data['Box Retrieval'] : "0");
             $(".returns").text(('Box Return' in data) ? ""+data['Box Return'] : "0");

The CSV version can use the fixed positioning to its advantage.

      $.get('/domain/admin/refreshBox.php', function(data) { 
             data = data.split(",");
             $(".notification").text(function (i) { return data[i]; })

(Untested code)

Sorry for delay. Away yesterday. I shall try this today and post back. Many thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.