Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Little help with my php code pls

Little help with my php code pls

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
helpdatabasejavascriptphphtml
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wartotojas
    wrote on last edited by
    #1

    hi everybody, Im new with php, started recently, Im making a registration form for my website and here is the problem. I try to check if email address entered isn't already in use. here is the code Im using:

    mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
    mysql_select_db($dbname) or die(mysql_error());

    $name = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $nickname = $_POST['nickname'];
    $password = md5($_POST['password']);

    // lets check to see if the username already exists
    $checkuser = mysql_query("SELECT email FROM users WHERE email='$email'");

    $username_exist = mysql_num_rows($checkuser);

    if($username_exist > 0){
    die('{status:0,txt:"This email address is already registered"}');
    unset($email);
    include 'register.html';
    exit();

    // lf no errors present with the username
    // use a query to insert the data into the database.

    $query = "INSERT INTO users (name, lname, email, nickname, password)
    VALUES('$name', '$lname', '$email', '$nickname', '$password')";
    mysql_query($query) or die(mysql_error());
    mysql_close();

    echo "You have successfully Registered";

    It does the job, doesnt send data to database if email is registered, but it allways displays error message

    {status:0,txt:"This email address is already registered"},

    and don't output message that you have been registered. If email address is unique it creates a record in a database, but displays error and doesnt redirect. Its probably something simple, but I cant figure out by my self. Anyone any ideas? By the way my form includes jQuery and few functions if it makes any difference. thanx in advance

    F 1 Reply Last reply
    0
    • W wartotojas

      hi everybody, Im new with php, started recently, Im making a registration form for my website and here is the problem. I try to check if email address entered isn't already in use. here is the code Im using:

      mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
      mysql_select_db($dbname) or die(mysql_error());

      $name = $_POST['fname'];
      $lname = $_POST['lname'];
      $email = $_POST['email'];
      $nickname = $_POST['nickname'];
      $password = md5($_POST['password']);

      // lets check to see if the username already exists
      $checkuser = mysql_query("SELECT email FROM users WHERE email='$email'");

      $username_exist = mysql_num_rows($checkuser);

      if($username_exist > 0){
      die('{status:0,txt:"This email address is already registered"}');
      unset($email);
      include 'register.html';
      exit();

      // lf no errors present with the username
      // use a query to insert the data into the database.

      $query = "INSERT INTO users (name, lname, email, nickname, password)
      VALUES('$name', '$lname', '$email', '$nickname', '$password')";
      mysql_query($query) or die(mysql_error());
      mysql_close();

      echo "You have successfully Registered";

      It does the job, doesnt send data to database if email is registered, but it allways displays error message

      {status:0,txt:"This email address is already registered"},

      and don't output message that you have been registered. If email address is unique it creates a record in a database, but displays error and doesnt redirect. Its probably something simple, but I cant figure out by my self. Anyone any ideas? By the way my form includes jQuery and few functions if it makes any difference. thanx in advance

      F Offline
      F Offline
      fly904
      wrote on last edited by
      #2

      wartotojas wrote:

      die('{status: 0,txt:"This email address is already registered"}');

      The die function stops the process from running, so every thing after die is not executed.

      wartotojas wrote:

      if($username_exist > 0){
      die('{status:0,txt:"This email address is already registered"}');
      unset($email);
      include 'register.html';
      exit();

      // lf no errors present with the username
      // use a query to insert the data into the database.

      $query = "INSERT INTO users (name, lname, email, nickname, password)
      VALUES('$name', '$lname', '$email', '$nickname', '$password')";
      mysql_query($query) or die(mysql_error());
      mysql_close();

      echo "You have successfully Registered";

      Why are you using die and exit? Where is the if tag closed? I can see the curly bracket open, but not close. This should work. Notice that I dont use die or exit (apart from if the query fails):

      // If the username exists then:
      // 1: Close the MySQL connection.
      // 2: Output that the email has already been registered.
      // 3: Include the registration file.
      if ( $username_exist > 0 )
      {
      mysql_close();
      echo 'Email already registered';
      include( 'register.html' );
      }

      // Otherwise (If the username doesn't exist):
      // 1: Insert the new user.
      // 2: Close the MySQL connection.
      // 3: Output that the user has been registered.
      else
      {
      $query = "INSERT INTO users (name, lname, email, nickname, password)
      VALUES('$name', '$lname', '$email', '$nickname', '$password')";
      mysql_query( $query ) or die( mysql_error() );
      mysql_close();

      echo "You have successfully Registered";
      }

      If at first you don't succeed, you're not Chuck Norris.

      W 1 Reply Last reply
      0
      • F fly904

        wartotojas wrote:

        die('{status: 0,txt:"This email address is already registered"}');

        The die function stops the process from running, so every thing after die is not executed.

        wartotojas wrote:

        if($username_exist > 0){
        die('{status:0,txt:"This email address is already registered"}');
        unset($email);
        include 'register.html';
        exit();

        // lf no errors present with the username
        // use a query to insert the data into the database.

        $query = "INSERT INTO users (name, lname, email, nickname, password)
        VALUES('$name', '$lname', '$email', '$nickname', '$password')";
        mysql_query($query) or die(mysql_error());
        mysql_close();

        echo "You have successfully Registered";

        Why are you using die and exit? Where is the if tag closed? I can see the curly bracket open, but not close. This should work. Notice that I dont use die or exit (apart from if the query fails):

        // If the username exists then:
        // 1: Close the MySQL connection.
        // 2: Output that the email has already been registered.
        // 3: Include the registration file.
        if ( $username_exist > 0 )
        {
        mysql_close();
        echo 'Email already registered';
        include( 'register.html' );
        }

        // Otherwise (If the username doesn't exist):
        // 1: Insert the new user.
        // 2: Close the MySQL connection.
        // 3: Output that the user has been registered.
        else
        {
        $query = "INSERT INTO users (name, lname, email, nickname, password)
        VALUES('$name', '$lname', '$email', '$nickname', '$password')";
        mysql_query( $query ) or die( mysql_error() );
        mysql_close();

        echo "You have successfully Registered";
        }

        If at first you don't succeed, you're not Chuck Norris.

        W Offline
        W Offline
        wartotojas
        wrote on last edited by
        #3

        I used die function few times, and it doesnt kill all process. here is full php code Im using :

        <?php

        // we check if everything is filled in

        if(empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['email']) || empty($_POST['nickname']) || empty($_POST['pass']))
        {
        die('{status:0,txt:"All the fields are required"}');
        }

        // is the email valid?

        if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])))
        {
        die('{status:0,txt:"You haven\'t provided a valid email"}');
        }

        $dbhost = "localhost";
        $dbname = "register";
        $dbuser = "*******";
        $dbpass = "******";

        mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
        mysql_select_db($dbname) or die(mysql_error());

        $name = $_POST['fname'];
        $lname = $_POST['lname'];
        $email = $_POST['email'];
        $nickname = $_POST['nickname'];
        $password = md5($_POST['password']);

        $checkuser = mysql_query("SELECT email FROM users WHERE email='$email'");

        $username_exist = mysql_num_rows($checkuser);

        if ( $username\_exist > 0 ){   
        	mysql\_close();   
        	echo 'Email already registered';   
        	include( 'members.php' );
        }
        
        else{   
        $query = "INSERT INTO users (name, lname, email, nickname, password)
        VALUES('$name', '$lname', '$email', '$nickname', '$password')";
        mysql\_query( $query) or die( mysql\_error() );
        mysql\_close();   
        echo "You have successfully Registered";
        }
        

        ?>

        It is still doesnt work. I used die('{status:0,txt:"You haven\'t provided a valid email"}'); to display error message in error box, before you are redirected, I tought it will work instead of echo statment and it is working with first few fields. Like I said before records are created in database in the way they are supposed to, the only thing is that error message I want to make for email validation. Any suggestion about that, and this might sound realy dumb, but how do I make that after registration and clicking submit button users would be redirected to other page, lets say "members.php"? thanks for answers and any help :)

        F 1 Reply Last reply
        0
        • W wartotojas

          I used die function few times, and it doesnt kill all process. here is full php code Im using :

          <?php

          // we check if everything is filled in

          if(empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['email']) || empty($_POST['nickname']) || empty($_POST['pass']))
          {
          die('{status:0,txt:"All the fields are required"}');
          }

          // is the email valid?

          if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])))
          {
          die('{status:0,txt:"You haven\'t provided a valid email"}');
          }

          $dbhost = "localhost";
          $dbname = "register";
          $dbuser = "*******";
          $dbpass = "******";

          mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
          mysql_select_db($dbname) or die(mysql_error());

          $name = $_POST['fname'];
          $lname = $_POST['lname'];
          $email = $_POST['email'];
          $nickname = $_POST['nickname'];
          $password = md5($_POST['password']);

          $checkuser = mysql_query("SELECT email FROM users WHERE email='$email'");

          $username_exist = mysql_num_rows($checkuser);

          if ( $username\_exist > 0 ){   
          	mysql\_close();   
          	echo 'Email already registered';   
          	include( 'members.php' );
          }
          
          else{   
          $query = "INSERT INTO users (name, lname, email, nickname, password)
          VALUES('$name', '$lname', '$email', '$nickname', '$password')";
          mysql\_query( $query) or die( mysql\_error() );
          mysql\_close();   
          echo "You have successfully Registered";
          }
          

          ?>

          It is still doesnt work. I used die('{status:0,txt:"You haven\'t provided a valid email"}'); to display error message in error box, before you are redirected, I tought it will work instead of echo statment and it is working with first few fields. Like I said before records are created in database in the way they are supposed to, the only thing is that error message I want to make for email validation. Any suggestion about that, and this might sound realy dumb, but how do I make that after registration and clicking submit button users would be redirected to other page, lets say "members.php"? thanks for answers and any help :)

          F Offline
          F Offline
          fly904
          wrote on last edited by
          #4

          wartotojas wrote:

          I tought it will work instead of echo statment

          No, read some documentation: php.net[^] W3Schools[^].

          wartotojas wrote:

          It is still doesnt work

          Ok. Here is a basic example of how you should do it.

          <?php
          // If the form has been submitted
          if ( isset( $_POST[ 'submit' ] )
          {
          // Check name and age are set.
          if ( isset( $_POST[ 'name' ] ) && isset( $_POST[ 'age' ] ) )
          {
          if ( strlen( $_POST[ 'name' ] ) > 0 && strlen( $_POST[ 'age' ] ) > 0 )
          {
          // Do more validation with name and age.
          // Do something with name and age.

                  if ( \*something\* )
                  {
                      // Redirect.
                      header( 'Location: redirect.php' );
          
                      // Exit the script.
                      exit();
                  }
                  else
                      $result = 'Something went wrong!';
              }
              else
                  $result = 'You must fill in name AND age.';
          }
          else
              $result = 'Name and age are not set';
          

          }
          ?>

          ...
          

          ...

          <?php echo $result; ?>

          Name: 
          Age:  
          

          Notice that the form is processed before anything is output, therefore you can redirect. To redirect to another page use header( 'Location: members.php' );. This can only be used before you have output anything to the browser. Example: This will work

          header( 'Location: members.php' );
          echo 'This won\'t be output as it the page will have been redirected.';

          This won't work

          echo 'This has been output, therefore headers cannot be set.';
          header( 'Location: members.php' );

          The exit() function is normally used after a redirect.

          If at first you don't succeed, you're not Chuck Norris.

          C 1 Reply Last reply
          0
          • F fly904

            wartotojas wrote:

            I tought it will work instead of echo statment

            No, read some documentation: php.net[^] W3Schools[^].

            wartotojas wrote:

            It is still doesnt work

            Ok. Here is a basic example of how you should do it.

            <?php
            // If the form has been submitted
            if ( isset( $_POST[ 'submit' ] )
            {
            // Check name and age are set.
            if ( isset( $_POST[ 'name' ] ) && isset( $_POST[ 'age' ] ) )
            {
            if ( strlen( $_POST[ 'name' ] ) > 0 && strlen( $_POST[ 'age' ] ) > 0 )
            {
            // Do more validation with name and age.
            // Do something with name and age.

                    if ( \*something\* )
                    {
                        // Redirect.
                        header( 'Location: redirect.php' );
            
                        // Exit the script.
                        exit();
                    }
                    else
                        $result = 'Something went wrong!';
                }
                else
                    $result = 'You must fill in name AND age.';
            }
            else
                $result = 'Name and age are not set';
            

            }
            ?>

            ...
            

            ...

            <?php echo $result; ?>

            Name: 
            Age:  
            

            Notice that the form is processed before anything is output, therefore you can redirect. To redirect to another page use header( 'Location: members.php' );. This can only be used before you have output anything to the browser. Example: This will work

            header( 'Location: members.php' );
            echo 'This won\'t be output as it the page will have been redirected.';

            This won't work

            echo 'This has been output, therefore headers cannot be set.';
            header( 'Location: members.php' );

            The exit() function is normally used after a redirect.

            If at first you don't succeed, you're not Chuck Norris.

            C Offline
            C Offline
            cjoki
            wrote on last edited by
            #5

            I always include the form processing at the top to the same page and allow the logic to either load a successful registration page on success or reload the same page with errors marked in the form. The general form is

            I often leave the action empty as it will default to the same page.

            Hope this helps

            F 1 Reply Last reply
            0
            • C cjoki

              I always include the form processing at the top to the same page and allow the logic to either load a successful registration page on success or reload the same page with errors marked in the form. The general form is

              I often leave the action empty as it will default to the same page.

              Hope this helps

              F Offline
              F Offline
              fly904
              wrote on last edited by
              #6

              Why do I need to know this?

              cjoki wrote:

              $pg = "\n"; $pg.= "\n"; $pg.= " Register\n"; $pg.= "\n"; $pg.= "\n"; $pg.= "

              \n"; $pg.= "Name: :omg: :wtf: Why are you assigning it to a variable? It's quicker just to output it directly onto the screen.

              Register

              Name:

              Also use ' to encapsulate strings as they are quicker than ", as " marks compile the string to check for variables, hence why they are slower. I suggest you look into using a template engine, such as Smarty[^], to format your pages.

              If at first you don't succeed, you're not Chuck Norris.

              C 1 Reply Last reply
              0
              • F fly904

                Why do I need to know this?

                cjoki wrote:

                $pg = "\n"; $pg.= "\n"; $pg.= " Register\n"; $pg.= "\n"; $pg.= "\n"; $pg.= "

                \n"; $pg.= "Name: :omg: :wtf: Why are you assigning it to a variable? It's quicker just to output it directly onto the screen.

                Register

                Name:

                Also use ' to encapsulate strings as they are quicker than ", as " marks compile the string to check for variables, hence why they are slower. I suggest you look into using a template engine, such as Smarty[^], to format your pages.

                If at first you don't succeed, you're not Chuck Norris.

                C Offline
                C Offline
                cjoki
                wrote on last edited by
                #7

                I have used this style for some time and it works fine and gives me better control over the output. With echo once something is echoed its gone to the browser. Any performance hits from the quote style has never been an issue. Also its just habit, I generally write much bigger forms that have variables worked into the HTML output...and why are you asking me to look into a template system when your concern is performance? As for smarty...or any other template engine...why bother? PHP IS my template engine. This style of code allows me to separate PHP and HTML from css and js. That allows me to write large scripts and maintain complete control over the source and keep it clean and easy to read, and thus maintain. Template engines just add another layer that IMHO is not needed and does little more than complicate a design and add to the learning.

                modified on Monday, October 12, 2009 2:08 PM

                F 1 Reply Last reply
                0
                • C cjoki

                  I have used this style for some time and it works fine and gives me better control over the output. With echo once something is echoed its gone to the browser. Any performance hits from the quote style has never been an issue. Also its just habit, I generally write much bigger forms that have variables worked into the HTML output...and why are you asking me to look into a template system when your concern is performance? As for smarty...or any other template engine...why bother? PHP IS my template engine. This style of code allows me to separate PHP and HTML from css and js. That allows me to write large scripts and maintain complete control over the source and keep it clean and easy to read, and thus maintain. Template engines just add another layer that IMHO is not needed and does little more than complicate a design and add to the learning.

                  modified on Monday, October 12, 2009 2:08 PM

                  F Offline
                  F Offline
                  fly904
                  wrote on last edited by
                  #8

                  cjoki wrote:

                  This style of code allows me to separate PHP and HTML from css and js.

                  How is $pg = "\n"; separating the two? You include the HTML IN your PHP. CSS and JS can be separated from HTML into several separate files (using the link tags and the script src attribute, respectively), rather than being scripted in, so to say. I prefer having separate files. When separating JS and CSS there are always certain bits which have to overlap such ass the class attributes. Smarty does the same for PHP and HTML. This is a very basic example of using Smarty. index.php

                  <?
                  include('Smarty.class.php');
                  $Smarty = new Smarty();
                  $Smarty->assign( 'message' , 'Smarty Rocks!!!' );
                  $Smarty->display( 'index.tpl' );
                  ?>

                  index.tpl

                  Smarty Example
                  
                  {$message}
                  

                  cjoki wrote:

                  Template engines just add another layer that IMHO is not needed and does little more than complicate a design and add to the learning.

                  After looking at the above code, you can't say that it's not insanely simple.

                  cjoki wrote:

                  and why are you asking me to look into a template system when your concern is performance?

                  Quite simply, I don't actually do the website design (I'm colour blind), I just worry about the functionality. The designer I hire designs the site and then writes the HTML and CSS and leaves me to do all the JS and server side, without having to get in my way. So if he wants to redesign the site then he just changes the templates, rather than having to troll through my PHP code, the same could be said if I want to redo the back-end of the site. Have a read of this[^].

                  If at first you don't succeed, you're not Chuck Norris.

                  C 1 Reply Last reply
                  0
                  • F fly904

                    cjoki wrote:

                    This style of code allows me to separate PHP and HTML from css and js.

                    How is $pg = "\n"; separating the two? You include the HTML IN your PHP. CSS and JS can be separated from HTML into several separate files (using the link tags and the script src attribute, respectively), rather than being scripted in, so to say. I prefer having separate files. When separating JS and CSS there are always certain bits which have to overlap such ass the class attributes. Smarty does the same for PHP and HTML. This is a very basic example of using Smarty. index.php

                    <?
                    include('Smarty.class.php');
                    $Smarty = new Smarty();
                    $Smarty->assign( 'message' , 'Smarty Rocks!!!' );
                    $Smarty->display( 'index.tpl' );
                    ?>

                    index.tpl

                    Smarty Example
                    
                    {$message}
                    

                    cjoki wrote:

                    Template engines just add another layer that IMHO is not needed and does little more than complicate a design and add to the learning.

                    After looking at the above code, you can't say that it's not insanely simple.

                    cjoki wrote:

                    and why are you asking me to look into a template system when your concern is performance?

                    Quite simply, I don't actually do the website design (I'm colour blind), I just worry about the functionality. The designer I hire designs the site and then writes the HTML and CSS and leaves me to do all the JS and server side, without having to get in my way. So if he wants to redesign the site then he just changes the templates, rather than having to troll through my PHP code, the same could be said if I want to redo the back-end of the site. Have a read of this[^].

                    If at first you don't succeed, you're not Chuck Norris.

                    C Offline
                    C Offline
                    cjoki
                    wrote on last edited by
                    #9

                    I could have stated this better, but I meant php and html, then js, and then css are seperated and controlled by code logic. I do entire projects, including layout and design, programing and db design. I have no need to separate the page further, than is needed by css and js, or even to keep the code more manageable. Also if you know php you can easily maintain my code. There is no need to learn additional language rules required by a template engine. My file structure is also way more simplistic than a template engine. I do not have to hunt a templates code to find where they buried a code fragment. ...and I counter your simple smarty code with an even simpler php code that only requires 1 file and will run..much...much faster!

                    <?php
                    $msg = "Smarty What...?";

                    /* I know you like the echos... ;) */

                    echo "";
                    echo "";
                    echo " PHP Example";
                    echo "";
                    echo "";
                    echo "<h1>".$msg."</h1>";
                    echo "";
                    echo "";
                    ?>

                    I sure we can go tit for tat, but this has gone way of topic from the original post. My reply was not meant to start a flame war, but to offer another option to the author.

                    F 1 Reply Last reply
                    0
                    • C cjoki

                      I could have stated this better, but I meant php and html, then js, and then css are seperated and controlled by code logic. I do entire projects, including layout and design, programing and db design. I have no need to separate the page further, than is needed by css and js, or even to keep the code more manageable. Also if you know php you can easily maintain my code. There is no need to learn additional language rules required by a template engine. My file structure is also way more simplistic than a template engine. I do not have to hunt a templates code to find where they buried a code fragment. ...and I counter your simple smarty code with an even simpler php code that only requires 1 file and will run..much...much faster!

                      <?php
                      $msg = "Smarty What...?";

                      /* I know you like the echos... ;) */

                      echo "";
                      echo "";
                      echo " PHP Example";
                      echo "";
                      echo "";
                      echo "<h1>".$msg."</h1>";
                      echo "";
                      echo "";
                      ?>

                      I sure we can go tit for tat, but this has gone way of topic from the original post. My reply was not meant to start a flame war, but to offer another option to the author.

                      F Offline
                      F Offline
                      fly904
                      wrote on last edited by
                      #10

                      I'm sorry, but I only echoed the variable, NOT the rest.

                      <?
                      $msg = 'Smarty is cool';
                      ?>

                      PHP Example
                      
                      <h1><? echo $msg; ?></h1>
                      
                      
                      
                      <h1><?=$msg?></h1>
                      

                      No unnecessary processing wasted by unnecessary echoing, holding all the output in memory or wasted time compiling strings using " instead of '. Which is what I said in the first place. Template engines generate it all for you, quite efficiently. They are only sluggish when compiling a template for the first time. I have to admit that it did take me a very long time (almost 5 years) to convert.

                      cjoki wrote:

                      My file structure is also way more simplistic than a template engine. I do not have to hunt a templates code to find where they buried a code fragment.

                      Using the template engine doesn't make my file system any more complicated:

                      /
                      classes/
                      logs/
                      smarty/
                      templates/
                      templates_c/ // Where the template is compiled to.
                      htdocs/
                      css/
                      js/
                      img/

                      Enough of the flame war. I call a truce, apologies.

                      cjoki wrote:

                      With echo once something is echoed its gone to the browser.

                      Have a look at Output Buffering[^]. It's quicker than both of our methods. I was going to talk about it when talking about the header redirection, but it's too advanced for what he is doing. I'm thinking about writing a couple of articles (when I have time) about OOP in PHP. It'll be more appropriate to continue the discussion then.

                      If at first you don't succeed, you're not Chuck Norris.

                      C 1 Reply Last reply
                      0
                      • F fly904

                        I'm sorry, but I only echoed the variable, NOT the rest.

                        <?
                        $msg = 'Smarty is cool';
                        ?>

                        PHP Example
                        
                        <h1><? echo $msg; ?></h1>
                        
                        
                        
                        <h1><?=$msg?></h1>
                        

                        No unnecessary processing wasted by unnecessary echoing, holding all the output in memory or wasted time compiling strings using " instead of '. Which is what I said in the first place. Template engines generate it all for you, quite efficiently. They are only sluggish when compiling a template for the first time. I have to admit that it did take me a very long time (almost 5 years) to convert.

                        cjoki wrote:

                        My file structure is also way more simplistic than a template engine. I do not have to hunt a templates code to find where they buried a code fragment.

                        Using the template engine doesn't make my file system any more complicated:

                        /
                        classes/
                        logs/
                        smarty/
                        templates/
                        templates_c/ // Where the template is compiled to.
                        htdocs/
                        css/
                        js/
                        img/

                        Enough of the flame war. I call a truce, apologies.

                        cjoki wrote:

                        With echo once something is echoed its gone to the browser.

                        Have a look at Output Buffering[^]. It's quicker than both of our methods. I was going to talk about it when talking about the header redirection, but it's too advanced for what he is doing. I'm thinking about writing a couple of articles (when I have time) about OOP in PHP. It'll be more appropriate to continue the discussion then.

                        If at first you don't succeed, you're not Chuck Norris.

                        C Offline
                        C Offline
                        cjoki
                        wrote on last edited by
                        #11

                        I never write broken HTML, large projects to easily become a mess to read. And honestly I have never had an issue with performance. But I would be interested in your article when it is ready... I am familiar with Output Buffering, but have not used it all that much, although maybe I should. Currently I am updating my js knowledge to the current standards of code separation...(i.e.: removing things like ...to and js adding the event logic at the end page load.). I am also contemplating an issue of external (multiple) css file loads vs style tags with css loaded based on page need using php code logic. I suspect in projects with a large number css rules can be refined to reduce the file size (sent to the browser) by send only the needed rules without the need for a larger number of file reads (split css files). But this flies in the face of separation of style from html...and I need to test, well I first need to figure out how I could test. All the best cjoki

                        modified on Monday, October 12, 2009 5:12 PM

                        F 1 Reply Last reply
                        0
                        • C cjoki

                          I never write broken HTML, large projects to easily become a mess to read. And honestly I have never had an issue with performance. But I would be interested in your article when it is ready... I am familiar with Output Buffering, but have not used it all that much, although maybe I should. Currently I am updating my js knowledge to the current standards of code separation...(i.e.: removing things like ...to and js adding the event logic at the end page load.). I am also contemplating an issue of external (multiple) css file loads vs style tags with css loaded based on page need using php code logic. I suspect in projects with a large number css rules can be refined to reduce the file size (sent to the browser) by send only the needed rules without the need for a larger number of file reads (split css files). But this flies in the face of separation of style from html...and I need to test, well I first need to figure out how I could test. All the best cjoki

                          modified on Monday, October 12, 2009 5:12 PM

                          F Offline
                          F Offline
                          fly904
                          wrote on last edited by
                          #12

                          cjoki wrote:

                          Currently I am updating my js knowledge to the current standards of code separation...(i.e.: removing things like ...to and js adding the event logic at the end page load.).

                          You get a thumbs up :thumbsup: That's the kind of thing I enjoy doing. Have a look at using jQuery. jQuery could do what you want with a single line.

                          $(document).ready(function() {
                          $('#btn').click(some_function);
                          });

                          (Added new lines to make it more readable.)

                          cjoki wrote:

                          I am also contemplating an issue of external (multiple) css file loads vs style tags with css loaded based on page need using php code logic. I suspect in projects with a large number css rules can be refined to reduce the file size (sent to the browser) by send only the needed rules without the need for a larger number of file reads (split css files). But this flies in the face of separation of style from html...and I need to test, well I first need to figure out how I could test.

                          I was playing around with this the other day. For performance, minimizing your js files, using a tool like this[^], greatly improves page load times. The same applies for css file. Loading multiple small css files is slower than loading one big css file. There is a css compressor[^] as well. Note: It's only worth minimizing files on release versions. A good tool for testing page load time is the Firebug addon[^] for Firefox. It has a tab called 'NET' which shows you how long everything takes to load. It also debugs JS which is very useful and shows the DOM while JS is changing it. Hope that helps :)

                          If at first you don't succeed, you're not Chuck Norris.

                          C 1 Reply Last reply
                          0
                          • F fly904

                            cjoki wrote:

                            Currently I am updating my js knowledge to the current standards of code separation...(i.e.: removing things like ...to and js adding the event logic at the end page load.).

                            You get a thumbs up :thumbsup: That's the kind of thing I enjoy doing. Have a look at using jQuery. jQuery could do what you want with a single line.

                            $(document).ready(function() {
                            $('#btn').click(some_function);
                            });

                            (Added new lines to make it more readable.)

                            cjoki wrote:

                            I am also contemplating an issue of external (multiple) css file loads vs style tags with css loaded based on page need using php code logic. I suspect in projects with a large number css rules can be refined to reduce the file size (sent to the browser) by send only the needed rules without the need for a larger number of file reads (split css files). But this flies in the face of separation of style from html...and I need to test, well I first need to figure out how I could test.

                            I was playing around with this the other day. For performance, minimizing your js files, using a tool like this[^], greatly improves page load times. The same applies for css file. Loading multiple small css files is slower than loading one big css file. There is a css compressor[^] as well. Note: It's only worth minimizing files on release versions. A good tool for testing page load time is the Firebug addon[^] for Firefox. It has a tab called 'NET' which shows you how long everything takes to load. It also debugs JS which is very useful and shows the DOM while JS is changing it. Hope that helps :)

                            If at first you don't succeed, you're not Chuck Norris.

                            C Offline
                            C Offline
                            cjoki
                            wrote on last edited by
                            #13

                            yeah, I keep forgetting about firebug's Net tab for download times, I tend to use it when debugging ajax calls. I also found a tool from yahoo, called Y!Slow that is a firebug plug-in. I have yet to play with it much but it also shows performance slow downs and offers corrective tips. On the css, my question comes down to 1) multiple css files, 2) one big css file or 3) adding css rules to a style tag in the page by way of code logic. Your reply confirms my gut feeling about a large number of small css files (is this true if the files are only added to the servers output on the fly and as needed?), but then there is the 1 large file vs the dynamic css rules in a style tag. With 1 large file there is no control over the css output...that I can think of. But the flow goes; page is requested by browser and then the css is request after the page download completes. The code added to the pages style tag, can use some simple logic rules to minimize the amount of css rules added to the page, thus making it small (vs the entire css file) and would not require a secondary file request for a separate css file (the 1 large css file). It would seam logical that the dynamic css rules added to the page will be the better performance solution. Of course I am not including compressed css files which is another performance question. JQuery, I have avoided...no real reason, but I have. I am still hand coding JavaScript.

                            F 1 Reply Last reply
                            0
                            • C cjoki

                              yeah, I keep forgetting about firebug's Net tab for download times, I tend to use it when debugging ajax calls. I also found a tool from yahoo, called Y!Slow that is a firebug plug-in. I have yet to play with it much but it also shows performance slow downs and offers corrective tips. On the css, my question comes down to 1) multiple css files, 2) one big css file or 3) adding css rules to a style tag in the page by way of code logic. Your reply confirms my gut feeling about a large number of small css files (is this true if the files are only added to the servers output on the fly and as needed?), but then there is the 1 large file vs the dynamic css rules in a style tag. With 1 large file there is no control over the css output...that I can think of. But the flow goes; page is requested by browser and then the css is request after the page download completes. The code added to the pages style tag, can use some simple logic rules to minimize the amount of css rules added to the page, thus making it small (vs the entire css file) and would not require a secondary file request for a separate css file (the 1 large css file). It would seam logical that the dynamic css rules added to the page will be the better performance solution. Of course I am not including compressed css files which is another performance question. JQuery, I have avoided...no real reason, but I have. I am still hand coding JavaScript.

                              F Offline
                              F Offline
                              fly904
                              wrote on last edited by
                              #14

                              The method I've used recently is to use one big css file. The browser normally caches it, so the only real performance loss will be the first page load. I went through a phase of having a separate specific css file for every page, and a global one included on each page. It was annoying, to say the least, worse to maintain than templates. Plus each page had to cache a separate page which slowed the load time. I tend to cache as many things as I can as it produces quicker load times and doesn't take up as much precious bandwidth :)

                              If at first you don't succeed, you're not Chuck Norris.

                              C 1 Reply Last reply
                              0
                              • F fly904

                                The method I've used recently is to use one big css file. The browser normally caches it, so the only real performance loss will be the first page load. I went through a phase of having a separate specific css file for every page, and a global one included on each page. It was annoying, to say the least, worse to maintain than templates. Plus each page had to cache a separate page which slowed the load time. I tend to cache as many things as I can as it produces quicker load times and doesn't take up as much precious bandwidth :)

                                If at first you don't succeed, you're not Chuck Norris.

                                C Offline
                                C Offline
                                cjoki
                                wrote on last edited by
                                #15

                                good point and well taken :)

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups