php - Get JSON to return list Book using while loop -
i want create list of books can return valid json output. there should 1 json output outputs list of books. have @ moment problem last book of while loop added list book want return.
//return each book found given search. while(list($book_id, $title, $authors, $description, $price) = mysqli_fetch_row($search)){ $thisbook = array("book_id"=>$book_id,"title"=>$title,"authors"=>$authors,"description"=>$description,"price"=>$price); $book = array(); $book[] = $thisbook; } //json return $success = true; $message = "success"; $output = $book; echo json_encode($output);
how this? thanks
$book = array(); while(list($book_id, $title, $authors, $description, $price) = mysqli_fetch_row($search)){ $thisbook = array("book_id"=>$book_id,"title"=>$title,"authors"=>$authors,"description"=>$description,"price"=>$price); $book[] = $thisbook; }
you over-writting array inside loop getting last data. declare $book = array();
outside loop
Comments
Post a Comment