Forum Moderators: coopster

Message Too Old, No Replies

if statement in PHP

I'm trying to make a search engine but i dont know what if statement to use

         

MrGecko

2:57 pm on Apr 15, 2006 (gmt 0)

10+ Year Member



Hi

I'm making a search engine and I don't know what if statement to use

I want it to be like

if search contains item then display item

but I don't know how

now I have it as

if ($row[3] == $search or $row[4] == $search or $row[5] == $search) {
echo "<tr>
<td bgcolor=\"$warna\" align=\"center\" valign=\"top\" width=\"15\">
<font size=\"2\">$inum</font>
</td>
<td bgcolor=\"$warna\" width=\"570\">
<table border=\"0\" width=\"100%\">
<tr>
<td>
<font size=\"2\"><b>$row[3]</b></font>
</td>";
echo "</td></tr></table>";
echo "<br><table border=\"0\" width=\"100%\">
<tr><td width=\"5\">&nbsp;</td><td>
<font size=\"2\" face=\"$message_font_face\" size=\"$message_font_size\">$row[4]</font>";
echo "<br><br><a href=\"$row[5]\">$row[5]</a></td></tr>
</table>";
echo "</td>";
$inum += 1;
}

and that works ok if they search for Something and the item is Something
but if they search for something an the item is Something then it wont work

if anyone can help Me that will be great
Thanks

alce

6:13 pm on Apr 15, 2006 (gmt 0)

10+ Year Member



I am not sure if I fully understood your post but try checking your MySql data types.

If you are using CHAR or VARCHAR make sure the BINARY attribute was not especified.

If you are using TINYBLOB, BLOB, MEDIUMBLOB, or LONGBLOB change them to TINYTEXT, TEXT, MEDIUMTEXT or LONTEXT.

alce

6:44 pm on Apr 15, 2006 (gmt 0)

10+ Year Member



MrGecko, disregard my last post. I believe the problem is in the comparison operator "==".

What you can do is store all the values you are comparing to your variable $search in lowercase, then change

