Hello,
I have been writing procedural php for a couple of years
and I have quite lot of little scrips that I use in different
large scripts.
I'll give and example
I display the category menu on onmany pages so I have this
little php file: - switch ($N_cat_cd)
-
{
-
case "ex": $Dcat = " Expert-World"; break;
-
case "se": $Dcat = " Self Improvement"; break;
-
case "s.": $Dcat = " S.E.O."; break;
-
case "tr": $Dcat = " Traffic"; break;
-
case "ni": $Dcat = " Niche-Affiliate"; break;
-
case "co": $Dcat = " Copy writing"; break;
-
case "sa": $Dcat = " Sales systems"; break;
-
case "pr": $Dcat = " Product creation"; break;
-
case "we": $Dcat = " Website Design"; break;
-
case "gr": $Dcat = " Graphics"; break;
-
}
-
$SEOcat = strtolower(trim($Dcat));
-
-
$cat_menu ="
-
<div class='menuCAT'>
-
<ul>
-
<li> <a href=\"/im/menu1.htm" > $SEOcat</a></li>
-
<li> <a href=\"/im/menu1.htm" > $SEOcat</a></li>
-
<li> <a href=\"/im/menu2.htm" > $SEOcat</a></li>
-
<li> <a href=\"/im/menu3.htm" > $SEOcat</a></li>
-
<li> <a href=\"/im/menu4.htm" > $SEOcat</a></li>
-
<li> <a href=\"/im/menu5.htm" > $SEOcat</a></li>
-
<li> <a href=\"/im/menu6.htm" > $SEOcat</a></li>
-
</ul>
-
</div>";
-
So when ever I need this code, I require inside my main php script
For example -
$page="pr";
-
$title1 = " products and resources";
-
$desc=" internet marketing products";
-
-
if (@$_SESSION['auth'] == "yes" ){
-
require_once("mem_head.php");
-
} // end if
-
else {
-
require_once("a_head.php");
-
} // end if
-
-
$dir = "products";
-
$pub = "prod";
- require_once("a_cat_menu.php");
-
?>
-
-
<div class="page_name">
-
<h1>Resources - Expert IM Products</h1>
-
</div> <!-- End div: page_name -->
-
In fact there are three sets of require command, I have bolded them
Now I was quite happy with this until I saw this
thread in a forum about an email class called swift.
The best way to work is usually by making a function in a file, and calling
that function instead of including / requiring in loop
Because the php works this way:
Read File,
Tokenize,
Compilation
Execution
If you include at each iteration, the whole process is repeated, if you use
functions, it will only execute the portion of code needed to send the mail
From a functionality point of view, it works the same
From a performance point of view, it will work way slower.
So - and here is my question:
Should I be re-writing my includes and requests as functions?
I assumed that the whole code would be read by the php engine before compilation and execution - so why the suggested slow down ?
Maybe this is just inside loops ?
The actual bit of code that was under discussion with this: Here is the script. - START
-
$swift_path = "/home/focus7/public_html/lib/swift_required.php";
-
$func_path = "/home/focus7/public_html/my_functions.php";
-
require_once("$swift_path");
-
require_once("$func_path");
-
-
$transport = Swift_SmtpTransport::newInstance('mail.MY-SITE.com', 25)
-
->setUsername('MY-NAME')
-
->setPassword('MY-PASS')
-
;
-
-
$mailer = Swift_Mailer::newInstance($transport);
-
-
$sql_client = "SELECT * FROM clients "; // OPEN
-
DATABASE & GET CLIENTS
-
$result_client = mysql_query($sql_client) or die("could not execute
-
FIND CLIENT query.". mysql_error());
-
while($row_client = mysql_fetch_assoc($result_client))
-
{ // START THE LOOP
-
extract($row_client);
-
-
$message = $message.$ek.$ek1.$ek2; // CREATE THE MESSAGE FROM
-
DATA
-
-
$simdi = date('H:m:s, l, j F Y',$today);
-
-
$to = $email; // CREATE THE
-
HEADERS
-
$contact = $contact;
-
$fm_email="Account-Mana...@support-focus.com";
-
$fm_name="Support Center";
-
$subject="Your Weekly Report";
-
$message_html = "Dear $contact,<br><br><b>Weekly Report: $simdi</
-
b><br><br>".$message”;
-
-
require("send_auto.php"); // SEE BELOW
-
-
} // end while // LOOPS THROUGH DATABASE ( ONLY 25
-
-
RECORDS )
-
-
THIS IS THE send_auto.php
-
-
START
-
$message = Swift_Message::newInstance();
-
send_auto.php
-
-
// set the sender
-
$message->setFrom(array($fm_email => $fm_name));
-
-
// set the recipient
-
$message->setTo(array($to => $contact));
-
-
// set the subject
-
$message->setSubject($subject);
-
-
// set the html body
-
$message->setBody($message_html, 'text/html');
-
-
// set the text body
-
$message->addPart($message_text, 'text/plain');
-
-
//Send the message
-
if (!$mailer->send($message, $failures)) {
-
// update failure database
-
$sql = "INSERT INTO `email_fail` (type,email,send_dt) VALUES
-
( '$send_type', '$to','$today')";
-
mysql_query($sql) or die("could not execute email_fail update
-
query". mysql_error());
-
} // end if
-
END
I would really appreciate you inputs as it may fundementally change the way I structure my code.
Thanks
5 1896 @jeddiki
this is a case where you have to carefully read. the paragraph is about include()/require() in a loop structure: - while ($condition)
-
{
-
include 'include_file.php';
-
}
then the meaning becomes obvious…
Thanks Dormilich for pointing that ,
I had only used the require because I have four scripts that
use the same sending code.
Apart from that, there is no need to put the code outside the
script. So I guess the loop will perform better like this: - START
-
$swift_path = "/home/focus7/public_html/lib/swift_required.php";
-
$func_path = "/home/focus7/public_html/my_functions.php";
-
require_once("$swift_path");
-
require_once("$func_path");
-
-
$transport = Swift_SmtpTransport::newInstance('mail.MY-SITE.com', 25)
-
->setUsername('MY-NAME')
-
->setPassword('MY-PASS')
-
;
-
-
$mailer = Swift_Mailer::newInstance($transport);
-
-
$sql_client = "SELECT * FROM clients "; // OPEN
-
DATABASE & GET CLIENTS
-
$result_client = mysql_query($sql_client) or die("could not
-
execute
-
FIND CLIENT query.". mysql_error());
-
while($row_client = mysql_fetch_assoc($result_client)) { //
-
START THE LOOP
-
extract($row_client);
-
-
$message = $message.$ek.$ek1.$ek2; // CREATE THE MESSAGE FROM
-
DATA
-
-
$simdi = date('H:m:s, l, j F Y',$today);
-
-
$to = $email; // CREATE THE
-
HEADERS
-
$contact = $contact;
-
$fm_email="Account-Mana...@support-focus.com";
-
$fm_name="Support Center";
-
$subject="Your Weekly Report";
-
$message_html = "Dear $contact,<br><br><b>Weekly Report: $simdi</
-
b><br><br>".$message”;
-
-
$message = Swift_Message::newInstance();
-
-
// set the sender
-
$message->setFrom(array($fm_email => $fm_name));
-
-
// set the recipient
-
$message->setTo(array($to => $contact));
-
-
// set the subject
-
$message->setSubject($subject);
-
-
// set the html body
-
$message->setBody($message_html, 'text/html');
-
-
// set the text body
-
$message->addPart($message_text, 'text/plain');
-
-
//Send the message
-
if (!$mailer->send($message, $failures)) {
-
// update failure database
-
$sql = "INSERT INTO `email_fail` (type,email,send_dt) VALUES
-
( '$send_type', '$to','$today')";
-
mysql_query($sql) or die("could not execute email_fail update
-
query". mysql_error());
-
} // end if
-
-
$content = "Sent Weekly Report to $contact at $email \r\n";
-
fwrite($handle, $content);
-
} // end while // END OF LOOP
-
-
fclose($handle);
-
?>
-
There seems no point in creating a function out of that code
... or is there ??? @jeddiki
if you’re sending to multiple recepients (I think you are), you can use Swiftmailer’s batchSend() method
Yes,
I did read about that,
but I also saw in their forum that a lot of people have trouble with it.
It uses the same sending code in the end so I
think I will stick to the basic version and control
it with my while loop.
But thanks anyway :)
@jeddiki
I use it and I don’t have any trouble. (I even use the Decorator Plugin). Nevertheless, batch sending is more effective, than using an external loop.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Joe Mowry |
last post by:
First off, I'm posting this question for a friend that doesn't have
access to the news groups.
Problem:
Using PHP as the base and has several PHP < includes > for various
functions, all this...
|
by: Martin Walke |
last post by:
Hi guys,
For those who responded before here's an update from hell!!!
I think I confused people before when I said on the 'page load' and Chris
suggested using the body onload. I was using the...
|
by: John Doe |
last post by:
Hi all,
Regarding those cyclic dependencies of classes (like cases in which
class A refers to class B and class B to class A): can they ALL be
resolved by forward defining classes, and by...
|
by: Jim |
last post by:
Hi:
Do we have some common style for includes when working on a project
with lots of c and h files. Wat I mean is do we have a rule in C when
a file includes several files and those file in turn...
|
by: Ben Taylor |
last post by:
Hi,
Coming from VB, I've still not really grasped the way how
in C++ if function A wants to call function B, then
function B has to be before function A in the
compilation, and #includes that use...
|
by: tshad |
last post by:
I am not sure why I am getting this error:
I have the following code I want to run from another include file that holds
all my functions.
functions.inc...
|
by: Eigenvector |
last post by:
I apologize if this is a trivial question, but it's always made me wonder
when I have to compile my code.
There are some #includes that you don't really need to reference in your
library and...
|
by: pengypenguin |
last post by:
Hello all,
I am trying to create a user authentication system, and I would like to
separate the authentication code into include files. As I see it, the
basic flow would go something like this:...
|
by: Richard |
last post by:
I am new to php and am looking for some quick "get up and go" help.
What would the arguments for and against be for function declarations v
simple include?
e.g
<?php
include("myfunc.php");...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |