php - Symfony JsonResponse: Fetching each element of multidimensional array returned by symfony controller -
basically trying trying fetch each element array of "arrays" passed symfony controller when try fetch rows of table. controller working absolutely fine, , giving me output:
[{"id":1,"name":"johnsonn's baby shampoo","dsc":"shampoo","pic":"572f8a02d59b3.jpg","company":4,"type":"baby products","price":150,"sold":null,"stock":200},{"id":2,"name":"johnson's soap","dsc":"baby soap","pic":"57303fc35f72c.jpg","company":4,"type":"baby products","price":52,"sold":null,"stock":1000}]
and javascript fetching output following:
<script> function fetchprod() { $.ajax({ url: "/show/prod", datatype: "json" }).success(function(data){ $('#container').html(json.stringify(data)); }); } fetchprod(); setinterval(function () {fetchprod()}, 3000); </script>
what wanna fetch each element of each array separately can place create div out of them arranged in grid properly.
also array fetched symfony controller (if need info):
public function showaction($slug){ $em = $this->getdoctrine()->getmanager(); if($slug == 'prod'){ $repo = $em->getrepository('systembundle:products'); $q = $repo->createquerybuilder('u') ->getquery() ->getarrayresult(); return new jsonresponse($q); } }
since you're using jquery, maybe use each
method:
// ajax call .success( function (data) { $.each(data, function(key, value) { var div = '<div>' + json.stringify(value) + '</div>'; $('#container').append(div); }); }); // close function
in way iterate on every object in array, place in div, , after append #container
.
it should create like:
<div id="container"> <div>{"id":1,"name":"johnsonn's baby shampoo","dsc":"shampoo","pic":"572f8a02d59b3.jpg","company":4,"type":"baby products","price":150,"sold":null,"stock":200}</div> <div>{"id":2,"name":"johnson's soap","dsc":"baby soap","pic":"57303fc35f72c.jpg","company":4,"type":"baby products","price":52,"sold":null,"stock":1000}</div> </div>
Comments
Post a Comment