1

Using this code, I do search from my database and display the result. Now its show all the result of my table first.

Then when I search for a new Item then its show the item. I do not want to display all result first. I want to display only searched Item.

<table class="table table-striped">
    <tr>
        <th>No</th>
        <th>Teacher Name</th>
        <th>Gender</th>
        <th>Date of Birth</th>
        <th>Place of Birth</th>
        <th>Degree</th>
        <th>Salary</th>
        <th>Married</th>
        <th>Phone</th>
        <th>E-mail</th>
    </tr>
     <?php
     $key="";
if(isset($_POST['searchtxt']))
    $key=$_POST['searchtxt'];

if($key !="")
    $sql_sel=mysql_query("SElECT * FROM teacher_tbl WHERE f_name  like '%$key%' or l_name like '%$key%'");
else
    $sql_sel=mysql_query("SELECT * FROM teacher_tbl");

$i=0;
while($row=mysql_fetch_array($sql_sel)){
$i++;
?>
  <tr >
        <td><?php echo $i;?></td>
        <td><?php echo $row['f_name']."    ".$row['l_name'];?></td>
        <td><?php echo $row['gender'];?></td>
        <td><?php echo $row['dob'];?></td>
        <td><?php echo $row['pob'];?></td>
        <td><?php echo $row['degree'];?></td>
        <td><?php echo $row['salary'];?></td>
        <td><?php echo $row['married'];?></td>
        <td><?php echo $row['phone'];?></td>
        <td><?php echo $row['email'];?></td>

    </tr>
<?php   
}
?>
</table>
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
  • Just move your `while` block inside the `if` statement and remove `else` – DWand May 10 '15 at 07:38
  • Oo, and welcome to SO, if you feel any of the answers helped you, then you may accept one as an [answer](http://stackoverflow.com/tour). – someOne May 10 '15 at 08:42

2 Answers2

2

Using the following code, the search will be carried out and displayed only when you have submitted a search key:

<?php
$key="";
if(isset($_POST['searchtxt']) && !empty($_POST['searchtxt'])) {
    $key=$_POST['searchtxt'];
    $sql_sel=mysql_query("SElECT * FROM teacher_tbl WHERE f_name  like '%$key%' or l_name like '%$key%'");

    $i=0;
    while($row=mysql_fetch_array($sql_sel)) {
        $i++;
        ?>
          <tr>
              <td><?php echo $i;?></td>
              <td><?php echo $row['f_name']."    ".$row['l_name'];?></td>
              <td><?php echo $row['gender'];?></td>
              <td><?php echo $row['dob'];?></td>
              <td><?php echo $row['pob'];?></td>
              <td><?php echo $row['degree'];?></td>
              <td><?php echo $row['salary'];?></td>
              <td><?php echo $row['married'];?></td>
              <td><?php echo $row['phone'];?></td>
              <td><?php echo $row['email'];?></td>
          </tr>
        <?php   
    }
}
?>
someOne
  • 1,975
  • 2
  • 14
  • 20
  • By the way, [avoid using the monstrous `mysql_*`](http://stackoverflow.com/q/12859942/3709765) :) – someOne May 10 '15 at 07:48
0

You just have to loop through the results only if $_POST['searchtxt'] is set.

 <?php
  $key=$_POST['searchtxt'];

if($key !="")
    $sql_sel=mysql_query("SElECT * FROM teacher_tbl WHERE f_name  like '%$key%' or l_name like '%$key%'");
else
    $sql_sel=mysql_query("SELECT * FROM teacher_tbl");

$i=0;
if(isset($_POST['searchtxt']) && !empty($_POST['searchtxt']))
{


 while($row=mysql_fetch_array($sql_sel)){
 $i++;
?>
  <tr >
        <td><?php echo $i;?></td>
        <td><?php echo $row['f_name']."    ".$row['l_name'];?></td>
        <td><?php echo $row['gender'];?></td>
        <td><?php echo $row['dob'];?></td>
        <td><?php echo $row['pob'];?></td>
        <td><?php echo $row['degree'];?></td>
        <td><?php echo $row['salary'];?></td>
        <td><?php echo $row['married'];?></td>
        <td><?php echo $row['phone'];?></td>
        <td><?php echo $row['email'];?></td>

    </tr>
<?php   
}
}
?>
</table>
Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • Well, using this code will **always** return **all** the data, regardless of weather any search key have been provided or not!! – someOne May 10 '15 at 07:53
  • !empty() was what missing...thanx for the correction...ur ans does the job as well – Abhinav May 10 '15 at 07:56
  • No, not actually; the first `if` statement has set the `$sql_sel` to query the SQL command of _"SELECT * FROM teacher_tbl"_ ; because the `$key` is initially set to `''` and have been never changed! – someOne May 10 '15 at 08:03
  • please have a look at the if statement, I have changed it – Abhinav May 10 '15 at 08:05
  • Not that one! the first `if` statement!! – someOne May 10 '15 at 08:06
  • okay....i just removed the $key='', cause that doesnt make any since its been again reassigned to $_POST[''searchtxt]; – Abhinav May 10 '15 at 08:13
  • Now it will work correctly, but the code still has some redundancies! – someOne May 10 '15 at 08:18
  • would you care to explain the redundancies? – Abhinav May 10 '15 at 08:19
  • Sure, You've firstly set the `$key=$_POST['searchtxt'];`, then checked against the `$key` using the first `if` statement, if the `$key` is empty then the statement in the `else` will be carried out, which is redundant. In the second `if` statement you've check against the emptiness of the same data you've already done once again. The suggestion is to remove the lines 2-7 and replace line 7 to exactly before the `while`, then you will get almost the exact same code as I've posted three quarters ago!! – someOne May 10 '15 at 08:27
  • thnx for the suggestion – Abhinav May 10 '15 at 08:29