Best way to program a call to php
        Posted  
        
            by hairdresser-101
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by hairdresser-101
        
        
        
        Published on 2010-04-14T06:36:23Z
        Indexed on 
            2010/04/14
            6:53 UTC
        
        
        Read the original article
        Hit count: 178
        
php
|file-get-contents
I've recently posted here http://stackoverflow.com/questions/2627645/accessing-session-when-using-file-get-contents-in-php about a problem I was having and the general consensus is that I'm not doing it right... while I generally think "as long as it works..." I thought I'd get some feedback on how I could do it better...
I was to send the exact same email in the exact same format from multiple different areas.
- When a job is entered (automatically as a part of the POST)
- Manually when reviewing jobs to re-assign to another installer
The original script is a php page which is called using AJAX to send the work order request - this worked by simply calling a standard php page, returning the success or error message and then displaying within the calling page.
Now I have tried to use the same page within the automated job entry so it accepts the job via a form, logs it and mails it.
My problem is (as you can see from the original post) the function file_get_contents() is not good for this cause in the automated script...
My problem is that from an AJAX call I need to do things like include the database connection initialiser, start the session and do whatever else needs to be done in a standalone page... Some or all of these are not required if it is an include so it makes the file only good for one purpose...
How do I make the file good for both purposes? I guess I'm looking for recommendations for the best file layout and structure to cater for both scenarios...
The current file looks like:
<?php
session_start();
$order_id = $_GET['order_id'];
include('include/database.php');
function getLineItems($order_id) {
    $query = mysql_query("SELECT ...lineItems...");
    //Print rows with data
    while($row = mysql_fetch_object($query)) {  
        $lineItems .= '...Build Line Item String...';
    }
    return $lineItems;
}
function send_email($order_id) {
    //Get data for current job to display
    $query = mysql_query("SELECT ...Job Details...");
    $row = mysql_fetch_object($query);
    $subject = 'Work Order Request';
    $email_message = '...Build Email...
                      ...Include Job Details...
                      '.getLineItems($order_id).'
                      ...Finish Email...';
    $headers  = '...Create Email Headers...';
    if (mail($row->primary_email, $subject, $email_message, $headers)) {
        $query = mysql_query("...log successful send...");
        if (mysql_error()!="") {
            $message .= '...display mysqlerror()..';
        }
        $message .= '...create success message...';
    } else {
        $query = mysql_query("...log failed send...");
        if (mysql_error()!="") {
            $message .= '...display mysqlerror()..';
        }
        $message .= '...create failed message...';
    }
    return $message;
} // END send_email() function
//Check supplier info 
$query = mysql_query("...get suppliers info attached to order_id...");
if (mysql_num_rows($query) > 0) {
    while($row = mysql_fetch_object($query)) {  
        if ($row->primary_email=="") { 
            $message .= '...no email message...';
        } else if ($row->notification_email=="") {
            $message .= '...no notifications message...';
        } else {
            $message .= send_email($order_id);
        }
    }
} else {
    $message .= '...no supplier matched message...';
}
print $message;
?>
© Stack Overflow or respective owner