views:

44

answers:

1

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.

  1. When a job is entered (automatically as a part of the POST)
  2. 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;

?>
A: 

make a function and include it
Do separate functions. Authentication (which requires sessions) from mail sending (which don't)
Then include mail sending function into both tasks.

Col. Shrapnel
Thanks Col but I was hoping for a little bit more guidance... I've already got that feedback on another post... I'm more looking to understand what to put in what function and what to ignore...I'm a very basic-intermediate programmer... It is a hobby for me, not a job so I don't have much training on the fundamentals
sjw