/    Sign up×
Community /Pin to ProfileBookmark

Search.php – help

hallo guys
i have a [I]search.php[/I] file and its code you can see below..

it does [I](i am sure you know what it does)[/I] but i`ll tell in short

so when search keyword/s are posted.. it connects to mysql and makes search.. after search is done… it displays something similar to this:

[B]Search KeyWord Was “some word”[/B]

and then displays how many [COLOR=”Gray”](mysql_num_rows)[/COLOR] results were found..

[B]Search Keywird was “some word”, and 15 results are found.[/B]

if there was [B]ZERO[/B] result says:

[B]Search Keywird was “some word”, and 15 results are found.[/B]

so i want to do something like this:

if there were some matches in search

to display: your search word was : [B]search word[/B]
[B]15 results found.[/B]

otherwise (else)

if search result == 0 to say: sorry, No Matches for your search

so please maybe someone will help me to add such feature to script.

THANK YOU.

[CODE]

<?php
include (“./includes/vars.php”);
$keyword = $_POST[‘keyword’];
mysql_connect(“$localhost”, “$username”, “$password”) or die(mysql_error());
mysql_select_db(“$database”) or die(mysql_error());
$query = “SELECT * FROM mynews WHERE date like ‘%$keyword%’ or title like ‘%$keyword%’ or subtitle like ‘%$keyword%'”;

if (strlen($keyword) > 0)
{
mysql_connect(“localhost”, “$username”, “$password”) or die(mysql_error());
mysql_select_db(“$database”) or die(mysql_error());
$query = “SELECT * FROM mynews WHERE date like ‘%$keyword%’ or title like ‘%$keyword%’ or subtitle like ‘%$keyword%’ or rte1 like ‘%$keyword%'”;
$result = mysql_query($query) or die(mysql_error());
$num=mysql_num_rows($result);
print “<br><br>”;
echo “<div class=’subtitle’>&nbsp;&nbsp;-&nbsp;&nbsp;you searched for <B>$keyword</B> -&#4321;, found <B>$num</B> results</div>”;
print “<br><br>”;
while($row = mysql_fetch_array($result))
{
print “<table border=’0′ cellpadding=’0′ cellspacing=’0′ height=”40″ width=”100%”>”;
print “<TR><TD valign=”top” width=”10%” class=’newsdate’ align=’left’>”.$row[‘date’].”</TD><TD valign=”top” class=’newstitle’ height=’20’><a href=’index.php?p=1&sec_id=”.$row[‘id’].”‘>”.$row[‘title’].”</a></td>”;
print “<TR><TD class=’subtitle’ colspan=’2′>”.$row[‘subtitle’].”</td></TR>”;
print “<TR><TD align=’right’ height=’20’ colspan=’2′><a class=’more’ href=’index.php?p=1&sec_id=”.$row[‘id’].”‘>read more</a></td></TR>”;
print “</table>”;
print “<br />”;

}}
else
{

echo “&nbsp;&nbsp;&nbsp;<div class=”subtitle”>empty search field !</div> “;

}

?>

[/CODE]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@sstalderJan 17.2008 — Something like this should work:

[code=php]
<?php
include ("./includes/vars.php");
$keyword = $_POST['keyword'];
mysql_connect("$localhost", "$username", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());
$query = "SELECT * FROM mynews WHERE date like '%$keyword%' or title like '%$keyword%' or subtitle like '%$keyword%'";


if (strlen($keyword) > 0)
{
mysql_connect("localhost", "$username", "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());
$query = "SELECT * FROM mynews WHERE date like '%$keyword%' or title like '%$keyword%' or subtitle like '%$keyword%' or rte1 like '%$keyword%'";
$result = mysql_query($query) or die(mysql_error());
$num=mysql_num_rows($result);
print "<br><br>";
echo "<div class='subtitle'>&nbsp;&nbsp;-&nbsp;&nbsp;you searched for <B>$keyword</B> -&#4321;, found <B>$num</B> results</div>";
print "<br><br>";

if($num > 0)
{
while($row = mysql_fetch_array($result))
{
print "<table border='0' cellpadding='0' cellspacing='0' height="40" width="100%">";
print "<TR><TD valign="top" width="10%" class='newsdate' align='left'>".$row['date']."</TD><TD valign="top" class='newstitle' height='20'><a href='index.php?p=1&sec_id=".$row['id']."'>".$row['title']."</a></td>";
print "<TR><TD class='subtitle' colspan='2'>".$row['subtitle']."</td></TR>";
print "<TR><TD align='right' height='20' colspan='2'><a class='more' href='index.php?p=1&sec_id=".$row['id']."'>read more</a></td></TR>";
print "</table>";
print "<br />";
print $num . " results were found.";
print "<br />";
}
}else{
print "No results found.";
}
}else{
echo "&nbsp;&nbsp;&nbsp;<div class="subtitle">empty search field !</div> ";
}
?>
[/code]
×

Success!

Help @LogicOpinion spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...