SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX Call one after anohter

    Hello

    I have a situation, where some records will be searched from a database, and then is the process button is clicked, an ajax process will initiate that will process the first row, when the first row is done, then it will automatically initiate the ajax process for row 2 and so on.

    I want to know what is the best way to do this ?

    What i have in my mind is that, passing all the rows id in via ajax to the script and then script process and return the next id, and on the reply, if there is an id, then another ajax will initiate the process with this id, and then so on.

    For example,

    here are the ids, 2,3,4,5,7,8,9

    i will sent this to the php script like this
    PHP Code:
    request.open('get'"process.php?this_id=2&ids=2|3|4|5|7|8|9"
    Process.php will process 2, and will also return the next id (3) and all the ids (2|3|4|5|7|8|9) so that on reply, the next ajax will initiate with

    process.php?this_id=3&ids=2|3|4|5|7|8|9

    and so on,

    Is this correct approach ?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,900
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Why not request process.php?ids[]=2&ids[]=3&ids[]=4&ids[]=5&ids[]=7&ids[]=8&ids[]=9

    And then in process.php
    PHP Code:
    foreach($_GET['ids'] as $id)
    {
       if (!
    ctype_digit($id)) // make sure only numbers are requested
         
    continue;

       
    // process id $id

    I don't see why you want to call php n times for n rows while 1 call should suffice (and is probably much faster to booth).
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, I want to show the LIVE status of process on each row.

    If I do it like you said, how I can echo multiple ?

    Is it possible like this ?

    foreach($_GET['ids'] as $id)
    {
    if (!ctype_digit($id)) // make sure only numbers are requested
    continue;

    // process id $id
    echo "$id##Status = Complete"
    }

    ////////////////////
    And then on the receiving Javascript, I will split the results so that, $id and "Status = Complete" will be in 2 separate variables, and based on id,

    i.e. data = answer.split("##");
    id = data[0];
    status = data[1];
    document.getElementById("div_"+id).innerHTML = status ?

    What do you think ?

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,900
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    I would use either XML or JSON to achieve this. Plain text is too fragile to do this kind of thing.

    XML:
    Code XML:
    <statuses>
      <status for="2">Complete</status>
      <status for="3">Complete</status>
      <status for="4">Complete</status>
    </statuses>

    JSON can be built similarly.
    Note that the results won't update real time if you do it this way, but the speed advantage you have of 1 ajax call instead of n ajax calls totally makes that worth it (imho).
    How many rows are we talking about (on average)?
    If it's 3 or 4 your solution is also OK.
    Although I don't think I would implement a queue, but just fire all AJAX calls at the same time (if the processing of row n+1 does not depend on the processing of row n that is).
    If it's more than 3 or 4 rows I would go for the batch update.

    PS. Do you also make an alternative implementation for those with javascript disabled?
    PPS. How is the AJAX initiated, does the user have to press a button or something?
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello ScallioXTX

    Thanks for your suggestion and nice reply. Well, the number of rows can be 1-50. But the point is that this thing is for the admin of a site and not for the users. So at a time, there will be only 1 user doing all this and that is admin.

    For the same reason, there is no alternatives if Javascript is not enabled. As the admin knows what he needs to run this script.

    I agree that batch thing is good. I want to know that when the xml or JSON is sent as a response from AJAX, how Javascript can handle that to show ?

    Despite the fact that batch thing is good, having AJAX doing it row by row and presenting Live updates will create a good user interface experience and with this it will give an impression that the page is live.

    In the case of Admin, do you think that using Live method is ok ?

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,900
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by zeeshanhashmi View Post
    For the same reason, there is no alternatives if Javascript is not enabled. As the admin knows what he needs to run this script.


    Quote Originally Posted by zeeshanhashmi View Post
    I agree that batch thing is good. I want to know that when the xml or JSON is sent as a response from AJAX, how Javascript can handle that to show ?
    This is not something I can explain easily. You should probably read some articles/books on how to work with AJAX and XML/JSON.

    Quote Originally Posted by zeeshanhashmi View Post
    In the case of Admin, do you think that using Live method is ok ?
    I still think the live method is too slow, regardless of the arguments on why you'd want to use it.
    You need to consider that a HTTP request will take about 30ms to connect, and an additional number of ms waiting for the answer. Let's assume your php code is quite simple and can be processed in 100ms. And suppose that out of this 100ms, 50ms is needed for PHP to initialize request, and the remaining 50ms are needed for the processing itself.
    Furthermore, suppose the table has 50 rows (worst case scenario is always good for these kind of calculations).

    Using the live method you would need 50 connects, so 50x30=1500ms, and an additional 50x100ms=5000ms, adding to a grant total of 6500ms = 6.5 seconds

    Using the batch update you need only one connect, 50x1=50ms, start the PHP process once, 50x1=50ms and process all 50 rows 50x50=2500ms, adding to a grant total of 2600ms = 2.6 seconds

    Of course the numbers are fictional/estimates, but my point is that using the live method you're using a lot of time of connecting to the server and firing PHP on each request, while the batch method does not suffer from this and is therefore also much more scalable. Consider the above calculation with 1000 rows!

    If you're familiar with the Big O notiation, the Live method runs in O(n), where n is the number of rows, whereas the batch method runs in O(1).
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •