Help with loop

please help…this mailing script is supposed to send email message reading from record 0 to the last. send 100 per go…loop and send another 100. The problem is it keeps sending the same 100 records over and over unless I enter on the upset where i want it to start.

am I doing something wrong.

$limit = 100; //number of records per go
$offset = 0; //starting point in the database to avoid rerunning records

if (!empty($_COOKIE['offset']))
{
$offset = $_GET['offset'];
setcookie("offset",$offset+100); //we increase the count by 100 so we know where to start getting the records from
}

if(mysql_num_rows($result) > 0)
{
$count = 0;
while ($row = mysql_fetch_array ($result))

MOD EDIT: Added CODE tags

I don’t really see anything wrong with what you have, however, what I don’t see is how you are actually using the OFFSET value. I presume it’s part of your SQL statement. Perhpas you could show us that.

It isn’t in your code - but it sounds like you aren’t changing the $_GET[‘offset’] for the second and subsequent pages so it is going to the default of 0.

Are you trying to save the offset in the cookie and then use it on the next page? if so you are using the wrong superglobal. Use $_COOKIE not $_GET.

I got the sense that he was using the GET as an overide (so to speak) because it only looks for the GET if the COOKIE is empty

So, I think we need to see how the offset is being used, set, and incremented.

Here’s the whole code…I change $_GET to $_COOKIE and even added a debugging code checking for the cookie…nothing comes up…

<? $location="localhost"; $username="MyUser"; $password="MyPass"; $database="MyDB"; $conn = mysql_connect($location,$username,$password); if (!$conn) die ("Could not connect MySQL"); mysql_select_db($database,$conn) or die ("Could not open database"); $limit = 100; //number of records per go $offset = 0; //starting point in the database to avoid rerunning records if (!empty($_COOKIE['offset'])) { echo "getting here
"; $offset = $_COOKIE['offset']; echo "offset from cookie is $offset
"; $newoffset = $offset+100; echo "new offset from cookie is $newoffset
"; setcookie("offset",$newoffset); //we increase the count by 100 so we know where to start getting the records from } $headers = 'From: From me' . "rn" ; $headers .= 'Reply-To: [email protected]' . "rn" ; $headers .= 'X-Mailer: php/' . phpversion(); $headers .= "MIME-Version: 1.0n"; //send html email (req'd for image) $headers .= "Content-type: text/html; charset=iso-8859-1n"; //send html email (req'd for image) $headers .= "X-Priority: 1n"; $headers .= "X-MSMail-Priority: Highn"; //you need values for these values $your_name = "Me"; $your_email = "[email protected]"; $headers .= "From: "".$your_name."" <".$your_email.">n"; $result = mysql_query("SELECT distinct(mail1) FROM my_mail LIMIT $offset, $limit"); if(mysql_num_rows($result) > 0) { $count = 0; while ($row = mysql_fetch_array ($result)) { $to = $row['mail1']; $subject = "Testing"; //enter your subject here //enter your message here (use as many lines as required) $message = "


Paragraph!



Get extra cash </fontParagraph Paragraph


Paragraph***


Some great deals at Paragraph...
</body"; //$headers .= "Bcc: $to n" //use this to send Blind Carbon Copies (ie each user only sees his address in when sending to multiple emails) echo "$to
"; mail($to, $subject, $message, $headers); $count++; } echo "Preparing to reload the page to continue emailing the records."; echo "

Kill the program"; //you were missing the ending ';' on the line above }else{ echo "myResult=Email Submissions Failed."; } ?>

Ok…

My earlier post about the using GET as an override was actually incorrect. The code checks to see if !empty (it’s NOT empty) and if it’s NOT empty then it set’s the offset to it’s current value + 100.

First problem is, your code will never enter this loop. This loop is also where the cookie get’s set. So since first time around, the cookie never get’s set… subsequent runs of the page will not update the cookie… Therefore, it always goes to Zero.

So if you move the setcookie(“offset”,$newoffset); OUTSIDE the loop, you are a step closer… but again, you are setting a cookie called ofset to a value which was assigned in $newoffset which get’s assigned in the loop. Since there is no value in newoffset you need to move it outside of that loop.

I ended up with something like this:

$limit = 100; //number of records per go
// Sets newoffset to the cookie value + limit if it exsist or just the limit if it doesn't
$newoffset = (!empty($_COOKIE['offset'])) ? ($_COOKIE['offset']+$limit) : $limit;

setcookie("offset",$newoffset); //we increase the count by $limit so we know where to start getting the records from
?>

This eliminates the need to initialize the offset value. Later on when you need to use the OFFSET value in your sql query, instead you could just use
the newoffset MINUS the limit

$result = mysql_query("SELECT distinct(mail1) FROM my_mail LIMIT ".($offset-100).", $limit"); 

Hope this helps.

Sponsor our Newsletter | Privacy Policy | Terms of Service