How to create a PHP/HTML simple search via tagging/tags for website?

Phil

New member
Setup a table in your MySQL database called "search_tags" or something similar

make this table layout:
| id | page_name | tags | page_url |

(MySQL code at bottom of this answer)

then make 2 pages in PHP. search.php and results.php

search.php Source:

<form method="post" action="results.php">
<input type="text" name="search" value="Search" />Â*
<input type="submit" name="btn_search" value="Find It!" />
</form>

results.php Source:

<?php

$search_for = $_POST['search'];
//check for empty search field
if(empty($search_for) || $search_for == "search") {
$page_data = "Please enter a value to search for.<br><br><a href='search.php'>Back</a>";
}

//get pages from db
$get_pages = "SELECT page_name,tags,page_url FROM page_tags";
$get_pages_res = mysql_query($get_pages);

if(!$get_pages_res) {
die(mysql_error()); //if mysql query errors stop page and echo mysql error for debugging.
}

//check some pages where found
if(mysql_num_rows($get_pages_res) < 1) {
//no pages found
$page_data = "No pages in database.";
}

//create display string for displaying pages
$page_data = "Results for search: <b>".$search_for."</b>.";

//create while loop
$x=1; //stops while loop never ending
while($pages = mysql_fetch_array($get_pages_res) && $x <= mysql_num_rows($get_pages_res)) {
$page_name = stripslashes($pages['name']);
$page_tags = stripslashes($pages['tags']);
$page_url = stripslashes($pages['page_url']);

//check if search is in page name
if(stristr($page_name,$search_for)) {
//page title found - no need to continue to check tags
$page_data .= "<a href='".$page_url."'>".$page_name."</a>";
$xi=1;
} else {
$xi=0;
}

//check tags
if(isset($xi) && $xi == 1) {
//check tags
$page_tags_exploded = explode(',',$page_tags);
$fi=0;
while($fi=0) {
if($page_tags_exploded[$fi] == $search_for) {
//found in tags - stop while loop no need to search tags anymore
$fi=1;
//add to display string
$page_data .= "<a href='".$page_url."'>".$page_name."</a>";
}
}
}
$x++; //increment while offset
}

//display $page_data
echo $page_data;

?>

be sure to seperate ALL your tags with commas (,) otherwize the script will asume the tag(s) not seperated by comma's are just one tag. If you only have one tag, then you dont need the comma.

MySQL code:
CREATE TABLE IF NOT EXISTS `search_tags` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`page_name` varchar(75) NOT NULL,
`tags` text NOT NULL,
`page_url` varchar(150) NOT NULL,
PRIMARY KEY (`id`)
);


hope this helps! and hope it works!

if any of that
 
Hi all!

I have been searching without much success, what I am looking for is a way for each web page to be tagged with a few key words. Then some kind of search engine will pick up on these tags when performing a search, it sounds simple to me but I can't find anything like it; all I can find are search engines that do all the tagging/indexing for you.

My web server can use MYSQL and I cannot afford to pay so it would have to be free.

Thanks for reading!
Ideally there would be a box on each page to enter some words into to search the website.

With regards to tagging, I would like for example, at the bottom of the page some tags like:

<-- BeginTags-->
review funny rabbits comedy
<-- EndTags -->

The search engine will look thorugh each page for words inside the <--begin--> and <--end--> tags and display pages that match some or all of the tags.

Is that a bit more clear =)?
 
Back
Top