Trouble with "Build your own website with PHP and MySQL" Chapter 7, searching the DB

Hi, I’ve been really drawn into PHP and MySQL these couple of days and borrowed a copy of Build your own website with PHP and MySQL (fantastic work Kevin!), which I’ve been learning mostly from. I’ve ran into my first wall, trying to get something to work. Here’s the thing, in Chapter 7 it teaches you how to create a simple search feature for the jokes database. However to do that you’ll need to create 3 files: searchform.html, resultdisplay.html and index.php. I want to have only 1 html page (the searchform is combined with the results) and the index.php page. I’ll probably get into trouble if I paste any of the coding from Chapter 7 here, so if anyone else also has the book please help!

Hey,

I don’t have the book, but you could just make a file called index.php

Put the page you’re on in the form action, ie: <form action=“index.php?search” method=“post”>

So I guess it’d be kinda like…


<?php

$showForm = true;
if (isset($_REQUEST['search'])) {
  // 
  // Process search request, however they do it.
  //
  //$resultData = // The MySQL Result stuff

  $showResults = true;
  $showForm = false;
}

?>

<html>
<body>

<?php if ($showForm == true): ?>

<form action="index.php?search" method="post">
  // Your search stuff 
</form>

<?php endif; ?>

<?php if ($showResults== true): ?>

// Display your results from DB up top..

<?php endif; ?>

</body>
</html>

Thanks for the reply JREAM, I’ll look over the actions section and over your code tonight.

I’ll send you a PM as well.

I also have the book somewhere which is probably one of the first versions.
Anyway I don’t remember exactly but the elements you described:
searchform.html, resultdisplay.html and index.php
can be easily combined into one PHP page.

To give you an idea the theory can be like this:


<?php

//if the user has posted the form with the search value input
if(isset($_POST['searchvalue']))
{	
[INDENT]//Code to search the Database[/INDENT]
[INDENT]//Code to display the results[/INDENT]
	
}
	
//if the user didn't posted anything 
else
{
[INDENT]//give user the search-form[/INDENT]	
}
?>

Bump, still having problems… I’ll put an excerpt of the code


// Display search form
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
include 'searchform.html.php';
?>

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 'displayresults.html.php';
exit();
}