hi
I have a for loop that extracts information from news websites and insert them into my database. How do I make the loop stops after it successfully inserts 5 rows?

hi
I have a for loop that extracts information from news websites and insert them into my database. How do I make the loop stops after it successfully inserts 5 rows?

count the number of inserts, add $numInserts <= 5 in the for loop condition

$numInserts = 0;
for($i = 0; ($i<=$countNews && $numInserts <=5); $i+=1){
// do you stuff, after every insert add
// $numInserts +=1;

}

basically the for loop can have anything in condition part that evaluates to true or false.


If this looks complicated, you can then do this:

$numInserts = 0;
for($i = 0; ($i<=$countNews); $i+=1){
// do you stuff, after every insert add
$numInserts +=1;

if($numInserts === 5){
break;
}
}

The break; will end the loop for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.