?
Solved

Formatting Search results from AJAX search

Posted on 2014-03-14
4
Medium Priority
?
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 111

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 58

Accepted Solution

by:
Julian Hansen earned 2000 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

762 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