Log in

View Full Version : Adding HTML to My PHP Page



tomyknoker
03-14-2007, 05:21 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>

codeexploiter
03-14-2007, 07:38 AM
have you tried this?



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

Twey
03-14-2007, 08:47 AM
Or:
?>
<div id="navigation">
<?php
include('nav.php');
?>
</div>
<?phpI always prefer the latter, as it is very difficult to maintain nicely-formatted code when using echo statements.

mburt
03-14-2007, 10:28 AM
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:

<title><?php echo strtoupper(substr($_GET["p"],0,1).substr($_GET["p"],1,strlen($_GET["p"])); ?></title>

Twey
03-14-2007, 04:42 PM
Likewise, you can put HTML just about anywhere in PHP. A block of HTML is roughly equivalent to an echo statement.
<?php if(user_is_logged_in()) { ?>
<a href="logout.php">Log Out</a>
<?php } ?>is fine, as is:
<?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
<?php something(); ?>with
<? something(); ?>and
<?php echo something(); ?>with
<?=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.

mburt
03-14-2007, 06:21 PM
Yeah, I knew the <? works, but why does everyone use <?php ?

thetestingsite
03-14-2007, 06:32 PM
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.

tomyknoker
03-14-2007, 10:21 PM
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-14-2007, 10:26 PM
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>

thetestingsite
03-14-2007, 10:45 PM
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.

djr33
03-14-2007, 10:58 PM
<?php is also only for PHP whereas <? can be used for other languages, I believe.
<&#37; also works in some cases for PHP I think, though that's the same for ASP, etc.

Twey, can you explain a bit more about:
<?=something()?>?

thetestingsite
03-14-2007, 11:20 PM
<?php is also only for PHP whereas <? can be used for other languages, I believe.
<&#37; also works in some cases for PHP I think, though that's the same for ASP, etc.


Well; as I said before, <? and (I haven't mentioned this yet) <% works for php if it is enabled in the php.ini file. Otherwise, those above mentioned tags will not work (with PHP that is).



can you explain a bit more about:
<?=something()?>?



<?="This is a test" ?>


is the same as



<?php
echo "This is a test"
?>


Hope this helps.

djr33
03-15-2007, 03:30 AM
I get that... just like Twey's example.

But what's up with the '='? Does it set the implied output (beyond the <?) to the value? Or does it change the meaning of the <?= tag? Would it work with <?php=, etc.

thetestingsite
03-15-2007, 03:39 AM
Well, I just tested this:



<?php="This is a test";?>


and it didn't work.

Twey
03-15-2007, 05:47 PM
Does it set the implied output (beyond the <?) to the value?No, it's just a character used in the opening tag. It has no syntactical meaning, any more than <? is related to numerical comparisons and conditionals.
Would it work with <?php=, etc.No.