problem while including two scripts...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leszek

    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


  • Carl

    #2
    Re: problem while including two scripts...

    Leszek wrote:[color=blue]
    > 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?
    >[/color]

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

    Try it.

    Carl.
    [color=blue]
    > 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
    >
    >[/color]

    Comment

    • Leszek

      #3
      Re: problem while including two scripts...

      Included -yes but I need to make sure that prog1.php is done before
      prog2.php starts

      Thanks.
      Leszek


      Comment

      • J.O. Aho

        #4
        Re: problem while including two scripts...

        Leszek wrote:[color=blue]
        > Included -yes but I need to make sure that prog1.php is done before
        > prog2.php starts[/color]

        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<100 00;$i++) {
        x++;
        }
        echo $x."\n";
        for($i=0;$i<100 00;$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('script 1.php');
        include('script 2.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('script 1.php);
        } else {
        include('script 1.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

        Comment

        • Carl

          #5
          Re: problem while including two scripts...

          Leszek wrote:[color=blue]
          > Included -yes but I need to make sure that prog1.php is done before
          > prog2.php starts
          >
          > Thanks.
          > Leszek
          >
          >[/color]

          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.i nc-1.php");
          include("test.i nc-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.

          Comment

          • Juliette

            #6
            Re: problem while including two scripts...

            Leszek wrote:[color=blue]
            > Included -yes but I need to make sure that prog1.php is done before
            > prog2.php starts
            >
            > Thanks.
            > Leszek
            >
            >[/color]

            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.

            Comment

            • Leszek

              #7
              Re: problem while including two scripts...

              Yes so maybe the problem is somewhere else...

              here are my scripts
              // zawiadomienie.p hp
              <?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.p hp


              // wyslij_zaw.php
              <?php
              require("class. phpmailer.php") ;

              $mail = new PHPMailer();
              $mail->IsSMTP(); // set mailer to use
              SMTP
              $mail->Host ="smtp.poczta.o net.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 ="leszekt80@poc zta.onet.pl";
              $mail->FromName ="{$_POST['$family_name']}";
              $mail->AddAddress("le szekt80@poczta. onet.pl","EMBRY O");
              #$mail->AddAddress("el len@example.com "); // name is
              optional
              #$mail->AddReplyTo("in fo@example.com" , "Informatio n");
              # $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 = "Registrati on";
              $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.p hp');
              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






              Comment

              • J.O. Aho

                #8
                Re: problem while including two scripts...

                Leszek wrote:
                [color=blue]
                > // zawiadomienie.p hp
                > <?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>";[/color]
                // we removed the echo "<b><p>";[color=blue]
                >
                > for ($i=1;$i<=5;$i+ +){
                > $osoba=$_POST["name"."$i"];
                > if (strlen($osoba) >0){[/color]
                //we store the values to the tresc1, no output[color=blue]
                > $tresc1.=$osoba ."<br>";
                > }
                > }[/color]
                //we still want to store everything in the variable[color=blue]
                > $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>";[/color]
                // we removed the echo "<b>";[color=blue]
                > for ($i=1;$i<=5;$i+ +){
                > $osoba=$_POST["name"."$i"];
                > if (strlen($osoba) >0){[/color]
                //we store the values to the tresc2, no output[color=blue]
                > $tresc2.=$osoba ."<br>";
                > }
                > }[/color]
                //we still want to store everything in the variable[color=blue]
                > $tresc2.="</b>";[/color]
                // as we haven't made any output yet, we will doi it now,
                // both $tresc1 and $tresc2
                echo $tresc1.$tresc2 ;[color=blue]
                > ?>
                > // end of zawiadomienie.p hp[/color]

                The rest can be unmodified.
                [color=blue]
                > it worked great until i added two "for" loops in "zawiadomienie. php"[/color]

                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.

                [color=blue]
                > and now user who sends data gets on the screen only variables from the both
                > "for" loops[/color]

                This for you store everything else in $tresc1 and $tresc2

                [color=blue]
                > and the one who recives is getting email with all the form variables without
                > the ones from the "for" loops[/color]

                This for you didn't store those values in $tresc1 and $tresc2


                //Aho

                Comment

                • Carl

                  #9
                  Re: problem while including two scripts...

                  Leszek wrote:[color=blue]
                  > Yes so maybe the problem is somewhere else...
                  >
                  > here are my scripts
                  > // zawiadomienie.p hp
                  > <?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.p hp
                  >
                  >
                  > // wyslij_zaw.php
                  > <?php
                  > require("class. phpmailer.php") ;
                  >
                  > $mail = new PHPMailer();
                  > $mail->IsSMTP(); // set mailer to use
                  > SMTP
                  > $mail->Host ="smtp.poczta.o net.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 ="leszekt80@poc zta.onet.pl";
                  > $mail->FromName ="{$_POST['$family_name']}";
                  > $mail->AddAddress("le szekt80@poczta. onet.pl","EMBRY O");
                  > #$mail->AddAddress("el len@example.com "); // name is
                  > optional
                  > #$mail->AddReplyTo("in fo@example.com" , "Informatio n");
                  > # $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 = "Registrati on";
                  > $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.p hp');
                  > 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
                  >
                  >
                  >
                  >
                  >
                  >[/color]

                  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.

                  Comment

                  Working...