php - Use Jquery to getJSON data feed, and display multiple json array objects in HTML -
i using jquery , getjson data feed constructed php. feed displays fine when visiting php page. problem running json feed returns multiple objects when requested in jquery, , don't know how write jquery display objects , data.
jquery:
$(document).ready(function () { $("#display_results").hide(); $("#searchbutton").click(function (event) { $("#display_results").show(); event.preventdefault(); search_ajax_way(); }); $("#search_results").slideup(); $("#searchbox").keyup(function (event) { $("#display_results").show(); }); }); function search_ajax_way() { //var search_this=$("#searchbox").val(); //$.post("http:/url.com/folder/cl/feed.php", {city : search_this}, function(data){ //$("#display_results").html(data); //}); var search_this = $("#searchbox").val(); $.getjson('http://url.com/app/cl/disp_byowner_feed.php', { city: search_this }, function (result) { $.each(result, function (i, r) { console.log(r.seller); window.title = r.title; window.seller = r.seller; window.loc = r.location; (plan on listing keys listed in data feed below here) }); console.log(result); $("#display_results").html(window.seller + < need list keys / values here > ); }); } php (constructs json feed):
$city = 'kanosh'; $s = "select * `list` `city` '%".$city."%'"; $res = $mysqli->query($s) or trigger_error($mysqli->error."[$s]"); $a = array(); while($row = $res->fetch_array(mysqli_both)) { $a[] = array( 'title' => $row['title'], 'price' => $row['price'], 'rooms' => $row['rooms'], 'dimensions' => $row['dimensions'], 'location' => preg_replace('pic\u00a0map', '', $row['location']), 'price' => $row['price'], 'address' => $row['address'], 'seller' => $row['seller'], 'href' => $row['href'], 'date' => $row['date'] ); } header('content-type: application/json'); echo json_encode($a); $res->free(); $mysqli->close(); sample json feed:
[{ "title": "great ski-in location seller financing available ", "price": " (park city near resort) ", "rooms": "", "dimensions": "", "location": "", "address": "crescent road @ 3 kings dri", "seller": "real estate - owner", "href": "http:\/\/saltlakecity.craigslist.org", "date": "20140811223115" }, { "title": "prospector building 1 - tiny condo, great location - owner financing", "price": "$75000", "rooms": false, "dimensions": "", "location": " prospector square park city ut", "address": "", "seller": "real estate - owner", "href": "http:\/\/saltlakecity.craigslist.org", "date": "20140811223115" }]
your output array of json objects. fortunately, javascript convenient manipulating json (actually, why json created..), , jquery convenient manipulating dom.
to parse result, can iterate through array, construct whatever html string need within array, , insert dom using jquery.
here simple example lists :
var html = ""; (var = 0; < result.length; i++) { //assuming result in json array html += '<ul id = 'house_' + i>'; (var property in result[i]) { //read object properties html += '<li>' + property + ' : ' + result[i][property] + '</li>'; } html += '</ul>'; }; $("whatever_div").html(html); if want display of properties, can read them separately , additional formatting (for date example). useful give different html objects ids corresponding represent.
Comments
Post a Comment