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
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