Jump to content

search.php + if statmant


logicopinion

Recommended Posts

hello,

 

i want to search my database and i created search.php file.

 

here is the code:

 

<?php

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("mynews") or die(mysql_error());
$query = "SELECT * FROM newsdb WHERE date like '%$keyword%' or title like '%$keyword%' or rte1 like '%$keyword%'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{

print "<table cellpadding=0 cellspacing= width=100% width=700>";
print "<tr><td height=20 class=\"date\" bgcolor=#E0E0E0 style=\"BORDER-BOTTOM:1px #1E90FF solid;\">  <B>"; echo $row['date']; print "</B></td></tr>";
print "<tr><td>"; print "<br>"; echo $row['title']; print "</td></td>";
print "<tr><td align=right>";
echo "<a class=\"more\" href='read_news.php?id=".$row['id'] ."'>დაწვრილებით...</a>";
print "<br><br>"; 
print "</td></td>";	
print "</table>";

}

?>

 

 

but if the search field is empty and i press "search" button, search.php displays all the data from DB, instead of this i want something like this: if searchword is not specified to echo "please insert search word" but if the searchword is inserted to do the action from above code.

 

how can do that? i know its if statmant function but could not write it right way.. please help me.

Link to comment
https://forums.phpfreaks.com/topic/72176-searchphp-if-statmant/
Share on other sites

like so....

 

<?php

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("mynews") or die(mysql_error());

if(str_replace(" ", "", $keyword) == ""){ //replace spaces with nothing, so if they enter a spaces
echo "You did not enter a keyword, please go back."; //your echo
}else{

$query = "SELECT * FROM newsdb WHERE date like '%$keyword%' or title like '%$keyword%' or rte1 like '%$keyword%'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{

print "<table cellpadding=0 cellspacing= width=100% width=700>";
print "<tr><td height=20 class=\"date\" bgcolor=#E0E0E0 style=\"BORDER-BOTTOM:1px #1E90FF solid;\">  <B>"; echo $row['date']; print "</B></td></tr>";
print "<tr><td>"; print "<br>"; echo $row['title']; print "</td></td>";
print "<tr><td align=right>";
echo "<a class=\"more\" href='read_news.php?id=".$row['id'] ."'>დაწვრილებით...</a>";
print "<br><br>"; 
print "</td></td>";	
print "</table>";

}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/72176-searchphp-if-statmant/#findComment-363944
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.