Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep, it is that simple, but you need to actually output the code:
    PHP Code:
    <?php

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

    ...
    ?>
    Simple as that, you just needed to use one of the output commands!

    edit
    Didn't notice you wanted the table part, its even easier since its already echoed:
    PHP Code:
    echo "<TABLE border=1>\n";
    // Change to:
    echo "<div id=\"tableID\"><TABLE border=1>\n"
    Notice the difference though, the table example uses double quotations to escape the use of double quotations, while the other does not. This is because of how they are treated, you cannot use single quotations with single quotes, and you cannot use doubles within doubles. Escaping will allow you to retain the quoations (and with the table example, its necessary due to the linebreak \n - it requires the double quotations)
    Hope thats what you're looking for!
    Last edited by Fou-Lu; 03-14-2007 at 06:35 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Always surround PHP code with the <?php and ?> delimiters.

    PHP Code:
    <div id="navigation">
    <?php
        
    include("nav.php");
    ?>
    </div>
    EDIT: I forgot to mention, avoid using /* */ style comments for single line comments. If you have to temporarily comment out a block of code, you'd either have to put // in front of every line, or temporarily undo all the comments.

    Just use // style comments for actual comments.

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?
    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>

  • #6
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    The way above is perfecly fine but sometimes it is nessesary to start your pages with:

    PHP Code:
    <?php

    // Stuff Here

    ?>

  • #7
    Regular Coder
    Join Date
    Mar 2006
    Posts
    459
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by aedrin View Post
    Always surround PHP code with the <?php and ?> delimiters.

    PHP Code:
    <div id="navigation">
    <?php
        
    include("nav.php");
    ?>
    </div>
    EDIT: I forgot to mention, avoid using /* */ style comments for single line comments. If you have to temporarily comment out a block of code, you'd either have to put // in front of every line, or temporarily undo all the comments.

    Just use // style comments for actual comments.
    Sorry so just to be clear like this
    PHP Code:
    /* now grab the first row 
    * and start the table */ 
    PHP Code:
    // Begin Navigation 


  •  

    Posting Permissions

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