How to get php while loop inside javascript -
i struggling solution of problem.
i have php file , javascript while.
my php file looks this:
<?php $sql = "select * flats"; $result = $conn->query($sql); if ($result->num_rows > 0) // output data of each row while($row = $result->fetch_assoc()) { $flat_id = $row["flat_id"]; $price = $row["price"]; $address = $row["address"]; $bedrooms = $row["bedroom"]; $maxguests = $row["maxguests"]; $area = $row["area"]; $lat = $row["lat"]; $lng = $row["lng"]; $map_flats = "{ title : 'property', image : '1-1-thmb.png', type : 'for sale', price : '$price', address : '$address', bedrooms : '$bedrooms', bathrooms : '$maxguests', area : '$area', position : { lat : $lat, lng : $lng }, markericon : 'marker-green.png' },"; echo $map_flats; }} else { echo "0 results"; } $conn->close(); ?>
and want show content of $map_flats inside js.
var flats = [$map_flats];
could please me how make working?
thanks in advance,
tibor.
javascript need structure in json format, , needs echo
ed output. this:
var flats = <?php echo json_encode($map_flats); ?>;
though looks $map_flats
isn't actually php object, formatted json string? easier use php objects in server-side code , json in client-side code, if don't need json-encode output can echo as-is:
var flats = <?php echo $map_flats; ?>;
Comments
Post a Comment