Sir I am using these codes

<div id="search">
        <label for="filter">Filter</label> <input type="text" name="filter" value="" id="filter" />
</div>   
<br /> 
          <table cellpadding="2" cellspacing="0" width="90%" id="resultTable">
          <thead>
            <tr>
              <th> <strong>ID</strong> </th>
              <th> <strong>Name</strong> </th>
              <th> <strong>Mobile</strong> </th>
              <th> <strong>City</strong> </th>
              <th> <strong>Country</strong> </th>
            </tr>
          </thead>
          <tbody>
<?php
$sql = "SELECT * from contacts order by name" ;
$select = mysqli_query($con, $sql);
$run_sel = mysqli_fetch_array($select);
$row=mysqli_num_rows($select);
//echo ($row);
$a = 1;
while($a<=$row)
{
    echo "<tr>";
    echo "<td> <a href='contacts.php?id=".$run_sel['id']."'>
    <img border=\"noborder\" src=\"images/pencil.png\" title=\"Display\"></a></td>";
    echo "<td align='left'> " . $run_sel["name"].  "</td>";
    echo "<td> " . $run_sel["moba"]. "</td>";
    echo "<td> " . $run_sel["city"]. "</td>";
    echo "<td> " . $run_sel["country"] . "</td>";
    echo "</tr>";
    $run_sel = mysqli_fetch_array($select);
    $a++;

}
?>
        </tbody>
       </table>      

These codes display following data
[IMG]http://i43.tinypic.com/or45r8.jpg[/IMG]

Now I want to apply incremental search with Filter box.

For example:

If I type "B" then all matching records (name field in table) which begins with "B" must appear in display list.

Please help

Recommended Answers

All 12 Replies

Member Avatar
"SELECT ... WHERE `name` LIKE 'B%'"

So..

"SELECT ... WHERE `name` LIKE '$filter%'"

where should i put my query?

I want to populate it with
<input type="text" name="filter" value="" id="filter" />

Member Avatar
$filter = '';
if(isset($_GET['filter']) && trim($_GET['filter']))
{
    $input = trim($_GET['filter']);
    $input = strtoupper(substr($input,0,1));
    if(ctype_alpha($input)) $filter = " WHERE `name` LIKE '$filter%'";
}    

$query = "SELECT ..." . $filter;

now run the query and do your stuff on the page

Sir I modified codes as you said.
Now codes are as follows

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>My Contacts</title>

    <style type="text/css">

    html {
overflow:auto;
}

    body{
        background-color:#e7f4fe;
        padding:15px;
        }

    #box2{
    float:left;
    width:500px;
    height:400px;
    border-color:#cceeff;
    padding:10px;
    background-color:#CCF;
    overflow:auto;
    }

    #box2 #resultTable{
    border-bottom: 7px solid #9BAFF1;
    border-collapse: collapse;
    border-top: 7px solid #9BAFF1;
    font-family: "Lucida Sans Unicode","Lucida Grande",Sans-Serif;
    font-size: 12px;
    margin: 10px;
    text-align: center;
    width: 480px;
    }

    #box2 #resultTable th{
    background: none repeat scroll 0 0 #B9C9FE;
    border-left: 1px solid #9BAFF1;
    border-right: 1px solid #9BAFF1;
    color: #003399;
    font-size: 13px;
    font-weight: normal;
    padding: 4px;
    }

    #box2 #resultTable td {
    background: none repeat scroll 0 0 #E8EDFF;
    border-left: 1px solid #AABCFE;
    border-top: 1px solid #9BAFF1;
    border-right: 1px solid #AABCFE;
    color: #666699;
    padding: 4px;
    }

    </style>
    </head>

    <body  onload="form1.filter.focus()">
    <div id="box2">

    <form name="form1" action=""  method="post">

        <label for="filter"><b>Filter</b></label>
        <input type="text" name="filter" value="" id="filter" />
        <input type="submit" value="Filter Result" name="display">

          <table cellpadding="2" cellspacing="0" width="80%" id="resultTable">
          <thead>
            <tr>
              <th> <strong>ID</strong> </th>
              <th> <strong>Name</strong> </th>
              <th> <strong>Mobile</strong> </th>
              <th> <strong>City</strong> </th>
              <th> <strong>Country</strong> </th>
            </tr>
          </thead>
          <tbody>
<?php

require_once("connect.php");

