Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2005
    Location
    Ontario, Canada
    Posts
    1,750

    Arrow How to write good PHP code in dreamweaver CS4?

    Here's the thing. I'm using Dreamweaver CS4 to develop a website. In the past my php files would look like this:
    Code:
    <?php
        echo '<h1>Welcome ' . $name . '</h1>';
    ?>
    This caused problems in the design view because all I would see would be the php icon to let me know I have some script in this location and that is it.

    Then I started to realize that I should write my code like this:
    Code:
    <h1>Welcome <?php echo $name; ?></h1>
    This second way of writing php code is a lot easier to work with in Dreamweaver since in the design view I can see the formatted h1 tag with the word 'Welcome' beside it (followed by the php icon). I can still apply all the standard CSS rules inside Dreamweaver and the design view updates nicely.

    So now here is my issue. What is the best way to handle if statements? Say I want to toggle between <h1>Welcome $user</h1> and <h2>Good bye</h2> BUT I would like to still be able to format the CSS and see the results on the screen in design view. Is it possible?

    If I do the following, then I'm back to square one. I can't see my CSS rules applied in the design view since all I have is the PHP icon to show where the script is located:
    Code:
    <?php
       if( $welcome ) {
          echo '<h1>Welcome ' . $name . '</h1>';
       }
       else {
          echo '<h2>Good bye</h2>';
       }
    ?>
    C++, 3D OpenGL and Game Programming video tutorials:
    www.MarekKnows.com
    Play my free games: Ghost Toast, Zing, Jewel Thief

  2. #2
    Join Date
    Mar 2006
    Location
    South Australia
    Posts
    4,521
    In my opinion, there is no way to write good php code in Dreamweaver, especially when you are combining logic and page layout.

    What I would do is start looking at one of the PHP template engines, such as Smarty or Dwoo. That way you can separate your logic from your layout. Both scripts and templates become easier to read, and it also means on projects where you have, say, a designer and programmer, the programmer can work on the controllers while the designers work on templates.

    The Model/View/Controller design pattern works great with this type of separation.

  3. #3
    Join Date
    Jan 2010
    Location
    Aveiro - Portugal
    Posts
    181
    Hi!

    Don't really know if this helps... here's a bit of code from an Uni exercise I did once... I'm afraid I can't give you much more because at the time I was also involved in other areas and this was done without much attention...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Formulário Simples_v5</title>
    </head>
    
    <body>
    
    <?php 
    require_once("header.php");
    if(isset ($_POST["nome_amigo"]))
    {
    	$nome=$_POST["nome_amigo"];
    }
    else
    {
    $nome="nome";
    };
    if(isset ($_POST["email_amigo"]))
    {
    	$mail=$_POST["email_amigo"];
    }
    else
    {
    $mail="email";
    };
    echo "<form action=\"$PHP_SELF\" method=\"post\">
    <input value=\"$nome\" name=\"nome_amigo\" type=\"text\" />
    <input value=\"$mail\" name=\"email_amigo\" type=\"text\" />
    <input name=\"convite\" value=\"convidar\" type=\"submit\" />
    <input name=\"matar\" type=\"submit\" value=\"matar\" />
    </form>";
    if(isset ($_POST["convite"])&& ($_POST["nome_amigo"]!="")&& ($_POST["email_amigo"]!="")&& ($_POST["email_amigo"]!="email") && ($_POST["email_amigo"]!="nome"))
    {
    	echo "O meu nome é ".$_POST["nome_amigo"]." , e o meu e-mail é o ".$_POST["email_amigo"]."... me liga Ã*s".date('c')." vai?!";
    }
    else
    {
    	if(($_POST["nome_amigo"]!="nome")|| ($_POST["email_amigo"]!="email"))
    	{
    		echo "Deverá preencher todos os campos!";
    	};
    };
    if(isset ($_POST["matar"])&& ($_POST["nome_amigo"]!="")&& ($_POST["email_amigo"]!="")&& ($_POST["email_amigo"]!="email") && ($_POST["email_amigo"]!="nome"))
    {
    	echo "És tu ".$_POST["nome_amigo"]." ?! Com o e-mail  ".$_POST["email_amigo"]."... Prepare to die at".date('c');
    };
    require_once("footer.php");
    ?>
    </body>
    </html>
    I do remember that all the CSS was done in a separate file that was imported. And I do think that a friend of mine that knew alot more then me was always dealing with Divs and so on... by the way, much of the php was also written in another file... the html was kept clean and you would import stuff as needed...
    Last edited by js_dmacb; 05-09-2010 at 05:59 AM. Reason: add some info
    One step at a time... towards enlightenment! I hope...

    My Amazing Blog - Enter at your own risk...

  4. #4
    Join Date
    Mar 2005
    Location
    Ontario, Canada
    Posts
    1,750
    I posted my question on the adobe forms and I got an answer that works!

    Code:
    <?php if( $welcome ) { ?>
        <h1>Welcome <?php echo $name; ?></h1>
    <?php } else { ?>
        <h2>Good bye</h2>
    <?php } ?>
    I'm surprised that you can write php code this way!
    C++, 3D OpenGL and Game Programming video tutorials:
    www.MarekKnows.com
    Play my free games: Ghost Toast, Zing, Jewel Thief

  5. #5
    Join Date
    Mar 2006
    Location
    South Australia
    Posts
    4,521
    You can. I find it messy and error prone, but if it works for you...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •