advertisement

Hi, this is an extension for “News System with Comments“. If you haven’t taken the tutorial already, I seriously recommend you do because we will be using the table created in that tutorial.

Hopefully in this tutorial I will teach you how to search a database with PHP. On with the tutorial…

First thing we need to do is create a new php file, so go into your text editor of choice and create a new file. Add the basic html page layout:

<!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>Search</title>
</head>

<body>
</body>
</html>

Save this file as search.php.

Now to add the form. For the form, we will have a field for the keyword(s) and a submit button.

<form id="search" action="search.php?search" method="post">
Keyword: <input type="text" name="search" size="30" class="text" /><br />
<input type="submit" name="submit" value="Search" />
</form>

Put this in between the two body tags of your page.

Now onto the php…
NOTE : PLACE ALL PHP CODE ABOVE THE FORM BUT STILL INSIDE THE BODY TAG

We need to open php and write 1 line to include the database connection (created in News system with Comments tutorial, go there if you don’t have one).

include_once("mysql_connect.php");

Now error checking…

if(isset($_POST['submit'])){
$problem = FALSE;

if(empty($_POST['search'])){
$problem = TRUE;
echo "<p style=\"color:#FF0000;\">Please enter a search term!</p>";
}

What the code above does is checks if the submit button is pressed, if it is, run the code. The second line creates a variable which will help errors checking. The next 4 lines check if the text field is empty, if it is the problem variable will return true and it will echo an error.

if(!$problem){
$text = $_POST['search'];

If there is no problems it will run the code. The second line creates a variable which is just a shortening term really but it is equal to the search field in the form.

$query = "SELECT * from news_posts WHERE title LIKE '%$text%' OR post LIKE '%$text%' OR author LIKE '%$text%'";
$result = @mysql_query($query);
$query2 = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY id DESC limit 15";
$result2 = @mysql_query($query);

These are two mysql queries that will be used for finding content and linking to them.

if($result && $result2){

If the two mysql queries have run…

echo "<h1>found " . mysql_num_rows($result) . " results</h1><br />";
echo "<ul>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$view = 'page.php?id=' . $row['id'];

echo '<li>' . $row['id'] . '&nbsp;-&nbsp;<a href="' . $view . '">' . $row['title'] . '</a>&nbsp;-&nbsp;' . $row['author'] . '</li>';
}
echo "</ul><br />
<a href=\"search.php\">Search Again?</a>";

The first line echo’s how many results have been found. The second line starts a list (all the results will be in a list, and it needs to start here). 3rd line creates a loop with a variable in it which gets an array of results from the database. 4th line creates a link to the content (this page will be created later). Next it echo’s a result in a list. It will return something like:
[list] ID – [link] Content Title [/link] – [author] [/list]
Next it closes the loop and ends the list. Lastly it provides a link so you can search again.

}
else
{
echo "No posts matched your query";
}
}
}
else
{
?>

This is just closing all the ifs (pretty self-explanatory).

Now after the html form (just before it ends the body tag) put this:

<?php
}
?>

Now in the search form we had links to the content, now we need to create the file that displays this, call this file page.php:

Basic html page format:

<!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>View</title>
</head>

<body>
</body>
</html>

Now the very simple php code:

<?php
      include('mysql_connect.php');
      if ((isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {
      $id = $_GET['id'];
      } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) {
      $id = $_POST['id'];
      } else {
      echo 'Please choose a news post to view.';
      exit();
      }
	  $query = "SELECT title, post, author, DATE_FORMAT(date, '%M %d, %Y') as date FROM news_posts WHERE id='$id'";
      $result = mysql_query($query);
      if ($result) {
	  $row = mysql_fetch_array($result, MYSQL_ASSOC);
	  echo '<div class="news">' . $row['title'] . '<br /><hr />
	  ' . $row['post'] . '<br /><hr />
	  Posted by : <b>' . $row['author'] . '</b> on ' . $row['date'] . '</div><br />';
	  echo "<h1>Comments</h1>";
	  include("comments.php");
	  }
?>  

This basically does exactly what the homepage does but for only 1 specific ID. And it inlcudes the comments page.

So… the full code for the two pages we created is:

search.php

<!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" />
<link rel="stylesheet" type="text/css" href="css/default.css" />
<title>Search</title>
</head>

<body>
<?php
include_once("mysql_connect.php");

if(isset($_POST['submit'])){
$problem = FALSE;

if(empty($_POST['search'])){
$problem = TRUE;
echo "<p style=\"color:#FF0000;\">Please enter a search term!</p>";
}

if(!$problem){
$text = $_POST['search'];

$query = "SELECT * from news_posts WHERE title LIKE '%$text%' OR post LIKE '%$text%' OR author LIKE '%$text%'";
$result = @mysql_query($query);
$query2 = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY id DESC limit 15";
$result2 = @mysql_query($query);

if($result && $result2){
echo "<h1>found " . mysql_num_rows($result) . "</h1><br />";
echo "<ul>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$view = 'page.php?id=' . $row['id'];

echo '<li>' . $row['id'] . '&nbsp;-&nbsp;<a href="' . $view . '">' . $row['title'] . '</a>&nbsp;-&nbsp;' . $row['author'] . '</li>';
}
echo "</ul><br />
<a href=\"search.php\">Search Again?</a>";
}
else
{
echo "No posts matched your query";
}
}
}
else
{
?>
<form id="search" action="search.php?search" method="post">
Keyword: <input type="text" name="search" size="30" class="text" /><br />
<input type="submit" name="submit" value="Search" />
</form>
<?php
}
?>
</body>
</html>

and page.php:

<!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>View</title>
</head>

<body>
<?php
      include('mysql_connect.php');
      if ((isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {
      $id = $_GET['id'];
      } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) {
      $id = $_POST['id'];
      } else {
      echo 'Please choose a news post to view.';
      exit();
      }
	  $query = "SELECT title, post, author, DATE_FORMAT(date, '%M %d, %Y') as date FROM news_posts WHERE id='$id'";
      $result = mysql_query($query);
      if ($result) {
	  $row = mysql_fetch_array($result, MYSQL_ASSOC);
	  echo '<div class="news">' . $row['title'] . '<br /><hr />
	  ' . $row['post'] . '<br /><hr />
	  Posted by : <b>' . $row['author'] . '</b> on ' . $row['date'] . '</div><br />';
	  echo "<h1>Comments</h1>";
	  include("comments.php");
	  }
?>
</body>
</html>

Thankyou for reading this tutorial, if I have made any errors or you need some help, feel free to comment.

Thanks,
mortisimus
http://www.flashdoom.net

EDIT: Permalinks:
On the front page of your cms, when it is listing the data for a post, find:

echo $row['title'];

or something along those lines, and replace with:

echo '<a href="page.php?id=' . $row['id'] . '">' . $row['title'] . '</a>';
advertisement