jQuery: How to parse a multidemensional array?

Posted by Allen G on Stack Overflow See other posts from Stack Overflow or by Allen G
Published on 2010-04-08T12:02:28Z Indexed on 2010/04/08 12:03 UTC
Read the original article Hit count: 318

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about php