1

I have written a simple page script to loop through a dynamic table that gets data from the database. But when I click on search the page returns blank with no error. I'm trying to understand what's going on without success.

display_table.php

<?php
  include('session.php');
if  ($_SESSION['login_user']){
   include 'includes/header.php';


   $searchQ = "SELECT * FROM companytable";


if(isset($_POST['search'])){
    $search_term = mysqli_real_escape_string($db, $_POST['search_box']);
$searchQ .="WHERE title ='{$search_term}' ";
$searchQ .="OR country ='{$search_term}' ";
$searchQ .="OR description ='{$search_term}' ";
$searchQ .="OR timezone ='{$search_term}' ";
}
$query = mysqli_query($db, $searchQ) or die(mysqli_error());

}

form

<form class="form"  name="search_form" method="POST" action="display_table.php"> 
      <input id="search_box" style="padding: 2px;" class=""  type="text" name="search_box"> 
      <input  class="btn btn-default" type="submit" name="search" value="&#128269;"> 
    </form>

table

<table>

  <tr>
      <th>ID</th>
    <th>Name</th>
      <th>Description</th>
        <th>Type</th>
    <th>Address</th>
    <th>Country</th>
        <th>Time Zone</th>
  </tr>

  <?php while($company=mysqli_fetch_array($result)){ ?>
  <tr>
      <td data-th="ID"><?=$company['id'];?></a></td>
      <td data-th="Name"><?=$company['title'];?></td>
    <td data-th="Description"><?=$company['description'];?></td>
    <td data-th="Type"><?=$company['type'];?></td>
    <td data-th="Address"><?=$company['address'];?></td>
    <td data-th="Country"><?=$company['country'];?></td>
    <td data-th="Time Zone"><?=$company['timezone'];?></td>
  </tr>

    <?php };?>

</table>

3 Answers 3

1

You need to change

<?php while($company=mysqli_fetch_array($result)){ ?>

to reference the mysqli result you created, $query:

<?php while($company=mysqli_fetch_array($query)){ ?>
Sign up to request clarification or add additional context in comments.

4 Comments

Same blank page return.
Have you confirmed the $searchQ you're creating has any rows in the result?
Well I made a test with $searchQ = "SELECT * FROM companytable LIMIT 5"; to see if the table would only return 5 rows at a time and it works. So I'm guessing yes.
Why not echo $searchQ after you build it and test that? If your $_POST is empty for example one would expect no results.
1

You can use object-oriented

<?php
  include('session.php');
if  ($_SESSION['login_user']){
   include 'includes/header.php';

   $query = "SELECT * FROM companytable ";

   if(isset($_POST['search_box'])){
    $search_term = $db->real_escape_string($_POST['search_box']);

    $query.=" WHERE title ='{$search_term}'
             OR country ='{$search_term}'
             OR description ='{$search_term}'
             OR timezone ='{$search_term}';";
   }

   if(!$s = $db->query($query)){
    die($db->error);
   }

}

Table

<table>

  <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Description</th>
      <th>Type</th>
      <th>Address</th>
      <th>Country</th>
      <th>Time Zone</th>
  </tr>

  <?php while($m = $s->fetch_object()){ ?>
  <tr>
      <td data-th="ID"><?=$m->id;?></a></td>
      <td data-th="Name"><?=$m->title;?></td>
      <td data-th="Description"><?=$m->description;?></td>
      <td data-th="Type"><?=$m->type;?></td>
      <td data-th="Address"><?=$m->address;?></td>
      <td data-th="Country"><?=$m->country;?></td>
      <td data-th="Time Zone"><?=$m->timezone;?></td>
  </tr>
  <?php 
    };
    $s->free();
  ?>

</table>

6 Comments

Now I get this Fatal error: Cannot use object of type stdClass as array
@Skynet check now, i changed some variables. Which version of php and mysql are you using?
I'm using 5.6 version.
@Skynet in php or mysql? or both?
@Skynet check now.. if(isset($_POST['search_box'])){
|
0

So turn out I decided to go with sortable.js It not only takes care of search but also takes care of sorting the rows by clicking on headers. So if you are looking for a clean fix, that would be it.

form

  <form class="form"  name="search_form">  <input  id="search" style="" class="form-control"  type="text" name="search" placeholder="&#128269; Search..."> 

table

<table id="table" class="sortable" >

  <thead style="cursor:pointer;">
      <th>ID</th>
    <th>Name</th>
      <th>Description</th>
        <th>Type</th>
    <th>Address</th>
    <th>Country</th>
        <th>Time Zone</th>
 </thead>

 <tbody>
 <?php while($company=mysqli_fetch_array($result)){ ?>
  <tr>
      <td data-th="ID" sorttable_customkey="2"><?=$company['id'];?></a></td>
      <td data-th="Name"><?=$company['title'];?></td>
    <td data-th="Description"><?=$company['description'];?></td>
    <td data-th="Type"><?=$company['type'];?></td>
    <td data-th="Address"><?=$company['address'];?></td>
    <td data-th="Country"><?=$company['country'];?></td>
    <td data-th="Time Zone"><?=$company['timezone'];?></td>
  </tr>
    <?php };?>
 </tbody>
 <tfoot></tfoot>


</table>

Search script

<script src="js/sorttable.js"></script> //load sortable.js

    <script type="text/javascript">
    var $search_rows = $('#table tr');
    $('#search').keyup(function() {
        var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

        $search_rows.show().filter(function() {
            var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
            return !~text.indexOf(val);
        }).hide();
    });
    </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.