Counter problem while loop

Hi

I want to send emails to only 10 addresses.

when the count 10 is reach it should stop.

The below code every time echoes 11111. It should echo 12345 till 10

<?php

$subject= "Newsletter";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:info@domain.com' . "\r\n";

$qry = "select * from ntest";
$result = mysql_query($qry);

$count = 0;
    
    while($row = mysql_fetch_array($result))
    {
    
        while($count < 10)
        {
        $count++;
        echo $row['id'];
        }

    }    

?>

The below code every time echoes 11111. It should echo 12345 till 10

Thanks
Vineet

omit the inner loop and use SELECT id FROM ntest LIMIT 10

Hi Dormilich

If i want to run this task as CRON task, then

If in the while loop i mark these 10 rows as “SENT”,
then everytime the NEXT 10 rows will be selected or i have to do some programming to again select the next 10 rows ??

Vineet

then you need something in the SQL that marks sent emails as sent (or you may delete those rows), so you can exclude them in the next query.

Remove spaces.

$count=0;

Let’s start with the minimum code changes to get OP’s code working.

Vinpkl, first time through your outer while loop, you fetch a row, then with that one row, your inner loop runs 10 times and prints the same id of the same row over and over, so we get rid of the inner loop.

$count = 0;

while(($row = mysql_fetch_array($result)) && $count < 10)
{
    $count++;
    echo $row['id'];
}

That being said, it’s better to do the limiting part directly in SQL, just like Dormilich suggested. It shouldn’t matter whether you’re running this as a cron job.

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