$filter = '';
if(isset($_GET['filter']) && trim($_GET['filter']))
{
    $input = trim($_GET['filter']);
    $input = strtoupper(substr($input,0,1));
    if(ctype_alpha($input)) $filter = " WHERE `name` LIKE '$filter%'";
}  

$sql = "SELECT * from contacts .$filter order by name"; 
$select = mysqli_query($con, $sql); 
$run_sel = mysqli_fetch_array($select); 
$row=mysqli_num_rows($select);  

$a = 1;
while($a<=$row)
{
    echo "<tr>";
    echo "<td> <a href='contacts.php?id=".$run_sel['id']."'>
    <img border=\"noborder\" src=\"images/pencil.png\" title=\"Display\"></a></td>";
    echo "<td align='left'> " . $run_sel["name"].  "</td>";
    echo "<td> " . $run_sel["moba"]. "</td>";
    echo "<td> " . $run_sel["city"]. "</td>";
    echo "<td> " . $run_sel["country"] . "</td>";
    echo "</tr>";
    $run_sel = mysqli_fetch_array($select);
    $a++;
 }
?>
        </tbody>
       </table>   
       </form>   
</div>   

    </body>
    </html>

But these codes still not work.
I enter some value into textbox and press Filter Result button then it says:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\Phonebook\filter_dia.php on line 93

and line number 93 is

$run_sel = mysqli_fetch_array($select); 

I think i have made some mistake in query.
Please help me to locate the error.

Thanks

Member Avatar

That error message suggests that there is a typo or wrong fieldname/ tablename etc in the query.

$sql = "SELECT * from contacts .$filter order by name"; 

You've included the concatenator (.) within the string - no need:

$sql = "SELECT * from contacts $filter order by name"; 

Try that.

Sir now I have these codes, when I submit form then nothing appears.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>My Contacts</title>

    <style type="text/css">

    html {
overflow:auto;
}

    body{
        background-color:#e7f4fe;
        padding:15px;
        }

    #box2{
    float:left;
    width:500px;
    height:400px;
    border-color:#cceeff;
    padding:10px;
    background-color:#CCF;
    overflow:auto;
    }

    #box2 #resultTable{
    border-bottom: 7px solid #9BAFF1;
    border-collapse: collapse;
    border-top: 7px solid #9BAFF1;
    font-family: "Lucida Sans Unicode","Lucida Grande",Sans-Serif;
    font-size: 12px;
    margin: 10px;
    text-align: center;
    width: 480px;
    }

    #box2 #resultTable th{
    background: none repeat scroll 0 0 #B9C9FE;
    border-left: 1px solid #9BAFF1;
    border-right: 1px solid #9BAFF1;
    color: #003399;
    font-size: 13px;
    font-weight: normal;
    padding: 4px;
    }

    #box2 #resultTable td {
    background: none repeat scroll 0 0 #E8EDFF;
    border-left: 1px solid #AABCFE;
    border-top: 1px solid #9BAFF1;
    border-right: 1px solid #AABCFE;
    color: #666699;
    padding: 4px;
    }

    </style>
    </head>

    <body  onload="form1.filter.focus()">
    <div id="box2">

    <form name="form1" action=""  method="post">

        <label for="filter"><b>Filter</b></label>
        <input type="text" name="filter" value="" id="filter" />
        <input type="submit" value="Filter Result" name="display">

          <table cellpadding="2" cellspacing="0" width="80%" id="resultTable">
          <thead>
            <tr>
              <th> <strong>ID</strong> </th>
              <th> <strong>Name</strong> </th>
              <th> <strong>Mobile</strong> </th>
              <th> <strong>City</strong> </th>
              <th> <strong>Country</strong> </th>
            </tr>
          </thead>
          <tbody>
<?php

require_once("connect.php");

$filter = '';
if(isset($_GET['filter']) && trim($_GET['filter']))
{
    $input = trim($_GET['filter']);
    $input = (substr($input,0,1));
    if(ctype_alpha($input)) $filter = " WHERE `name` LIKE '$filter%'";


$sql = "SELECT * from contacts .$filter order by name"; 
$select = mysqli_query($con, $sql); 
$run_sel = mysqli_fetch_array($select); 
$row=mysqli_num_rows($select);  

$a = 1;
while($a<=$row)
{
    echo "<tr>";
    echo "<td> <a href='contacts.php?id=".$run_sel['id']."'>
    <img border=\"noborder\" src=\"images/pencil.png\" title=\"Display\"></a></td>";
    echo "<td align='left'> " . $run_sel["name"].  "</td>";
    echo "<td> " . $run_sel["moba"]. "</td>";
    echo "<td> " . $run_sel["city"]. "</td>";
    echo "<td> " . $run_sel["country"] . "</td>";
    echo "</tr>";
    $run_sel = mysqli_fetch_array($select);
    $a++;
 }
 }  
