Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adapting: for( ..mysql_fetch_array) To mysql_fetch_object

Status
Not open for further replies.

webdev007

Programmer
Sep 9, 2005
168
This mailer (Bottom script) is CRON activated and as is, it does fine.
but I would like sending the emails in bacthes and pausing in between each batch
if this was procedural I could use
<<<<<<<
<?
$between_delay = 10; $send_delay = 30;
for ( $sent=0; $row = mysql_fetch_array($result); $sent++ )
{
if ( ($sent % $between_delay) == 0 )
sleep( $send_delay );
$emailall = $row[0];
>>>>>>>

But cannot figure how adapting it in an OOP environment using mysql_fetch_object.
any idea on doing it?
Thanks

$mailer = new CcMailer();

$sql = mysql_query("
SELECT
p.first_name, p.email, p.type, p.state, p.id,
m.subject, m.htmlbody, m.textbody,
l.time_posted, l.state_lost, l.hook, l.id
FROM
(a bunch of join tables)....
"); //echo"sql $sql";

while($row = mysql_fetch_object($sql))
{
// Send the emails in this loop.
$member_name = $row->first_name;
$address=$row->email;
 
Hi

I see no relation between using [tt]mysql_fetch_object()[/tt] and making it OO. What you started in the chunk of the [tt]while[/tt] block makes absolutely no difference.

What is that CcMailer ? What it does ? What it requires ?

If the CcMailer documentation said you have to use [tt]mysql_fetch_object()[/tt], then maybe it needs something like :
Code:
$mailer = new CcMailer();

[red]$mailer->loadTemplate('template.file');[/red]

[small]$sql = mysql_query("
SELECT
p.first_name, p.email, p.type, p.state, p.id,
m.subject, m.htmlbody, m.textbody,
l.time_posted, l.state_lost, l.hook, l.id
 FROM
 (a bunch of join tables)....
  "); //echo"sql $sql";[/small]

while($row = mysql_fetch_object($sql)) {
  [red]$mailer->populateTemplateWithData($row);[/red]
  [red]$mailer->send();[/red]
}

Feherke.
 
HI, Thanks.

Let's rephrase my problem
the script does a WHILE using mysql_fetch_object


What I need is mingling the fetch object
with a "FOR"
in order to proceed in sending mails by batches and pausing in between.

so it should start by for(.....mysql_fetch_object..
but I cannot make it working, probaly missing the logic.

the class CcMailer is the mail "engine".
 
Hi

Sorry for asking it, but do you understand what and why you try to do ?
[ol]
[li]Why to use [tt]for[/tt] and not [tt]while[/tt] ?[/li]
[li]Why to use [tt]mysql_fetch_object()[/tt] and not [tt]mysql_fetch_array()[/tt] ?[/li]
[/ol]
Until now, there is no logic explanation for changing those two. Excepting the situation I described earlier.

By the way, your answer related to CcMailer is far not enough.

Feherke.
 
Hi "for" does the job as in my very fisrt post

and my script uses fetch_object because I need those objects
example: from the class
var $body;

//// var methods
function Body($htmlBody)
{
this->Body = $htmlBody
}

from the script:
while($row = mysql_fetch_object($sql))
{
// Send the emails in this loop.
$member_name = $row->first_name;
$address=$row->email;
much more as the abave and as in the example
$htmlBody = $row->htmlbody;


 
what feherke is trying to say is that you do not need to use mysql_fetch_object even if you are trying to code using OOP. the more usual functions of mysql_fetch_assoc and ..._array will work just as well.

there is nothing wrong with your use of mysql_fetch_object, however.

to insert a delay within the while loop try something like this

Code:
<?
$counter = 0;
$bulk = 20;
$secs2sleep = 10;
while ($row = mysql_fetch_object($result)){
	if ($counter == $bulk){
		$counter =0;
		sleep($secs2sleep);
	}
	//process email
}
?>
 
Thank you!
this is in the direct line of what I tried to achieve

Accessorily
what would be the beast to time/benchmark it?
 
I also found a way to make working my first try
Code:
$between_delay = 20;  $send_delay = 300;

for( $sent=0; $row = mysql_fetch_object($sql);$sent++) 
{ 
	if ( ($sent % $between_delay) == 0 )     
sleep( $send_delay );

BUT WHILE is MANY FOLDS FASTER :)

Again thanks for pointing me in the correct direction
 
i've never seen a for construct like that. i can see how it works and am impressed by the ingenuity but I suspect that this type of use was not quite the intention of the developers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top