0

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

5

1 Answer 1

1

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.

Note:

mysql_* is deprecated as of and removed as of . So instead use mysqli_* or PDO.
Why shouldn't I use mysql_* functions in PHP?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mega6382 for your Answer
@coco_nk4l If the answer was helpful, please be sure to accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.