A better way: Displaying XHTML through PHP
Is there an easier way to basically say "if $_GET = this then display this chunk of HTML, otherwise display this chunk of html?"
I have a page and only certain things should be shown depending on the value sent to the page. I'd hate to write all the XHTML code in PHP's print, printf, or echo functions with \" everywhere.
Re: A better way: Displaying XHTML through PHP
This works:
PHP Code:
<?php if($_GET['value'] == "a") { ?>
<a href="http://www.google.com">Type In standard HTML here</a>
<?php }else{ ?>
<table>
<tr>
<td>Type in a different set of HTML here</td>
</tr>
</table>
<?php } ?>
Re: A better way: Displaying XHTML through PHP
My current home page - which is only going to be up for a couple more weeks uses a system like this. http://www.sccode.com/
Basically you can use an included file based on a value sent in the query string. When you include a file, it switches from PHP mode to HTML mode, so your included file can contain HTML. This is the source code from my index.php page:
PHP Code:
<?php header ('Content-Type: text/html; charset=iso-8859-1'); ?>
<!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" xml:lang="en" lang="en">
<head>
<title>The Home of Random Code - Under Construction</title>
<link rel="stylesheet" type="text/css" href="css/main.css" />
</head>
<body>
<div class="main">
<div class="top">
<h2>Sccode.com</h2>
<table class="navbar">
<tr>
<td><a href="<?php link_self () ?>">Home</a></td>
<td><a href="<?php link_self ('location=articles') ?>">Articles</a></td>
<td><a href="<?php link_self ('location=examples') ?>">Examples</a></td>
<td><a href="<?php link_self ('location=projects') ?>">Projects</a></td>
<td><a href="<?php link_self ('location=links') ?>">Links</a></td>
<td><a href="<?php link_self ('location=random') ?>">Random</a></td>
<td><a href="<?php link_self ('location=contact') ?>">Contact Me</a></td>
</tr>
</table>
</div>
<?php
$location = (isset ($_GET['location'])?$_GET['location']:'main');
switch ($location) {
case 'random':
$includefile = 'random.php';
break;
case 'projects':
$includefile = 'projects.php';
break;
case 'articles':
$includefile = 'articles.php';
break;
case 'links':
$includefile = 'links.php';
break;
case 'main':
default:
$includefile = 'main.php';
}
@include ("include/$includefile");
?>
<div class="right" <?php if (! function_exists('left')): ?>style="width: 100%"<?php endif; ?>>
<?php right(); ?>
</div>
<?php if (function_exists('left')) left(); ?>
<div class="bottom"></div>
</div>
</body>
</html>
<?php
function link_self ($querystring = '') {
echo (htmlspecialchars ($_SERVER['PHP_SELF'] . ($querystring == ''?'':"?$querystring")));
}
?>
You can see that its easy to jump in and out of PHP mode whenever you have a large chunk of HTML. In my opinion, echo should only ever be used to ouput small peices of text. This is the reason PHP is HTML embedded, if you don't take advantage of this, you may as well use perl.
You can also use alternate VB like constructs in place of braces to make the output more readable. This is part of a template file I use:
PHP Code:
<div class="left">
<?php if (isset($this->left_nav)): ?>
<div class="leftitem">
<table class="leftnav">
<?php foreach ($this->left_nav as $left_item): ?>
<tr>
<td <?php if (isset($left_item['type'])): ?>
class="<?php echo($left_item['type']) ?>"
<?php endif;?>>
<a href="<?php echo($left_item['link']) ?>"
><?php echo ($left_item['text']) ?></a>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<?php endif; ?>