473,378 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

problem while including two scripts...

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
8 1602
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
Included -yes but I need to make sure that prog1.php is done before
prog2.php starts

Thanks.
Leszek
Dec 28 '05 #3
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rob Long | last post by:
Hey, I've written a custom HTML library using many PHP scripts as seperate files (just like I'd do a java project) and I'm having some problems where I'm including scripts in different...
4
by: WindAndWaves | last post by:
Hi Gurus I hope I am going to make sense with this question: I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a...
7
by: Ant | last post by:
Hi, I'm, having some problems with this function. function displayElements() { for (i=0;i<document.forms.elements.length; ++i) { document.writeln(document.forms.elements.value); }
0
by: Audrey Pratt | last post by:
Happy Holidays to you and allow us to play Santa this year with these awsome deals that in anyway you can refuse: 2000 Web Templates for only $18.00 (Savings Over $1,000.00) ...
42
by: Greg | last post by:
Hi, I've designed a bookmark in Ajax / PHP that I will put soon on sourceforge.net. But I've got an very tricky bug. I try it on some computers with Internet Explorer/Windows, Firefox...
34
by: Simon Wigzell | last post by:
document...focus() will scroll the form to move the specified text field into view on everything I have tried it with except Safari on the MAC. The form doesn't move. Any work around? Thanks.
18
by: len.hartley | last post by:
Hi, I am trying to pop-up a window when the user clicks on an image. The problem is that when the user clicks on the image and the window pops up OK, but the window underneath also proceeds to...
1
by: relisoft | last post by:
SEATTLE, Washington. - July 12, 2006: Reliable Software® announces the upcoming release of Code Co-op® version 5.0. Code Co-op is an affordable peer-to-peer version control system for distributed...
8
by: Dhananjay | last post by:
hello everyone Do you have any information how to generate a tool using .net which is used to translate the web page contents to html format. Plz reply me asap Thanks in advance Dhananjay
1
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.