jQuery: How to parse a multidemensional array?
- by Allen G
I'm not sure if the array is too deep or what, but I can't seem to get anything other than the keys and undefines. Help please!
I'm using Codeigniter for development. Here is the PHP then the jQuery:
$term = $this->input->post('search_term');
$this->db->like('book_title', $term);
$search_query = $this->db->get('books');
$return = array();
$i = 0;
if ($search_query->num_rows() > 1) {
foreach($search_query->result() as $s) {
$return['books']['book_id'] = $s->book_id;
$return['books']['book_title'] = $s->book_title;
$return['books']['book_price'] = $s->book_price;
$i++;
}
} elseif ($search_query->num_rows() == 1) {
echo 1;
$i = 0;
$return['book_id'] = $search_query->row('book_id');
$return['book_title'] = $search_query->row('book_title');
$return['book_price'] = $search_query->row('book_price');
} elseif ($search_query->num_rows() == 0) {
echo 0;
}
echo json_encode($return);
$("#search").change(function() {
var searchTerm = $(this).val();
$.post("/contentcreator/search_by_term", { search_term: searchTerm }, function(data) {
$("#book_scroller").empty();
var lengthHolder = data.books;
for (var i = 0; i data.books.length; i++) {
var row = '<li id="book_item_' + l + '">' + data.books['book_title'] +'</li>';
$("#book_scroller").append(row);
};
i++;
}, "json");
});
Thanks!