Extended - News System: Search Form
3,853 ViewsPHP Tutorials June 12th, 2007
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:
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.
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...
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%'";
-
$query2 = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY id DESC limit 15";
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...
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:
Now the very simple php code:
-
<?php
-
include('mysql_connect.php');
-
$id = $_GET['id'];
-
$id = $_POST['id'];
-
} else {
-
echo 'Please choose a news post to view.';
-
}
-
$query = "SELECT title, post, author, DATE_FORMAT(date, '%M %d, %Y') as date FROM news_posts WHERE id='$id'";
-
if ($result) {
-
' . $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");
-
-
$problem = FALSE;
-
-
$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%'";
-
$query2 = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY id DESC limit 15";
-
-
if($result && $result2){
-
echo "<ul>";
-
$view = 'page.php?id=' . $row['id'];
-
-
echo '<li>' . $row['id'] . ' - <a href="' . $view . '">' . $row['title'] . '</a> - ' . $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');
-
$id = $_GET['id'];
-
$id = $_POST['id'];
-
} else {
-
echo 'Please choose a news post to view.';
-
}
-
$query = "SELECT title, post, author, DATE_FORMAT(date, '%M %d, %Y') as date FROM news_posts WHERE id='$id'";
-
if ($result) {
-
' . $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:
or something along those lines, and replace with:

(15 votes, average: 4.8 out of 5)










June 12th, 2007 at 1:23 pm
[...] Search Form Share and enjoy our tutorial : These icons link to social bookmarking sites where readers can [...]
July 21st, 2007 at 8:57 pm
Thanks, once I finish with the news system I'll definitely move onto this.
July 23rd, 2007 at 9:13 am
You could also use the page.php file for a permalink, for example, on the front page you could make the titles of the posts into links and the link format would be something like http://www.site.com/page.php?id=2
I will update the tutorial to give te code for this.
August 4th, 2007 at 1:57 am
@mortisimus
If you could that would be great! I would love to have that!!! If you can that would be great! Even if you could leaver the new code in a comment or an email
aneveu{at}verizon(dot)net
if you can please e-mail me the new help please
Thank-you!!!
-Adam
August 8th, 2007 at 7:48 am
Done
June 12th, 2008 at 12:28 am
Hey Adam and mortisimus. How can i add permalinks to the News System with Comments?
June 12th, 2008 at 12:40 am
Forget about it, already got it now. thanks a lot for this tutorial!
How about making a second addition to the News System, an archive?
June 12th, 2008 at 2:03 am
Ok, i got a little archive now. It's valid and hopefully someone could find it usefull.
Here is the code:
*****************
Archive
' . $row['title'] . ' '.$row['sd'].'
Read more';
}
} else {
echo 'There are no news posts to display';
}
?>
*****************
First of all let's break this down into pieces.
Archive
Starting of with the normal Head content. Defining the Doctype and the title of your page.
' . $row['title'] . ' '.$row['sd'].'
Read more';
This is the part which you will see on your site. You have all the News-Headlines with Posting Date behind them and a "Read More" link beneath.
I think for the archive you need the "Search Addon" installed since we need the page.php. You can click on a headline to get redirected to the full news on a seperate page (For example "page.php?id=3").
}
} else {
echo 'There are no news posts to display';
}
The possibility of having no news to show in the archive is also given. You just define what will be the output if there are no news. Basicly this is the same as in news.php
?>
Closing the whole php and the whole File.
*****************
I hope this helped anyone out there since i really wanted to have an archive for this!
I would be really happy!
Thanks for reading and hopefully this addition will get a little post or so. Like this Search addon. Contact me at admin[at]janschuster.com if you want
June 12th, 2008 at 2:04 am
damn i forgot to put it in codes. could someone delete the post above? Thank you