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

1 replies
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 Code:
<?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";

?>
#begin #file #keyword #make #search
  • Profile picture of the author CBSnooper
    If you look at the sql statements, the % represents the 'wildcard' (ie: any letters).

    So change the code from

    PHP Code:
     "Select * from names where   and (name like '%' OR  meaning like '%') order by name limit ,"
    to

    PHP Code:
     "Select * from names where   and (name like '%%' OR  meaning like '%%') order by name limit ,"
    Note the extra % at the just after the opening quote. Because in the first statement there is only one % after the field name ($l) it only returns results starting with whatever they enter.

    Hope you understand!

    edit: It's not formatted the code properly. Make sure the statement says

    like '%$l%'
    {{ DiscussionBoard.errors[3231323].message }}

Trending Topics