Link to home
Start Free TrialLog in
Avatar of brianmfalls
brianmfallsFlag for United States of America

asked on

PHP For Loop - Updating a span for each record processed in loop

Prior to my loop, I have the following header encapsulating a span which outputs the variable $count (set to 0 to begin with):

$count = 0;
echo '<h4>Compiled Data (<span class="changeNumber">', $count, '</span> Records):</h4>';

Open in new window


Within my loop I am incrementing the value of $count by one:

$count++

Open in new window


That's all fine and good, but I don't know how to update the span without using JavaScript for each iteration of the loop.  I can make it happen all day in ColdFusion and/or JavaScript.  There has to be an equivalent PHP solution.  I'm all ears!  How do I update the span with the incremented value of $count using only PHP?
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of NerdsOfTech
NerdsOfTech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brianmfalls

ASKER

I thought that might be the case.  How then, do I integrate a script in with the php that will update the count on the client side as the server side processes?  I started out by outputting the total count.  That is simple.  :)

The purpose of updating the count is to entertain the end user.  If they see that the page is working, they are less likely to do something stupid, like refreshing the screen while the data is still being processed.  My end goal is to display the total, and to display a reverse counter that will count down from the total records being processed to 0.   The average records being processed are between 1700 and 1800.  As you would expect, that takes quite some time.  While there is a loading modal in place during processing, the user may be inclined to think that the process is stuck.  The counter would assure them that the process is running, and not stuck.

So...  Via my current, and somewhat educated but ignorant, understanding of PHP, I expected that this script (jQuery) would happily update our counter:

 echo "<script>$('.changeNumber').html('$count')</script>";

Open in new window


Clearly, it doesn't.  How can I make it work?

Ray, I'm off to read your article right now.  :)
I figured it out.  For reasons I'm still looking into, jQuery wasn't loaded in the iFrame where all the fun is taking place.  I rewrote the script with the standard, non-jQuery equivalent, then changed the class attribute to an id attribute on the counter, and viola!  It works.
echo '<h4>Compiled Data (<span id="changeNumber">', $count, '</span> Records):</h4>';

Open in new window

echo "<script>document.getElementById('changeNumber').innerHTML='$count';</script>";

Open in new window

This iteration simply updates the counter as the data is processed.  It moves in chunks rather than one at a time, but it works none the less.  I'll post up the reverse iteration when I get it done.  Shouldn't be long.  Then I'll split the points between y'all and call it done.  Thanks for the input guys!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray,

jQuery's progress bar is a great solution, but it wouldn't play nice with what I have.  It would be difficult, if not impossible to shut it down when the processing is complete.  iFrames suck in that respect.  I hate using them, but was left with no option in this instance.  Even though I am calling the iFrame through our existing framework, jQuery isn't loading...  I'm not sure I want to open that bag of worms at the moment, since everything is working, and I have a great deal of work yet to do and not a lot of time to get it done.  I know one thing though, it is being called in through the framework, since I can see the the style sheets are being leveraged.  It seems that only jQuery is awol.

I am currently using a Bootstrap modal loading variant that I was easily able to close post to processing completion.  It looks great, and in addition to the modal and the loading animation, the user can now see the records count down to 0.  It's exactly what I was shooting for.

Thank you VERY much for your valuable input.

Brian
I accepted my own solution because it was the actual solution.  The others' input was relevant and helpful in leading me to my solution, which is why I credited them as well.