...

View Full Version : Adding HTML to My PHP Page



tomyknoker
03-14-2007, 07:20 AM
Hi all,

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



<?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
<div ="navigation">//navigation
include("nav.php");</div>

Fou-Lu
03-14-2007, 07:31 AM
Yep, it is that simple, but you need to actually output the 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:


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!

aedrin
03-14-2007, 04:43 PM
Always surround PHP code with the <?php and ?> delimiters.



<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.

tomyknoker
03-15-2007, 12:22 AM
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?

tomyknoker
03-15-2007, 12:26 AM
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?
<!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>

the-dream
03-15-2007, 12:35 AM
The way above is perfecly fine but sometimes it is nessesary to start your pages with:


<?php

// Stuff Here

?>

tomyknoker
03-15-2007, 02:16 AM
Always surround PHP code with the <?php and ?> delimiters.



<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

/* now grab the first row
* and start the table */


// Begin Navigation



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum