Display result after each loop - php

In the code below, a url variable is increasing by 1 every time in a while loop. When $i is equal to 1000, the loop will end and $i will be displayed (from 1 all the way to 1000).
How do I display the value of $i after every loop, rather than waiting to the end?
$i = 1;
while($file = file_get_contents('http://example.com?id='.$i)) {
if($i !== 1000) {
echo $i;
}else{
break;
}
$i++;
}

You will need to flush message after each iteration. That way your browser will receive part of the information even if your request/response is still pending.
ob_implicit_flush(true);
$i = 1;
while($file = file_get_contents('http://example.com?id='.$i) && $i < 1000)
{
echo $i;
$i++;
ob_flush();
flush();
}
ob_end_flush();

Move echo $i; outside of the if() statement:
$i = 1;
while($file = file_get_contents('http://example.com?id='.$i)) {
if($i !== 1000) {
// echo $i;
}else{
break;
}
echo $i;
$i++;
}
This scenario is surely better suited for for() loop instead of while()...

PHP happen on the server. To see each value for $i immediately you would need to make the server:
do it's thing -> display that thing -> reload.
you can reload with header() but that is limited to 20 reloads (I think) unless you use header("Refresh:0");
I would make it display by making a stacking condition.
if(isset($diplay)){//2+ times through
$display = $display . $i . "<br>";
}else{//first time through
$diplay = $i."<br>";
}
then put that inside your condition
if($i !== 1000) {
if(isset($diplay)){
$display = $display . $i . "<br>";
}else{
$diplay = $i."<br>";
}
}else{
break;
}
echo $display
echo $display;
Then increment your variable
$i++;
then refresh page and pass the variable at the same time.
header("Refresh:0; url=page.php?i=$i");
then you'll have to add a condition at the beginning that gets $i or assign it if not found.
if(isset($_GET['i'])){//meaning if it's found in the url like so ?i=$i
$i = $_GET['i'];
}else{//first time through
$i = 1;
}
///////////////////putting it all together////////////////////////
if(isset($_GET['i'])){
$i = $_GET['i'];
}else{
$i = 1;
}
while($file = file_get_contents('http://example.com?id='.$i)) {
if($i !== 1000) {
if(isset($diplay)){
$display = $display . $i . "<br>";
}else{
$diplay = $i."<br>";
}
}else{
break;
}
echo $display;
$i++;
header("Refresh:0; url=page.php?i=$i");
}

Related

<PHP> How to make echo stay on the page permanently with sessions?

I have code, which is supposed to echo if it's true.
I did this with an if, but every time I run the code it will replace the previously echo'd number.
Can I make it so echo doesn't replace the past echo, but add an additional $i every time I run the code?
$i = $_SESSION["priem"];
$i++;
$aantaldelers = 2;
for ($j = 2; $j < $i; $j++) {
if ($i%$j == 0) {
$aantaldelers++;
}
}
if ($aantaldelers == 2) {
echo "$i, ";
}
$aantaldelers = 2;
$_SESSION["priem"] = $i;
yes you can do it by adding a string in session same as variable
$i = $_SESSION["priem"];
$i++;
$str = isset($_SESSION["str"]) ? $_SESSION["str"] : ''; //my changes
$aantaldelers = 2;
for ($j=2; $j<$i; $j++)
if ($i%$j == 0)
$aantaldelers++;
if ($aantaldelers == 2) {
$str .= "$i, "; //my changes
echo $str; //my changes
}
$aantaldelers = 2;
$_SESSION["priem"] = $i;
$_SESSION["str"] = $str; //my changes

php for loops not working - second loop not working after first loop

I have two for loops in php. Only the first loop is outputted. Why is the second for loop being ignored? I have spent time looking at this code and trying different things so any thoughts will be much appreciated. I have tried each for loop independently and they work so i know the code is correct.
The problem just seems to be when I'm asking a second for loop to run after the first.
Many Thanks
$str_test = file_get_contents("...");
$count = 0;
$count2=0;
for($y = 0; $y<strlen($str_test); $y++)
{
$pos2 = strpos($str_test, "<div class=\"f-reviews-title f-reviews-title--noSpace\" itemprop=\"author\">",$count2);
if($pos2 == $count2){
$pos2 = substr($str_test, $pos2+196);
$pos3 = strpos($pos2, "</a>");
$author= substr($pos2, 0, $pos3);
echo '<br>';
echo $author;
echo '<br>';
}
$count2++;
}
for($i = 0; $i<strlen($str_test); $i++)
{
$pos = strpos($str_test, "<p class=\"f-reviews-txt\" itemprop=\"reviewBody\">", $count);
if($pos == $count){
$pos= substr($str_test, $pos+47);
$pos4 = strpos($pos, "</p>");
$review= substr($pos, 0, $pos4);
echo '<br>';
echo $review;
echo '<br>';
}
$count++;
}

Each 3rd Increment Loop PHP

Hi im looking for a way to breakout of a loop every 3 increments to echo a static string.this script echos a "test" after each increment. i want a test after each 3rd increment. any ideas ?
my loop:
<?php
$i = 0;
while (++$i < 100){
$x = $i - 3;
if ($i+3) {echo $i . "<br>TEST<br>";}
else{ echo $i . "<br>";}
}
?>
try below Code
<?php
for($i=1;$i<=100;$i++)
{
if($i%3==0)
{
echo $i."test"."<br>";
}
}
?>
use % operator in if loop,
i = 0;
while (++$i < 100){
$x = $i - 3;
if ($i%3 == 0) {echo $i . "<br>TEST<br>";}
else{ echo $i . "<br>";}
}

how do I make an array position equal a variable

what I want to do is run this for loop and within there is a foreach searching the positions. what I want to do is once there it returns false I want it to break and save the position of $i in a variable. I'm using simple_html_dom.php but I don't think that matters since this is more of a basic programming problem.
for($i = $0; $i < $20; $i++){
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
break;
}
}
//this is not valid, but essentialy this is what I want to do.
$stop = $i;
}
To break multiple levels in a loop you simply specify the levels, eg, break 2 - see the manual on break - http://www.php.net/manual/en/control-structures.break.php.
As such your code might work as
for($i = $0; $i < $20; $i++){
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
$stop = $i; // Set variable
break 2; // break both loops
// or alternatively force the outer loop condition to expire
//$i = 21; // Force the outer loop to exit
//break;
}
}
}
I have expanded to question to set $i = 21 to break the outer loop with a single break.
Untested code but syntax checked...
<?php
// Untested code...
// Assume that you WILL break out of the loops...
$currentForIdx = -1; // so we can test that 'for' loop actually ran
$quitLoops = false;
for($i = 0; $i < $20 && !quitLoops; $i++) {
$currentForIdx = $i; // in case we break out of the loops
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false) {
$quitLoops = true;
break;
}
}
}
// test $quitLoops and $currentForIdx to work out what happened...
?>
I havent tested this, but I would try something like this:
for($i = $0; $i < $20; $i++){
$stop = false;
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
$stop = $i;
}
}
if ($stop !== false) {break;}
}

for loop keeps timing out, but it looks fine

Morning, I have a script tha is calling entries from a database, their history actually, and then displays the entries in a table, right now in order to create the table im using a pretty big for loop to make comparisons and add the results in. However for some reason it is timing out after the 30 seconds(the loop doesnt make that many cycles) i have dtermined that the cause is an issue with the first inner loop due to the fact that the echo statement in there just repeats for the duration of the loop, it never leaves that. any ideas?
for($i = 1; $i <= $FirstCount; $i++)
{
$HistoryTable .= "<tr>";
if($i = 1)
{
for($j = 0; $j < $ThirdCount; $j++)
{
if($EntryTwo[0][$j+1] == $EntryOne[$j])
{
$HistoryTable .= "<td></td>";
}
else
{
$HistoryTable .= "<td>".$EntryTwo[0][$j+1]."</td>";
}
echo $EntryTwo[0][$j+1].' == '.$EntryOne[$j];
}
}
else
{
$first = 0;
$second = 1;
for($k = 1; $k <= $SecondCount; $k++)
{
if($EntryTwo[$first][$k] == $EntryTwo[$second][$k])
{
$HistoryTable .= "<td>".$EntryTwo[$second][$k]."</td>";
}
else
{
$HistoryTable .= "<td></td>";
}
$first++;
$second++;
}
unset($k);
unset($first);
unset($second);
}
$HistoryTable .= "</tr>";
}
variables:
$FirstCount = 4;
$SecondCount = 18
$ThirdCount = 17
if($i = 1) is setting $i to 1 every time, so it's an infinite loop.
What you want is if ($i == 1).

Categories