Hai, so I've made this little script that searches through my database of 'articles'
and returns based on if the search string matches words in the title of the article.
It does work, but I was wondering if any of you could spot anything that I should have
done differently. In terms of effeciency, easier to code or whatnot. My current database is not
big, but I might aswell learn how to be efficient right away.
Here is my code:
CODE
<?php
// SET NUMBER OF RESULTS
$numresults = 0;
// IF ACCESSED BY THE RAW FILE SEND THEM TO INDEX
if(!isset($_POST["search_submit"])) {
header("Location: index.php");
die();
} else {
// IF THE BOX DOES NOT CONTAIN ANYTHING
if(empty($_POST["search_string"])) {
echo 'Please enter something to search for.';
} else {
$searchsql = "SELECT * FROM blog_articles";
$searchresults = mysql_query($searchsql,$mysql_connect);
while($searchrow = mysql_fetch_array($searchresults)) {
// WHAT TO SEARCH FOR
$searchresult = stripos($searchrow["blog_title"],$_POST["search_string"]);
// CHECK IF THE STRING IS FOUND IN TITLES
if($searchresult !== false) {
// STRING WAS FOUND, ECHO LINK
echo '<h1><a href="index.php?p=article&article=';
echo $searchrow["blog_article_id"];
echo '">';
echo $searchrow["blog_title"];
echo '</a></h1>';
// ADD TO NUMBER OF RESULTS
$numresults = $numresults + 1;
}
}
// ECHO THE AMOUNT OF RESULTS
echo $numresults . ' posts found.';
}
}
mysql_close();
?>
// SET NUMBER OF RESULTS
$numresults = 0;
// IF ACCESSED BY THE RAW FILE SEND THEM TO INDEX
if(!isset($_POST["search_submit"])) {
header("Location: index.php");
die();
} else {
// IF THE BOX DOES NOT CONTAIN ANYTHING
if(empty($_POST["search_string"])) {
echo 'Please enter something to search for.';
} else {
$searchsql = "SELECT * FROM blog_articles";
$searchresults = mysql_query($searchsql,$mysql_connect);
while($searchrow = mysql_fetch_array($searchresults)) {
// WHAT TO SEARCH FOR
$searchresult = stripos($searchrow["blog_title"],$_POST["search_string"]);
// CHECK IF THE STRING IS FOUND IN TITLES
if($searchresult !== false) {
// STRING WAS FOUND, ECHO LINK
echo '<h1><a href="index.php?p=article&article=';
echo $searchrow["blog_article_id"];
echo '">';
echo $searchrow["blog_title"];
echo '</a></h1>';
// ADD TO NUMBER OF RESULTS
$numresults = $numresults + 1;
}
}
// ECHO THE AMOUNT OF RESULTS
echo $numresults . ' posts found.';
}
}
mysql_close();
?>
Thanks in advance,
/Madz0r
