Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Adding HTML to My PHP Page

  1. #1
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding HTML to My PHP Page

    Hi all,

    Forgive me if this is an obvious question, Say I have this page

    Code:
    <?php
    $cat = $_GET['cat'];
    
    /* connect to the mysql database and use a query to get the members info */
    
    include 'library/config.php';
    include 'library/opendb.php';
    
    //navigation
    include("nav.php");
    
    
    /* set the allowed order by columns */
    
    $default_sort = 'LastName';
    $allowed_order = array ('JoinDate', 'FirstName','LastName', 'loginDateTime');
    
    /* if order is not set, or it is not in the allowed
     * list, then set it to a default value. Otherwise, 
     * set it to what was passed in. */
    if (!isset ($_GET['order']) || 
        !in_array ($_GET['order'], $allowed_order)) {
        $order = $default_sort;
    } else {
        $order = $_GET['order'];
    }
    
    /* construct and run our query */
    $query = "SELECT * FROM tblmembers WHERE `MemberApproved`='$cat'   ORDER BY $order";
    
    $result = mysql_query ($query);
    
    /* make sure data was retrieved */
    $numrows = mysql_num_rows($result);
    if ($numrows == 0) {
        echo "No data to display!";
        exit;
    }
    
    /* now grab the first row and start the table */
    $row = mysql_fetch_assoc ($result);
    echo "<TABLE border=1>\n";
    echo "<TR>\n";
    foreach ($row as $heading=>$column) {
        /* check if the heading is in our allowed_order
         * array. If it is, hyperlink it so that we can
         * order by this column */
        echo "<TD><b>";
        if (in_array ($heading, $allowed_order)) {
            echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat\">$heading</a>";
        } else {
            echo $heading;
        }                
        echo "</b></TD>\n";
    }
    echo "</TR>\n";
    
    /* reset the $result set back to the first row and 
     * display the data */
    mysql_data_seek ($result, 0);
    while ($row = mysql_fetch_assoc ($result)) {
        echo "<TR>\n";
        foreach ($row as $column) {
            echo "<TD>$column</TD>\n";
        }
        echo "</TR>\n";
    }
    echo "</TABLE>\n";
    ?
    I want to add div tags around the nav.php, and then also around the table below, but when I do this it makes the display errors, is it not just as simple as adding
    Code:
    <div ="navigation">//navigation
    include("nav.php");</div>

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    have you tried this?

    Code:
    echo "<div id='navigation'>";
    include("nav.php");
    echo "</div>";

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Or:
    Code:
    ?>
      <div id="navigation">
        <?php
          include('nav.php');
        ?>
      </div>
    <?php
    I always prefer the latter, as it is very difficult to maintain nicely-formatted code when using echo statements.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Yes. What he said, and also: you can put PHP statements anywhere on the HTML page. Even in JavaScript, so if you wanted it on the same line, simply put it on the same line. Remember to use <?php and ?>.
    Example:
    Code:
    <title><?php echo strtoupper(substr($_GET["p"],0,1).substr($_GET["p"],1,strlen($_GET["p"])); ?></title>
    - Mike

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Likewise, you can put HTML just about anywhere in PHP. A block of HTML is roughly equivalent to an echo statement.
    Code:
    <?php if(user_is_logged_in()) { ?>
      <a href="logout.php">Log Out</a>
    <?php } ?>
    is fine, as is:
    Code:
    <?php while($paragraph = mysql_fetch_array($results)) { ?>
      <h3><?php echo $paragraph['title']; ?></h3>
      <p>
        <img src="<?php echo $paragraph['image']; ?>" alt="<?php echo $paragraph['image_alt']; ?>">
        <?php echo $paragraph['text']; ?>
      </p>
    <?php } ?>
    Note that if you have short tags enabled in your php.ini, you can replace
    Code:
    <?php something(); ?>
    with
    Code:
    <? something(); ?>
    and
    Code:
    <?php echo something(); ?>
    with
    Code:
    <?=something()?>
    However, since one can't be sure of another server's config, it's wise to avoid using these short forms for scripts designed to be run on servers other than one's own.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Yeah, I knew the <? works, but why does everyone use <?php ?
    - Mike

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    The <? only works if the short_open_tags is set to "On" in your php.ini file. <?php works always. That is why everyone uses it instead of the short tag (just to make sure that the code will run no matter what the ini settings are.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. #8
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So guys if I were to use CSS in my php pages, what would be the most correct and Web 2.0 accepted way? I know there are all the above ways but which is the technically the most correct?

  9. #9
    Join Date
    Apr 2006
    Posts
    584
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I also have noticed that people just start there pages with '<?php', Should all my php pages still look have these tags or is that incorrect?
    HTML 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=ISO-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    </body>
    </html>

  10. #10
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    The above is correct HTML. The only reason to start with "<?php" is if you have some php code at the beginning of your html document. Otherwise, start with that and then (when you start your php code) open it with <?php and close it with ?>.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

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