Solved

Formatting Search results from AJAX search

Posted on 2014-03-14
4
381 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 110

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 56

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

679 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