Forum Moderators: coopster

Message Too Old, No Replies

This file only search 'Begin with keyword', how to make it search 'Contain keyword' ?

         

basketmen

10:01 pm on Jan 22, 2011 (gmt 0)

10+ Year Member



This search.php file only search 'Begin with keyword' only, how to make it search 'Contain keyword'



for example there already a name in database, lets say James


1. right now if user search for 'Begin with keyword' like Ja, or Jam, it will showing it (James, and other name begin with that keyword)

2. but not if search for 'End with keyword' or 'Contain keyword', like mes, or ames, or ame (it will not showing James, and other name with that keyword in the middle or in the end)




i need to make it like no.2 above, can search for 'Contain keyword', not search for 'Begin with keyword' only. Please help guys what is the right code, i think here is the code part that responsible for it

$sq = "select * from names where $gn $ori and (name like '$l%' OR meaning like '$l%') order by name";

$sql = "Select * from names where $gn $ori and (name like '$l%' OR meaning like '$l%') order by name limit $limitvalue,$limit";


* = are the keyword that typed by user
names = the database table contain name list
meaning = the database table contain name meaning list









here is the entire search.php file if needed

<?php
include "conn.php";

$origin = $_GET[origin];
$gender = $_GET[gender];
$l = $_GET[letter];


$page=$_GET[page];
$limit=50;

if(empty($_GET[page])){
$page = 1;
}

$limitvalue = $page * $limit - ($limit);


if($origin != "any") $ori = " and origin = '$origin'";
if($gender == "either") $gn = "(gender = 'male' or gender = 'female')";
if($gender != "either") $gn = "gender = '$gender'";


$sq = "select * from names where $gn $ori and (name like '$l%' OR meaning like '$l%') order by name";
$rst = mysql_query($sq) or die(mysql_error());
$totalrows = mysql_numrows($rst);

$sql = "Select * from names where $gn $ori and (name like '$l%' OR meaning like '$l%') order by name limit $limitvalue,$limit";
$rec = mysql_query($sql) or die(mysql_error());





$topcontent = "<table width='100%' cellpadding='3'>
<tr>
<th bgcolor='#FFEAF5' aling='left'>Nama</th>
<th bgcolor='#FFEAF5' aling='left'>Jenis Kelamin</th>
<th bgcolor='#FFEAF5' aling='left'>Asal</th>
<th bgcolor='#FFEAF5' aling='left'>Arti</th>
<th bgcolor='#FFEAF5' aling='left'>Detail</th></tr>";

$lowcontent = "<table width='100%' cellpadding='3'>";


$bg="#FFEAF5";

$ctr=0;
while($datas=mysql_fetch_array($rec)){
if($bg=="#FFEAF5"){
$bg="#ffffff";
}else{
$bg="#FFEAF5";
}
$means=substr($datas[meaning],0,9999);
$link = str_replace(" ","_",$datas[name]);
if($datas[gender]=="female"){
$gender = "Perempuan";
}else{
$gender = "Laki-Laki";
}
$topcontent .= "<tr><td bgcolor='$bg'><b><a href='$datas[id]/$link.html'>$datas[name]</a></b></td>
<td bgcolor='$bg'>$gender</td>
<td bgcolor='$bg'>Nama $datas[origin]</td>
<td bgcolor='$bg'>$means</td>
<td bgcolor='$bg'><a href='$datas[id]/$link.html' rel='nofollow'>Detail...</a></td></tr>";
$ctr++;

}



/*
Page pagination
*/
$l = str_replace("%","",$letter);

$pages .= "<p align='left'>Page(s) ";
if($page > 1){
$pageprev = $page-1;
$pages .= "<a href=\"search.php?origin=$origin&gender=$_GET[gender]&letter=$_GET[letter]&page=$pageprev\">PREV</a>&nbsp;";
}

$numofpages = ceil($totalrows / $limit);

$starting = $page - 5;
$ending = $page + 5;

if($starting < 0) $starting = 1;
if($ending > $numofpages) $ending = $numofpages;

for($i = $starting; $i <= $ending; $i++){
if($page == $i){
$pages .= "<b>". $i."</b>&nbsp;";
}else
$pages .= "<a href=\"search.php?origin=$origin&gender=$_GET[gender]&letter=$_GET[letter]&page=$i\">$i</a>&nbsp;";
}



if($page < $numofpages){
$pagenext = ($page + 1);
$pages .="<a href=\"search.php?origin=$origin&gender=$_GET[gender]&letter=$_GET[letter]&page=$pagenext\">NEXT</a>";
}


/*********************************************************************************/







$topcontent .= "</table>";
$lowcontent .= "</table>";

$heading = "Hasil Cari Nama $gender - $origin";
if($page > 1){
$title = "Hasil Cari Nama $gender - $origin $page. Arti &amp; Daftar Nama Bayi, Anak, Indonesia, Laki, Perempuan";
}
if($page == 1){
$title = "Hasil Cari Nama $gender - $origin. Arti &amp; Daftar Nama Bayi, Anak, Indonesia, Laki, Perempuan";
}

include "template.php";

?>

rocknbil

5:28 pm on Jan 24, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Examine the example I posted here [webmasterworld.com] closely; you can do this with a series of radio buttons on the front end and build your where statement accordingly to compile begins with, starts with, contains. Note that is a PERL example, but the logic is the same.

Also it's important to mention, never use uncleansed input data in your selects, it's a recipe for disaster.