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:

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  4. <title>Search</title>
  5. </head>
  6.  
  7. </body>
  8. </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.

HTML:
  1. <form id="search" action="search.php?search" method="post">
  2. Keyword: <input type="text" name="search" size="30" class="text" /><br />
  3. <input type="submit" name="submit" value="Search" />
  4. </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).

PHP:
  1. include_once("mysql_connect.php");

Now error checking...

PHP:
  1. if(isset($_POST['submit'])){
  2. $problem = FALSE;
  3.  
  4. if(empty($_POST['search'])){
  5. $problem = TRUE;
  6. echo "<p style=\"color:#FF0000;\">Please enter a search term!</p>";
  7. }

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.

PHP:
  1. if(!$problem){
  2. $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.

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

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

PHP:
  1. if($result && $result2){

If the two mysql queries have run...

PHP:
  1. echo "<h1>found " . mysql_num_rows($result) . " results</h1><br />";
  2. echo "<ul>";
  3. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  4. $view = 'page.php?id=' . $row['id'];
  5.  
  6. echo '<li>' . $row['id'] . '&nbsp;-&nbsp;<a href="' . $view . '">' . $row['title'] . '</a>&nbsp;-&nbsp;' . $row['author'] . '</li>';
  7. }
  8. echo "</ul><br />
  9. <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.

PHP:
  1. }
  2. else
  3. {
  4. echo "No posts matched your query";
  5. }
  6. }
  7. }
  8. else
  9. {
  10. ?>

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:
  1. <?php
  2. }
  3. ?>

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:

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  4. <title>View</title>
  5. </head>
  6.  
  7. </body>
  8. </html>

Now the very simple php code:

PHP:
  1. <?php
  2.       include('mysql_connect.php');
  3.       if ((isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {
  4.       $id = $_GET['id'];
  5.       } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) {
  6.       $id = $_POST['id'];
  7.       } else {
  8.       echo 'Please choose a news post to view.';
  9.       exit();
  10.       }
  11.       $query = "SELECT title, post, author, DATE_FORMAT(date, '%M %d, %Y') as date FROM news_posts WHERE id='$id'";
  12.       $result = mysql_query($query);
  13.       if ($result) {
  14.       $row = mysql_fetch_array($result, MYSQL_ASSOC);
  15.       echo '<div class="news">' . $row['title'] . '<br /><hr />
  16.       ' . $row['post'] . '<br /><hr />
  17.       Posted by : <b>' . $row['author'] . '</b> on ' . $row['date'] . '</div><br />';
  18.       echo "<h1>Comments</h1>";
  19.       include("comments.php");
  20.       }
  21. ?>

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

PHP:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <link rel="stylesheet" type="text/css" href="css/default.css" />
  6. <title>Search</title>
  7. </head>
  8.  
  9. <body>
  10. <?php
  11. include_once("mysql_connect.php");
  12.  
  13. if(isset($_POST['submit'])){
  14. $problem = FALSE;
  15.  
  16. if(empty($_POST['search'])){
  17. $problem = TRUE;
  18. echo "<p style=\"color:#FF0000;\">Please enter a search term!</p>";
  19. }
  20.  
  21. if(!$problem){
  22. $text = $_POST['search'];
  23.  
  24. $query = "SELECT * from news_posts WHERE title LIKE '%$text%' OR post LIKE '%$text%' OR author LIKE '%$text%'";
  25. $result = @mysql_query($query);
  26. $query2 = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY id DESC limit 15";
  27. $result2 = @mysql_query($query);
  28.  
  29. if($result && $result2){
  30. echo "<h1>found " . mysql_num_rows($result) . "</h1><br />";
  31. echo "<ul>";
  32. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  33. $view = 'page.php?id=' . $row['id'];
  34.  
  35. echo '<li>' . $row['id'] . '&nbsp;-&nbsp;<a href="' . $view . '">' . $row['title'] . '</a>&nbsp;-&nbsp;' . $row['author'] . '</li>';
  36. }
  37. echo "</ul><br />
  38. <a href=\"search.php\">Search Again?</a>";
  39. }
  40. else
  41. {
  42. echo "No posts matched your query";
  43. }
  44. }
  45. }
  46. else
  47. {
  48. ?>
  49. <form id="search" action="search.php?search" method="post">
  50. Keyword: <input type="text" name="search" size="30" class="text" /><br />
  51. <input type="submit" name="submit" value="Search" />
  52. </form>
  53. <?php
  54. }
  55. ?>
  56. </body>
  57. </html>

and page.php:

PHP:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>View</title>
  6. </head>
  7.  
  8. <body>
  9. <?php
  10.       include('mysql_connect.php');
  11.       if ((isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {
  12.       $id = $_GET['id'];
  13.       } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) {
  14.       $id = $_POST['id'];
  15.       } else {
  16.       echo 'Please choose a news post to view.';
  17.       exit();
  18.       }
  19.       $query = "SELECT title, post, author, DATE_FORMAT(date, '%M %d, %Y') as date FROM news_posts WHERE id='$id'";
  20.       $result = mysql_query($query);
  21.       if ($result) {
  22.       $row = mysql_fetch_array($result, MYSQL_ASSOC);
  23.       echo '<div class="news">' . $row['title'] . '<br /><hr />
  24.       ' . $row['post'] . '<br /><hr />
  25.       Posted by : <b>' . $row['author'] . '</b> on ' . $row['date'] . '</div><br />';
  26.       echo "<h1>Comments</h1>";
  27.       include("comments.php");
  28.       }
  29. ?>
  30. </body>
  31. </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:

PHP:
  1. echo $row['title'];

or something along those lines, and replace with:

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

Share and enjoy our tutorial : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • StumbleUpon
  • del.icio.us
  • Fark
  • Furl
  • Slashdot
  • Netscape
  • BlinkList
  • Ma.gnolia
  • Reddit
  • Technorati