Solved

Formatting Search results from AJAX search

Posted on 2014-03-14
4
357 Views
Last Modified: 2014-05-29
We have (for example) a management system written using CodeIgniter.
This one (for example) is a list of diagnostic information of 100 PC systems.

The existing view code roughly goes
foreach ($query->result() as $row)
{
 // resolve a load of numerical data into meaningful text strings using CodeIgniter helpers
 // resolve another load of numerical data into image inserts using CodeIgniter helpers functions
 // create a html string for that row
 // output the row
}


We're looking to implement a dynamic search facility using an AJAX call and have the results brought back as a JSON object.

I don't want to have to duplicate the above formatting loop in Javascript, therefore what is the correct practice to ensure the results are formatted as per the PHP loop above?
0
Comment
Question by:stuartlandreth
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Can you please show us a little of the data?  Maybe print out one of the $row variables with var_dump() and post the output here, thanks.
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
I would not worry about sending back the data as JSON. Rather have one function that generates the output.

When the page is loaded - call this function to generate the output.

When the AJAX call is made - have the AJAX script call the same code and then just dump the returned HTML straight into the document.

Here is a simple sample illustrating the point
HTML: t606.php
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  $('#search_button').click(function(e) {
    e.preventDefault();
    $.post('data.php', {search: $('#search').val()}, function(html) {
      $('#superheros tbody').html(html);
    });
  });
});
</script>
<style type="text/css">
</style>
</head>
<body>
  <h2>Super Hero Contact List</h2>
  Search <input type="text" id="search" /> <input type="button" value="Search" id="search_button" /><br/>
  <table id="superheros">
    <thead>
      <tr>
        <th>Firstname</th><th>Surname</th><th>Age</th><th>Email</th>
      </tr>
    </thead>
    <?php require_once('data.php'); ?>
  </table>
</body>
</html>

Open in new window

Data.php
<?php
$values = array(
  array('firstname' => 'Peter', 'surname' => 'Parker', 'age' =>'21', 'email' =>'peter@arachnaphopia.com'),
  array('firstname' => 'Clark', 'surname' => 'Kent', 'age' =>'36', 'email' =>'kal-el@krypton.com'),
  array('firstname' => 'Bruce', 'surname' => 'Banner', 'age' =>'28', 'email' =>'hulk@bigandgreen.com'),
  array('firstname' => 'Steven', 'surname' => 'RogersS', 'age' =>'95', 'email' =>'captain@merica.com'),
  array('firstname' => 'Bruce', 'surname' => 'Wayne', 'age' =>'32', 'email' =>'bruce@batcave.com')
);
$expr = empty($_POST['search']) ? '' : $_POST['search'];
$expr = str_replace('%', '.*', $expr);

foreach($values as $v) {
  $rv = 1;
  if (!empty($expr)) {
    $results = implode(" ", $v);

    $rv = preg_match("/$expr/", $results);
  }
  if ($rv > 0) :
 echo '<tr>
   <td>' . $v['firstname'] . '</td><td>' . $v['surname'] . '</td><td>' . $v['age'] . '</td><td>' . $v['email'] .'</td>
   </tr>';
   endif;
}
?>

Open in new window

Working sample here
Try searching with
Bru%
ar%
kr%
The code demonstrates how the same code is used to generate the table on first render and on subsequent requests by the AJAX search
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 500 points for julianH's comment #a39929071

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now