By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,293 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,293 IT Pros & Developers. It's quick & easy.

problem while including two scripts...

P: n/a
Hi.
Could you help solve the problem:

i have a file that has two includes:

//my script
<?php
include(.'/prog1.php');
include(.'/prog2.php');
?>
the thing is that prog1.php has two loops inside and prog2.php has no loops.
So prog1.php is slower than prog2.php

So prog2.php is done before prog1.php

Am I right?

And I want to make sure that prog1.php is done before prog2.php starts

Is it possible to do?
How can it be done?

Thanks
Leszek
Dec 28 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Leszek wrote:
Hi.
Could you help solve the problem:

i have a file that has two includes:

//my script
<?php
include(.'/prog1.php');
include(.'/prog2.php');
?>
the thing is that prog1.php has two loops inside and prog2.php has no loops.
So prog1.php is slower than prog2.php

So prog2.php is done before prog1.php

Am I right?

No.
The file 'prog1.php' will be completely 'included' before the
'prog2.php' include happens.

Try it.

Carl.
And I want to make sure that prog1.php is done before prog2.php starts

Is it possible to do?
How can it be done?

Thanks
Leszek

Dec 28 '05 #2

P: n/a
Included -yes but I need to make sure that prog1.php is done before
prog2.php starts

Thanks.
Leszek
Dec 28 '05 #3

P: n/a
Leszek wrote:
Included -yes but I need to make sure that prog1.php is done before
prog2.php starts


As everything is executed in sequel, the line thats is before another, will
first be done before the next line is executed

so we have two scripts,

--- script1.php ---
<?PHP
for($i=0;$i<10000;$i++) {
x++;
}
echo $x."\n";
for($i=0;$i<10000;$i++) {
x++;
}
echo $x."\n";
echo "script1 finished!!!\n";
?>
--- eof ---

--- script1.php ---
<?PHP
echo "script2 finished!!!\n";
?>
--- eof ---

Without any deep study we can see that script1.php takes longer time to
execute than what script2.php
We then have the main script

--- main.php ---
<?PHP
include('script1.php');
include('script2.php');
?>
--- eof ---
No matter which computer, what type of CPU it has, as long as it has a working
version of PHP that supports include(), echo() and for() (that should be all
released versions), you will get the same result

--- output of executing main.php ---
10000
20000
script1 finished!!!
script2 finished!!!
--- eof ---

If you want to execute something at the same time, you need to fork the
process and for that you need pcntl_fork()

and to make things to do as you don't want, that the results from the fastest
script run will come first, then you need to modify the main.php to look
something like this

--- forkedmain.php
if (($pid = pcntl_fork()) == -1) {
die("could not fork");
} else if ($pid) {
include('script1.php);
} else {
include('script1.php);
}
?>
--- eof ---

This could result in an output that looks more like

--- output of executing main.php ---
script2 finished!!!
10000
20000
script1 finished!!!
--- eof ---

So to be sure that your first include will be executed before your second
include, _DON'T_ use pcntl_fork().

//Aho
Dec 29 '05 #4

P: n/a
Leszek wrote:
Included -yes but I need to make sure that prog1.php is done before
prog2.php starts

Thanks.
Leszek


Leszek,

Simply put, php programs are actually scripts which get interpreted from
the top down ( barring any inline methods ).

A simple example I think will illustrate your problem (or lack thereof).
The code for the 3 scripts is below.

//file test.php
<?php

include("test.inc-1.php");
include("test.inc-2.php");

echo "<br />Done!";

?>
//file test.inc-1.php
<?php

for ($x = 0; $x < 100; $x++) {
for ($y = 0; $y < 50; $y++) {
echo '- ';
}

echo '<br />';
}

?>
//file test.inc-2.php
<?php

echo '<br /> Now in test.inc-2.php';

?>
If you run the script 'test.php' you will see that all of the output
generated from test.inc-1.php is produced before the output from
test.inc-2.php. I don't think that there is any question which script
takes longer to run.

Does this answer your question, or is there something I am missing.

Cheers,
Carl.
Dec 29 '05 #5

P: n/a
Leszek wrote:
Included -yes but I need to make sure that prog1.php is done before
prog2.php starts

Thanks.
Leszek


It will be, as Carl said.

The include statement is basically replaced with the code in the file.
This code will then execute.
The second include will only be read after the code from the first file
has all been executed.
The second include statement will then be replaced with the code from
that file etc etc.
Dec 29 '05 #6

P: n/a
Yes so maybe the problem is somewhere else...

here are my scripts
// zawiadomienie.php
<?php
$tresc1="
<b>Thanks you entered:<br />
{$_POST['$title']} {$_POST['$first_name']} {$_POST['$family_name']} <br />
{$_POST['$gender']} <br />
{$_POST['$address']} <br />
{$_POST['$pcode']} {$_POST['$city']} {$_POST['$country']} <br />
{$_POST['$phone']} <br />
{$_POST['$fax']}<br />
{$_POST['$email']}<br />
Names of accompanying porsons:</b><br />";
echo"<b><p>";

for ($i=1;$i<=5;$i++){
$osoba=$_POST["name"."$i"];
if (strlen($osoba)>0){
echo $osoba;
echo "<br>";
}
}
echo "</p></b>";
$tresc2="
<b>Nowy uczestnik konferencji zarejestrowal sie w bazie danych: </b><br
/><br />
Title: <b>{$_POST['$title']}</b> <br />
Family Name: <b>{$_POST['$family_name']}</b><br />
First Name: <b>{$_POST['$first_name']}</b><br />
Gender: <b>{$_POST['$gender']}</b><br />
Address: <b>{$_POST['$address']}</b> <br />
Postal Code: <b>{$_POST['$pcode']}</b><br />
City: <b>{$_POST['$city']}</b><br />
Country: <b>{$_POST['$country']}</b><br />
Phone: <b>{$_POST['$phone']}</b><br />
Fax: <b>{$_POST['$fax']}</b><br />
Email: <b>{$_POST['$email']}</b><br />
<br />
Accompanying persons are:<br />";
echo"<b>";
for ($i=1;$i<=5;$i++){
$osoba=$_POST["name"."$i"];
if (strlen($osoba)>0){
echo $osoba;
echo "<br>";
}
}
echo "</b>";
?>
// end of zawiadomienie.php
// wyslij_zaw.php
<?php
require("class.phpmailer.php");

$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use
SMTP
$mail->Host ="smtp.poczta.onet.pl"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username ="user"; // SMTP username
$mail->Password ="password"; // SMTP password

$mail->From ="le*******@poczta.onet.pl";
$mail->FromName ="{$_POST['$family_name']}";
$mail->AddAddress("le*******@poczta.onet.pl","EMBRYO") ;
#$mail->AddAddress("el***@example.com"); // name is
optional
#$mail->AddReplyTo("in**@example.com", "Information");
# $mail->WordWrap = 50; // set word wrap to
50 characters
# $mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
# $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$mail->IsHTML(true); // set email format to
HTML

$mail->Subject = "Registration";
$mail->Body = "$tresc2";
#$mail->AltBody = "This is the body in plain text for non-HTML mail
clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
// end of wyslij_zaw.php

// and the last one

//uzytkownik.php
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-2" />
</head>
<body>
<?php
// include('./zawiadomienia.php');
include('./wyslij_zaw.php')
?>

</body></html
// end of uzytkownik.php
it worked great until i added two "for" loops in "zawiadomienie.php"

and now user who sends data gets on the screen only variables from the both
"for" loops
and the one who recives is getting email with all the form variables without
the ones from the "for" loops

I have no idea why it doesn't work properly :((

Thanks for any ideas
Leszek


Dec 29 '05 #7

P: n/a
Leszek wrote:
// zawiadomienie.php
<?php
$tresc1="
<b>Thanks you entered:<br />
{$_POST['$title']} {$_POST['$first_name']} {$_POST['$family_name']} <br />
{$_POST['$gender']} <br />
{$_POST['$address']} <br />
{$_POST['$pcode']} {$_POST['$city']} {$_POST['$country']} <br />
{$_POST['$phone']} <br />
{$_POST['$fax']}<br />
{$_POST['$email']}<br />
Names of accompanying porsons:</b><br /><b><p>"; // we removed the echo "<b><p>";
for ($i=1;$i<=5;$i++){
$osoba=$_POST["name"."$i"];
if (strlen($osoba)>0){ //we store the values to the tresc1, no output $tresc1.=$osoba."<br>";
}
} //we still want to store everything in the variable $tresc1.="</p></b>";
$tresc2="
<b>Nowy uczestnik konferencji zarejestrowal sie w bazie danych: </b><br
/><br />
Title: <b>{$_POST['$title']}</b> <br />
Family Name: <b>{$_POST['$family_name']}</b><br />
First Name: <b>{$_POST['$first_name']}</b><br />
Gender: <b>{$_POST['$gender']}</b><br />
Address: <b>{$_POST['$address']}</b> <br />
Postal Code: <b>{$_POST['$pcode']}</b><br />
City: <b>{$_POST['$city']}</b><br />
Country: <b>{$_POST['$country']}</b><br />
Phone: <b>{$_POST['$phone']}</b><br />
Fax: <b>{$_POST['$fax']}</b><br />
Email: <b>{$_POST['$email']}</b><br />
<br />
Accompanying persons are:<br /><b>"; // we removed the echo "<b>"; for ($i=1;$i<=5;$i++){
$osoba=$_POST["name"."$i"];
if (strlen($osoba)>0){ //we store the values to the tresc2, no output $tresc2.=$osoba."<br>";
}
} //we still want to store everything in the variable $tresc2.="</b>"; // as we haven't made any output yet, we will doi it now,
// both $tresc1 and $tresc2
echo $tresc1.$tresc2; ?>
// end of zawiadomienie.php
The rest can be unmodified.
it worked great until i added two "for" loops in "zawiadomienie.php"
The problem is that you are mixing storage in variables and echo out, you
shouldn't do this, store everything in variables and when finished, then echo
out the stuff.

and now user who sends data gets on the screen only variables from the both
"for" loops
This for you store everything else in $tresc1 and $tresc2

and the one who recives is getting email with all the form variables without
the ones from the "for" loops


This for you didn't store those values in $tresc1 and $tresc2
//Aho
Dec 29 '05 #8

P: n/a
Leszek wrote:
Yes so maybe the problem is somewhere else...

here are my scripts
// zawiadomienie.php
<?php
$tresc1="
<b>Thanks you entered:<br />
{$_POST['$title']} {$_POST['$first_name']} {$_POST['$family_name']} <br />
{$_POST['$gender']} <br />
{$_POST['$address']} <br />
{$_POST['$pcode']} {$_POST['$city']} {$_POST['$country']} <br />
{$_POST['$phone']} <br />
{$_POST['$fax']}<br />
{$_POST['$email']}<br />
Names of accompanying porsons:</b><br />";
echo"<b><p>";

for ($i=1;$i<=5;$i++){
$osoba=$_POST["name"."$i"];
if (strlen($osoba)>0){
echo $osoba;
echo "<br>";
}
}
echo "</p></b>";
$tresc2="
<b>Nowy uczestnik konferencji zarejestrowal sie w bazie danych: </b><br
/><br />
Title: <b>{$_POST['$title']}</b> <br />
Family Name: <b>{$_POST['$family_name']}</b><br />
First Name: <b>{$_POST['$first_name']}</b><br />
Gender: <b>{$_POST['$gender']}</b><br />
Address: <b>{$_POST['$address']}</b> <br />
Postal Code: <b>{$_POST['$pcode']}</b><br />
City: <b>{$_POST['$city']}</b><br />
Country: <b>{$_POST['$country']}</b><br />
Phone: <b>{$_POST['$phone']}</b><br />
Fax: <b>{$_POST['$fax']}</b><br />
Email: <b>{$_POST['$email']}</b><br />
<br />
Accompanying persons are:<br />";
echo"<b>";
for ($i=1;$i<=5;$i++){
$osoba=$_POST["name"."$i"];
if (strlen($osoba)>0){
echo $osoba;
echo "<br>";
}
}
echo "</b>";
?>
// end of zawiadomienie.php
// wyslij_zaw.php
<?php
require("class.phpmailer.php");

$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use
SMTP
$mail->Host ="smtp.poczta.onet.pl"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username ="user"; // SMTP username
$mail->Password ="password"; // SMTP password

$mail->From ="le*******@poczta.onet.pl";
$mail->FromName ="{$_POST['$family_name']}";
$mail->AddAddress("le*******@poczta.onet.pl","EMBRYO") ;
#$mail->AddAddress("el***@example.com"); // name is
optional
#$mail->AddReplyTo("in**@example.com", "Information");
# $mail->WordWrap = 50; // set word wrap to
50 characters
# $mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
# $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$mail->IsHTML(true); // set email format to
HTML

$mail->Subject = "Registration";
$mail->Body = "$tresc2";
#$mail->AltBody = "This is the body in plain text for non-HTML mail
clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
// end of wyslij_zaw.php

// and the last one

//uzytkownik.php
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-2" />
</head>
<body>
<?php
// include('./zawiadomienia.php');
include('./wyslij_zaw.php')
?>

</body></html
// end of uzytkownik.php
it worked great until i added two "for" loops in "zawiadomienie.php"

and now user who sends data gets on the screen only variables from the both
"for" loops
and the one who recives is getting email with all the form variables without
the ones from the "for" loops

I have no idea why it doesn't work properly :((

Thanks for any ideas
Leszek



Leszek,

I think your problem is in "zawiadomienie.php". In this script, you
first begin to build the variable '$tresc1' then you stop appending to
this variable and echo the contents of your for loop to the client. You
then do the same for the variable '$tresc2'.

Try changing your for loop to continue appending to the proper variables.

//example code for _second_ loop:
$tresc2 .= "<b><p>";

for ($i=1;$i<=5;$i++){
$osoba=$_POST["name"."$i"];
if (strlen($osoba)>0){
$tresc2 .= $osoba."<br>";
}
}
$tresc2 .= "</p></b>";
// end-example.

This should address getting the complete text in the email, though I
think that at this point you will not see any of this on screen. If you
wish to see it on screen, in 'uzytkownik.php' add:

echo $tresc1 .'<br />'. $tresc2;

anytime after including 'zawiadomienia.php'.

Hope this helps.
Carl.
Dec 29 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.