Ajay Kumar Ajay Kumar - 7 months ago 15
PHP Question

How to prepare a table format from a JSON in PHP

I have JSON string and it has

n
number of item, I want to convert it into a table

Json String

{"HotelSearchResult":{"ResponseStatus":1,"Error":{"ErrorCode":0,"ErrorMessage":""},"TraceId":"70e718ad-a9c2-4e82-9125-e3ee2f1573f1","CityId":"15254","CheckInDate":"2016-03-22","CheckOutDate":"2016-03-24","PreferredCurrency":"INR","NoOfRooms":1,"RoomGuests":[{"NoOfAdults":1,"NoOfChild":0,"ChildAge":[]}],"HotelResults":[{"ResultIndex":1,"HotelCode":"348043","HotelName":"Base Taupo - Hostel \/ Backpacker","HotelCategory":"","StarRating":2,"HotelDescription":"Property Location A stay at Base Taupo - Hostel \/ Backpacker places you in the heart of Taupo, walking distance from Taupo Museum and Art Gallery and Barbary. This hostel is within Near Taupo Museum and Art Gallery ","HotelPromotion":"","HotelPolicy":"","Price":{"CurrencyCode":"INR","RoomPrice":1837.47,"Tax":0.00,"ExtraGuestCharge":0,"ChildCharge":0,"OtherCharges":0,"Discount":0.00,"PublishedPrice":1837.47,"PublishedPriceRoundedOff":1837,"OfferedPrice":1837.47,"OfferedPriceRoundedOff":1837,"AgentCommission":0.00,"AgentMarkUp":0.00,"ServiceTax":0,"TDS":0},"HotelPicture":"http:\/\/www.travelboutiqueonline.com\/imageresource.aspx?img=ckZZ2jR\/KJFL8uCNV\/6rUdikLJoSoWwsQ4+ucla4wpAL9fwTuqkLXc3dJ9jA0ZdlXkihFvi968p+KSfi8oYVU8Tj278FBz\/1XkfoRZtaubj\/jenFXq1xBUpWskdqQlXdlSq2E+AsRgAHCH7MUGE+VOR1GoL89wEcl7bKkOpOKoV8u5\/s6kQwUbyMvkUTHXfr\/OjXIi4RNZfESY3+adSUVfiu7NVoTmH5TlFdNsylMzU=","HotelAddress":"7 Tuwharetoa Street, Taupo, , 3330, , , ","HotelContactNo":"","HotelMap":null,"Latitude":"","Longitude":"","HotelLocation":null,"SupplierPrice":null,"RoomDetails":[]},{"ResultIndex":2,"HotelCode":"444277","HotelName":"Camellia Court Family Motel","HotelCategory":"","StarRating":3,"HotelDescription":"Property Location Located in Taupo, Camellia Court Family Motel is convenient to Taupo Bungy and Taupo Museum and Art Gallery. This motel is within close proximity of Barbary and Near Taupo Bungy ","HotelPromotion":"","HotelPolicy":"","Price":{"CurrencyCode":"INR","RoomPrice":2721.38,"Tax":0.00,"ExtraGuestCharge":0,"ChildCharge":0,"OtherCharges":0,"Discount":0.00,"PublishedPrice":2721.38,"PublishedPriceRoundedOff":2721,"OfferedPrice":2721.38,"OfferedPriceRoundedOff":2721,"AgentCommission":0.00,"AgentMarkUp":0.00,"ServiceTax":0,"TDS":0},"HotelPicture":"http:\/\/www.travelboutiqueonline.com\/imageresource.aspx?img=ckZZ2jR\/KJFL8uCNV\/6rUdikLJoSoWwsQ4+ucla4wpAL9fwTuqkLXc3dJ9jA0ZdlXkihFvi968p+KSfi8oYVU8Tj278FBz\/1XkfoRZtaubj\/jenFXq1xBQF2KZhLrrzlriBhuyokbu67Iabfrn\/oYXyyOQZ\/9ufWgRFwVHZjjFr3Cndi8RUX2MpUZp2C+OI\/3j7hNbZEVxU3aohoWZK3Xd79WBVqb\/DrXZotWZd4wAU=","HotelAddress":"50 Tonga Street, Taupo, , 2730, , , ","HotelContactNo":"","HotelMap":null,"Latitude":"","Longitude":"","HotelLocation":null,"SupplierPrice":null,"RoomDetails":[]},}};


complete data must see it

https://drive.google.com/open?id=0B9VV_J4sKTataU1KdUtReFJyaUk





Desired HTML output:
enter image description here

Answer

this is that you are looking for

$json='{"HotelSearchResult":{"ResponseStatus":1,"Error":{"ErrorCode":...};
echo '<table><thead><tr><td>Hotel Name </td><td>Price</td><td>Action</td></tr></thead><tbody>';
$str = json_decode($json, true);
foreach ($str['HotelSearchResult']['HotelResults'] as $key=> $Result) {
    echo "<tr><td>".$Result['HotelName']."</td>
        <td>".$Result['Price']['RoomPrice']."</td>
        <td><button type='button' value=".$Result['ResultIndex']." >Select</button></td>
    </tr>";
}
echo '</tbody></table>';
Comments