I’ve been learning some PHP and MySQL from a book that teaches you how create a simple database driven site. In the book’s examples, we’re creating a joke database that store author names, joke text, date and id. Progressing I’ve been taught how to use includes in my main controller, index.php. I’m stuck at a part where they tell me to create a search feature for the joke database, coding as follows:
This is the first part of the controller called ‘index.php’ all it does is display the search form.
// Display search form
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
include 'searchform.html.php'; //CHANGE 1
?>
The next part of the controller builds the SQL and then sends it to jokes.html.php, fairly simple… no problems here.
if (isset($_GET['action']) and $_GET['action'] == 'search')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
//Build SQL statement and output results into an array code here
}
include 'jokes.html.php'; //CHANGE 2
exit();
}
How would you modify the code above if the your searchform.html and jokes.html are just the single html file? I find it inconvenient using 2 files for searching.
My first attempt (I’ve merged searchform and jokes into “jokesearch.html.php”) was to include ‘jokesearch.html.php’ in CHANGE 1 and again in CHANGE 2, however that didn’t help… it just reloaded the page.
2nd attempt was to use header(‘Location: .’)… no luck here too it just reloaded.
EDIT: By popular demand, I’ll include the two html files.
searchform.html.php:
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/includes/helpers.inc.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Manage Jokes</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
</head>
<body>
<h1>Manage Jokes</h1>
<p><a href="?add">Add new joke</a></p>
<form action="" method="get">
<p>View jokes satisfying the following criteria:</p>
<div>
<label for="author">By author:</label>
<select name="author" id="author">
<option value="">Any author</option>
<?php foreach ($authors as $author): ?>
<option value="<?php htmlout($author['id']); ?>"><?php
htmlout($author['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="category">By category:</label>
<select name="category" id="category">
<option value="">Any category</option>
<?php foreach ($categories as $category): ?>
<option value="<?php htmlout($category['id']); ?>"><?php
htmlout($category['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="text">Containing text:</label>
<input type="text" name="text" id="text"/>
</div>
<div>
<input type="hidden" name="action" value="search"/>
<input type="submit" value="Search"/>
</div>
</form>
<p><a href="..">Return to JMS home</a></p>
</body>
</html>
jokes.html.php
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/includes/helpers.inc.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Manage Jokes: Search Results</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
</head>
<body>
<h1>Search Results</h1>
<?php if (isset($jokes)): ?>
<table>
<tr><th>Joke Text</th><th>Options</th></tr>
<?php foreach ($jokes as $joke): ?>
<tr valign="top">
<td><?php htmlout($joke['text']); ?></td>
<td>
<form action="?" method="post">
<div>
<input type="hidden" name="id" value="<?php
htmlout($joke['id']); ?>"/>
<input type="submit" name="action" value="Edit"/>
<input type="submit" name="action" value="Delete"/>
</div>
</form>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<p><a href="?">New search</a></p>
<p><a href="..">Return to JMS home</a></p>
</body>
</html>
From a MVC perspective your initial setup is the right way to go.
Your controller collects and processes data and sends it to views (your .html.php files).
It is good practice to separate defferent elements into different views. So a search box or search results go in a different view than the jokes.
Putting both logical different elements in one view file makes maintenance harder.
Regards,
Erwin Vrolijk
snow.nl