开发者

Best way to program a call to php

I've recently posted here 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-ass开发者_如何转开发ign 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;

?>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