if ($row[3] == $search or $row[4] == $search or $row[5] == $search) {
echo "<tr>

to

if ($row[3] == strtolower($search) or $row[4] == strtolower($search) or $row[5] == strtolower($search)) {
echo "<tr>

or better yet

$new_search = strtolower($search)
if ($row[3] == $new_search or $row[4] == $new_search or $row[5] == $new_search) {
echo "<tr>

MrGecko

6:50 pm on Apr 16, 2006 (gmt 0)

10+ Year Member



I wanted it to be like if I searched for pizza
ad the item says Mr Gecko's Pizza
then it will display that item

Habtom

4:02 am on Apr 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can change your query from:

$sql = "SELECT * FROM mytable WHERE this=that;

to:

$sql = "SELECT * FROM mytable WHERE this like %that%";

Habtom

lexipixel

4:45 am on Apr 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The problem you present has to do with something called "normalizing (data)".

Working with normalized data the query as well as the data to be searched all follow the same rules.

Lets say you have a database with (3) records:


9999¦JOHN O. PUBLICK¦9 WEST STREET¦SUMTOWN¦WV¦
99991¦John Public¦123 Main St.¦Sumtown¦Wv¦
99992¦Jon J. O'Public¦456 Main Street¦E. Sumtown¦WV¦

And you want to list:

- everyone with last name PUBLIC
- everyone with middle initial O
- everyone in SUMTOWN
- everyone in state of WV
- everyone with ID # 9999

Now we have all kinds of matching problem, more than just "case".

The answer: NORMALIZE YOUR DATA and NORMALIZE THE QUERY

Some basic steps towards normalization:


- convert the strings to the same case (upper or lower)
- ignore all characters other than Aa-Za, 0-9 and SPACE
- remove all extra whitespace (compress all multiple SPACE characters to a single SPACE)
- left pad all numeric strings with leading zeros

The solution usually involves using regular expressions (regex) for pattern matching....

Once you get that worked out, you will want to most likely match single or multiple words, exact patterns, multiple matches, partial matches, etc..

More info here:

[php.net...]

or:

[php.net...]

.

MrGecko

1:44 pm on Apr 18, 2006 (gmt 0)

10+ Year Member



I have Tried that and it did not work

here's Some info about my web server
i am hosting off my mac os x computer running Apache and I have php 5.0.3

here's the way i have my data file looks

¦~¦20060412135644¦~¦April 12, 2006 - 01:56 PM¦~¦Mr Gecko's Pizza¦~¦umme Pizza Come And Eat Now.¦~¦http://www.example.com/¦~¦192.168.0.100¦~¦

here's my script that i have now

<?
$record = file($search_file);
rsort($record);
$jmlrec = count($record);
?>
<html>
<head><title><?=$title?></title></head>
<body>
<center>
<font color="<?=$title_color?>" size="5"><b><?=$title?></b></font>
<br>
<br>
<form action="search.php">
<input type="hidden" name="do" value="search">
<table width="300" border="0" cellspacing="0" cellpadding="0" bgcolor="<?=$table_border?>">
<tr>
<td>
<div align="center">
<table width="100%" border="0" cellspacing="1" cellpadding="5">
<tr bgcolor="<?=$table_content_1a?>"> <td width="15%"> <div align="right"><font size="2">Search </font></div>
</td>
<td width="85%"> <input type="text" name="search" value="<?=$search?>" size="30" maxlength="70">
</td>
</tr>
<tr bgcolor="<?=$table_content_1b?>"> <td colspan="2"> <div align="center">
<font size="2">
<input type="submit" value="Search">
</font>
</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</form>
<?
if ($search=="") {
$search = "Nothing";
}
?>
<b>You Have Searched For <?=$search?></b>
<br>
<br>
<table width="600" cellpadding="0" cellspacing="1" border="0">
<tr bgcolor="<?=$edit_table_border?>">
<td>
<table width="100%" cellpadding="4" cellspacing="1" border="0">
<tr>
</tr>
<?
$jml_page = intval($jmlrec/$max_entry_per_page);
$sisa = $jmlrec%$max_entry_per_page;
if ($sisa > 0) $jml_page++;
$no = $page*$max_entry_per_page-$max_entry_per_page;
if ($jmlrec == 0) echo "<tr><td colspan=\"3\" align=\"center\"><font size=\"3\">Not found.</font></td></tr>";

$inum = 1;
$not = 0;
$max = $max_entry_per_page;
$max -= 2;
$w = 0; //--Color
for ($i=0; $i<$max_entry_per_page; $i++) {
$not += 1;
$no++;
$recno = $no-1;
if (isset($record[$recno])) {
$row = explode("¦~¦",$record[$recno]);
if ($w==0) {
$warna = $table_content;
$w=1;
} else {
$warna = $table_content;
$w=0;
}
if ($row[3] == $search or $row[4] == $search or $row[5] == $search) {
echo "<tr>
<td bgcolor=\"$warna\" align=\"center\" valign=\"top\" width=\"15\">
<font size=\"2\">$inum</font>
</td>
<td bgcolor=\"$warna\" width=\"570\">
<table border=\"0\" width=\"100%\">
<tr>
<td>
<font size=\"2\"><b>$row[3]</b></font>
</td>
";
echo "</td></tr></table>";
echo "<br><table border=\"0\" width=\"100%\">
<tr><td width=\"5\">&nbsp;</td><td>
<font size=\"2\" face=\"$message_font_face\" size=\"$message_font_size\">$row[4]</font>";
echo "<br><br><a href=\"$row[5]\">$row[5]</a></td></tr>
</table>";
echo "</td>";
$inum += 1;
} else {
if ($search == "Nothing")
{
echo "<tr>
<td bgcolor=\"$warna\" align=\"center\" valign=\"top\" width=\"15\">
<font size=\"2\">$inum</font>
</td>
<td bgcolor=\"$warna\" width=\"570\">
<table border=\"0\" width=\"100%\">
<tr>
<td>
<font size=\"2\"><b>$row[3]</b></font>
</td>
";
echo "</td></tr></table>";
echo "<br><table border=\"0\" width=\"100%\">
<tr><td width=\"5\">&nbsp;</td><td>
<font size=\"2\" face=\"$message_font_face\" size=\"$message_font_size\">$row[4]</font>";
echo "<br><br><a href=\"$row[5]\">$row[5]</a></td></tr>
</table>";
echo "</td>";
$inum += 1;
}
if ($search!= $row[3] && $search!= $row[4] && $search!= $row[5] && $search!= "Nothing") {
if ($not == $max) {
echo "<tr>
<td bgcolor=\"$warna\" width=\"570\">
<table border=\"0\" width=\"100%\">
<tr>
<td>
<font size=\"5\"><b><center>Not Found</center></b></font>
</td>
";
echo "</tr></table></td>";
$not += 1;
}
}
}
}
}
echo "<tr><td colspan=\"3\" bgcolor=\"$table_bottom\" align=\"center\" width=\"600\"><font size=\"1\">";
if ($jml_page > 1) {
if ($page <> 1) echo "[<a href=\"search.php?do=edit&page=1\">Top</a>] "; else echo "[Top] ";
echo "Page # ";
if ($jml_page > 10) {
if ($page < 5) {
$start = 1;
$stop = 10;
} elseif ($jml_page - $page < 5) {
$start = $jml_page - 9;
$stop = $jml_page;
} else {
$start = $page-4;
$stop = $page+5;
}
if ($start <> 1) echo "... ";
for ($p=$start; $p<=$stop; $p++) {
if ($p == $page) echo "<font color=\"$active_link\"><b>$p</b></font>&nbsp;&nbsp;";
else echo "<a href=\"search.php?do=edit&page=$p\">$p</a>&nbsp;&nbsp;";
}
if ($stop <> $jml_page) echo "... ";
echo "of $jml_page ";
} else {
for ($p=1; $p<=$jml_page; $p++) {
if ($p == $page) echo "<font color=\"$active_link\"><b>$p</b></font>&nbsp;&nbsp;";
else echo "<a href=\"search.php?do=edit&page=$p\">$p</a>&nbsp;&nbsp;";
}
}
if ($page <> $jml_page) echo "[<a href=\"search.php?do=edit&page=$jml_page\">Bottom</a>]"; else echo "[bottom]";
} else echo "Page #1 of 1";
echo "</font></td></tr>";
?>
</table> </td>
</tr>
</table><br><br>
</center>
</body>
</html>

Thanks For Trying!
Mr Gecko

[edited by: coopster at 5:26 pm (utc) on April 18, 2006]
[edit reason] generalized url [/edit]

MrGecko

8:51 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



It Works sorta now but now if you search
Vital Sign and the item is Vital Signs
it dose not work or if you search
vital signs and the item is Vital Signs
but i have found that if you search
Vital Signs y and the item is Vital Signs
then it will work

now the if statement is
if (ereg($row[3], $search) or ereg($row[4], $search) or ereg($row[5], $search))

if someone can help me then that will be great!

Mr. Gecko

[edited by: jatar_k at 6:06 pm (utc) on April 25, 2006]