Solved

Formatting Search results from AJAX search

Posted on 2014-03-14
4
378 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 109

Expert Comment

by:Ray Paseur
ID: 39928967
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 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39929071
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
ID: 40098420
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This video teaches users how to migrate an existing Wordpress website to a new domain.
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.

809 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