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 1918 Dormilich 8,658
Recognized Expert Moderator Expert @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 ??? Dormilich 8,658
Recognized Expert Moderator Expert @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 :)
Dormilich 8,658
Recognized Expert Moderator Expert @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 its self works just fine.
Failure occurs on adding the XML include.
Here's the wierd: If all the PHP includes are removed/commented out,
|
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 onLoad of the body tag
anyway to start my JS function.
Anyway, I changed the order of my includes such that the one that calls all
the rest was loaded last and that worked.....or so I thought.
|
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 implementing the actual
functions outside the class definition, OR there are STILL cases which
cannot be solved in this way and could require rewriting your code /
rewriting your class hierarchy?
|
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 can call back it.
So do we keep the includes in the source file or in its header file
and just call the header from the c file. Are there any more rules
when we go for includes. Thanx to yu all who bother to share....
su
|
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 the classes in others
rely on others have to be after them accordingly.
I've got three questions about this if I may,
1) When I create an MFC application by having a dialog
| |
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
************************************************************************************
<Script runat="Server">
Sub fnHeader(client As String)
response.write("<!-- #include file = ../includes/staffingHeaders.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 header references in the compilation string.
For instance the standard hello.c
#include <stdio.h>
int main()
|
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:
if (not authentic) {
display login
} else {
display content
|
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"); // declares a function "myfunc".
myfunc("hello");
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |