script within a script.. or not

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Auselan
Forum Newbie
Posts: 18
Joined: Sat Dec 27, 2008 7:04 am

script within a script.. or not

Post by Auselan »

I decided to remove this post as it potentially revealed coding i don't want in the public arena
Last edited by Auselan on Tue Dec 11, 2012 2:55 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: script within a script.. or not

Post by califdon »

I think you would find it more understandable (I certainly would) if you get rid of the concept of an embedded "script within in a script" because that's not what it is. It's simply a script that contains a loop, as your second example does. Indeed, most PHP scripts have such loops, often several.

Do you really want it to send these emails every time you update the database with a new record? If you do, that's fine, but if you want to be able to update the database separately and send emails separately, then you have to revise the script to do that.

So you already have an example script that contains a loop to obtain the email addresses you want (although there are simpler ways to code it, I don't want to confuse you at this point). All you have to do is insert the same kind of code in your first script but instead of echoing a string of these addresses, at the same point of the loop just use the mail($to,$subject,$body,$from) function, only use your $email value instead of $to. You only need to set the values of the $subject, $body and $from variables once, before the loop, since they appear to be the same for all of the emails. And you will probably want to have just one echo to the screen, after the close of the loop, unless you do want to echo each successful email that is sent.
Auselan
Forum Newbie
Posts: 18
Joined: Sat Dec 27, 2008 7:04 am

Re: script within a script.. or not

Post by Auselan »

califdon wrote:Do you really want it to send these emails every time you update the database with a new record?
not madness I assure you - this is to alert doctors who are wanting receive emails regarding new work as and when it is posted to the website - they can opt out by unticking their box on their profile ;)

So if I follow you correctly - if I modify the code below slightly, I can replace the echo with a mail function and the server will just process them all individually until it has completed every email address until it runs out of results from the query:

Code: Select all

 
include("connect.php");
// these are my variables to convert the form data //
$practice = $_POST['practice'];
$date_posted = $_POST['date_posted'];
$job_start_date = $_POST['job_start_date'];
$job_end_date = $_POST['job_end_date'];
$contact = $_POST['contact'];
$session_visible = $_POST['session_visible'];
$accepted_by = $_POST['accepted_by'];
$job_details = $_POST['job_details'];
 
$results = mysql_query($query) or die
("Could not execute query : $query." . mysql_error());
// these lines pass the information into the job table //
$query = "INSERT INTO job (id, practice, date_posted, job_start_date, job_end_date, contact, session_visible, accepted_by, job_details)
VALUES ('', '$practice', CURRENT_TIMESTAMP, '$job_start_date', '$job_end_date', '$contact', 'Y', '$accepted_by', '$job_details')";
 
// defining the variables for use in the outbound email //
$from = "From: OxNoG Jobs <admin@oxnog.org>";
$subject = "New Job alert";
$body = "$contact from $practice has posted a new job advertisement starting on $job_start_date and finishing on $job_end_date. To review the details, log in to the database at http://www.oxnog.org using password: nogger  If the job does not appear in the database, it is because the session has already been filled, and the practice manager has de-activated the session.
 
Contact the Group Administrator on admin@oxnog.org if you need help";
 
// now I'm setting up a loop to send out a series of emails to the members who have chosen to receive updates //
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * from doctors WHERE (working='Y' AND validated='Y')";
$result=mysql_query($query);
 
$num=mysql_numrows($result);
 
mysql_close();
 
$i=0;
while ($i < $num) {
 
$emailaddy=mysql_result($result,$i,"email");
 
mail($emailaddy,$subject,$body,$from);
 
$i++;
}
// I'll need to add an echo here to tell the person posting the advert to the database that their post has been added and distributed successfully ok later on //
 
I'm so grateful to have some input into this - I can't tell ou how lonely it is coding this stuff on your own!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: script within a script.. or not

Post by califdon »

That's almost it. Move your mysql_close() down, outside the loop, though. Also remove the @ from in front of the mysql_query(). That suppresses error messages, including the one from die(). Only use the @ after you're absolutely sure that everything is working correctly, or you'll miss diagnostics.

Then it should work, I'd think. For a more conventional (and probably easier to understand when reading the code) syntax for the loop, as well as proper indentation, I recommend the following:

Code: Select all

...
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * from doctors WHERE (working='Y' AND validated='Y')";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)) {
    $emailaddy=$row['email'];
    mail($emailaddy,$subject,$body,$from);
}
...
The concept behind the above syntax is that the while loop will continue to loop as long as the $row variable (which is, in effect, a pointer to the array of data fetched) isn't Null, so you don't need to determine the number of rows, or count the number of iterations of the loop. Just loop while (as long as) there is data to be fetched. And you can define $row in the same statement in which you are testing whether it is Null or not. When there is no more data to be fetched from the $result resource, $row will contain a Null, so the loop won't execute again.

Actually, you really don't even need the mysql_close(), because PHP automatically closes the database connection when the scripts terminates. I think most developers don't bother with it.

You could add refinements such as intercepting any errors from the mail() function, such as caused by an invalid email address or a SMTP server that's down, but I'd have to think about how to do that so it doesn't abort the entire operation if one address is bad, but it would be nice to send you either an email confirming the success of all emails or a list of emails sent or not sent. And you have identified another function, notifying the user that the emails were sent. So you have some more to do, but I hope this gets you started.

You won't have to feel lonely anymore. This Forum has lots of experienced folks willing to help you.
Auselan
Forum Newbie
Posts: 18
Joined: Sat Dec 27, 2008 7:04 am

Re: script within a script.. or not

Post by Auselan »

:drunk: *cries tears* it works... coding php/mysql is like being in love - hateful and joyful at the same time. although words (even with smilies) can't express my gratitude properly, I rather suspect that you folks know how I feel.
Auselan wrote:could add refinements such as intercepting any errors from the mail() function, such as caused by an invalid email address
I'm not partic concerned about this as they will bounce to the administrators email account, and any such errors will need to be ironed out pronto if they happen - there's only a limited number of punters using the database so it should be manageable.

Code: Select all

<?php
include("connect.php");
$practice = $_POST['practice'];
$date_posted = $_POST['date_posted'];
$job_start_date = $_POST['job_start_date'];
$job_end_date = $_POST['job_end_date'];
$contact = $_POST['contact'];
$session_visible = $_POST['session_visible'];
$accepted_by = $_POST['accepted_by'];
$job_details = $_POST['job_details'];
 
$query = "INSERT INTO job (id, practice, date_posted, job_start_date, job_end_date, contact, session_visible, accepted_by, job_details)
VALUES ('', '$practice', CURRENT_TIMESTAMP, '$job_start_date', '$job_end_date', '$contact', 'Y', '$accepted_by', '$job_details')";
 
$results = mysql_query($query) or die 
("Could not execute query : $query." . mysql_error());
 
if ($results)
{
echo "Details added.";
}
 
// defining the variables for use in the outbound email //
$from = "From: OxNoG Jobs <admin@oxnog.org>";
$subject = "New Job alert";
$body = "$contact from $practice has posted a new job advertisement starting on $job_start_date and finishing on $job_end_date. To review the details, log in to the database at http://www.oxnog.org using password: nogger  If the job does not appear in the database, it is because the session has already been filled, and the practice manager has de-activated the session.
 
Contact the Group Administrator on admin@oxnog.org if you need help";
 
// now I'm setting up a loop to send out a series of emails to the members who have chosen to receive updates //
mysql_select_db($dbase) or die( "Unable to select database");
$query="SELECT * from doctors WHERE (working='Y' AND validated='Y')";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)) {
    $emailaddy=$row['email'];
    mail($emailaddy,$subject,$body,$from);
}
 
?>
<link href="/CSS/colors5.css" rel="stylesheet" type="text/css" />
Emails have been generated to all doctors with full membership who are looking for Locum work<br>
<a href="shortindex.php">click here to go back to the Job index and view/modify the advert</a>
In due course I'll add something to email the person who posted the job back to them, and I'll add something acid proof to confirm the specific details of wot was posted out, but for the time being I've got something functional which was wot I needed. Thanksyou :D
Post Reply