?>
        </tbody>
       </table>   
       </form>   
</div>   

    </body>
    </html>

I want to ask more, should I fill the value in this input?

<input type="text" name="filter" value="" id="filter" />

if yes then what variable should I use?

$filter or $input

Member Avatar

Sigh, you still haev't taken out the concatenator:

$sql = "SELECT * from contacts .$filter order by name"; 

SHOULD BE

$sql = "SELECT * from contacts $filter order by name"; 

Then...

$table = '';
$select = mysqli_query($con, $sql); 
if(mysqli_num_rows($select))
{
    $table .= '<table cellpadding="2" cellspacing="0" width="80%" id="resultTable">
      <thead>
        <tr>
          <th> <strong>ID</strong> </th>
          <th> <strong>Name</strong> </th>
          <th> <strong>Mobile</strong> </th>
          <th> <strong>City</strong> </th>
          <th> <strong>Country</strong> </th>
        </tr>
      </thead>
      <tbody>";
    while($run_sel = mysqli_fetch_array($select))
    {
        $table .= "<tr><td> <a href='contacts.php?id={$run_sel['id']}'><img border='noborder' src='images/pencil.png' title='Display'></a></td><td align='left'>{$run_sel['name']}</td><td>{$run_sel['moba']}</td><td>{$run_sel['city']}</td><td>{$run_sel['country']}</td></tr>";
    }
    $table .= "</tbody></table>";
}

//later on...
echo $table;

Sir I have modified as you said above.
Now body tag is like this

<body  onload="form1.filter.focus()">
    <div id="box2">

    <form name="form1" action=""  method="post">

        <label for="filter"><b>Filter</b></label>
        <input type="text" name="filter" value="" id="filter" />
        <input type="submit" value="Filter Result" name="display">

          <table cellpadding="2" cellspacing="0" width="80%" id="resultTable">
          <thead>
            <tr>
              <th> <strong>ID</strong> </th>
              <th> <strong>Name</strong> </th>
              <th> <strong>Mobile</strong> </th>
              <th> <strong>City</strong> </th>
              <th> <strong>Country</strong> </th>
            </tr>
          </thead>
          <tbody>
<?php

require_once("connect.php");

$filter = "";
if(isset($_GET['filter']) && trim($_GET['filter']))
{
    $input = trim($_GET['filter']);
    $input = (substr($input,0,1));
    if(ctype_alpha($input)) $filter = " WHERE `name` LIKE '$filter%'";


$sql = "SELECT * from contacts $filter order by name"; 
$table = '';
$select = mysqli_query($con, $sql); 
if(mysqli_num_rows($select))
{
    $table .= '<table cellpadding="2" cellspacing="0" width="80%" id="resultTable">
      <thead>
        <tr>
          <th> <strong>ID</strong> </th>
          <th> <strong>Name</strong> </th>
          <th> <strong>Mobile</strong> </th>
          <th> <strong>City</strong> </th>
          <th> <strong>Country</strong> </th>
        </tr>
      </thead>
      <tbody>";
    while($run_sel = mysqli_fetch_array($select))
    {
        $table .= "<tr><td> <a href='contacts.php?id={$run_sel['id']}'><img border='noborder' src='images/pencil.png' title='Display'></a></td><td align='left'>{$run_sel['name']}</td><td>{$run_sel['moba']}</td><td>{$run_sel['city']}</td><td>{$run_sel['country']}</td></tr>";
    }
    $table .= "</tbody></table>";
}
//later on...
echo $table;
?>
        </tbody>
       </table>   
       </form>   
</div>   

    </body>

but it has many errors now.
Sir I am still unable to solve out the problems.

Please

Member Avatar

Why are you using post and get? use method="get" in the form tag

Well, now when I enter any name in search box then it show all records of table.

I tested query in this way

$sql = "SELECT * from contacts $filter order by name"; 
echo $sql; 

it shows:
SELECT * from contacts WHERE name LIKE '%' order by name

It means $filter is not getting any value from form.

Should we fill value in this line

  <input type="text" name="filter" value="" />
Member Avatar

My bad, the line should be...

if(ctype_alpha($input)) $filter = " WHERE `name` LIKE '$input%'";

Thanks sir, it works fine now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.