i have some php to show result as JSON like this
$dp = $_GET['dp'];
$query = mysql_query("select trip_id, trip_desc from current_data where dispatcher='".$dp."'");
$json = array();
while($row = mysql_fetch_assoc($query)){
$tripdata["code"]=$row["trip_id"]." - ".$row["trip_code"];
$json[] = $tripdata;
}
echo json_encode($json);
and result like this
[{"code":"S1.001 - UK"},{"code":"S1.002 - US"},{"code":"S1.003 - CA"}]
How to add value in First Result of my JSON, i want to result like this?
[**{"code":"Select Country"}**,{"code":"S1.001 - UK"},{"code":"S1.002 - US"},{"code":"S1.003 - CA"}]
Thanks
Push the first value to the array before the while loop:
$json[] = [
'code' => 'Select Country',
];
while($row = mysql_fetch_assoc($query)){
$tripdata["code"]=$row["trip_id"]." - ".$row["trip_code"];
$json[] = $tripdata;
}
echo json_encode($json);
This will add a the desired value in the beginning of json.
mysql_*
is deprecated as of php-5.5 and removed as of php-7. So instead use mysqli_*
or PDO
.
Why shouldn't I use mysql_* functions in PHP?