Solved

Formatting Search results from AJAX search

Posted on 2014-03-14
4
386 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 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 57

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 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