in there i have profile_driver
, i want to make this data will show when i input the parameter first. example input the 'id'
$command = $_GET['command'];
switch ($command) {
case 'profile_driver':
if(!empty($_REQUEST['id'])) {
$data = array( "api_status" => 0, "api_message" => "cant Find data");
echo json_encode($data);
} else {
$loop = new WP_Query(
array(
'post_type' => 'drivers'
)
);
if( $loop->have_posts() ) :
$data = array( "api_status" => 1, "api_message" => "success");
$meta = array();
while ( $loop->have_posts() ) : $loop->the_post();
$meta[] = array(
"id" => get_the_ID(),
"post_name" => get_the_title(),
"username" => get_post_meta( get_the_ID(), 'username', true ),
"password" => get_post_meta( get_the_ID(), 'password', true ),
"email" => get_post_meta( get_the_ID(), 'email', true ),
"phone" => get_post_meta( get_the_ID(), 'phone', true ),
);
endwhile;
endif;
echo json_encode($meta);
}
break;
}
here when i try to run in postman( i have the data with the right id ) :
have someone help me improve my code so my code can be work like what i want ?
You need to place if
condition after this line case 'profile_driver':
before returning data to web-service.
Try dumping the var_dump($_REQUEST)
to check if the id parameter is in request or not OR
it is empty.
if(!empty($_REQUEST['id'])) {
echo 'Please enter id';
} else {
// your rest of the code goes here
}
Also you need to change the loop.
$data = array("api_status" => 1, "api_message" => "success", "result" => "");
if( $loop->have_posts() ) :
while($loop->have_posts()) : $loop->the_post();
$data['result'][] = array(
"id" => get_the_ID(),
"post_name" => get_the_title(),
"username" => get_post_meta(get_the_ID(), 'username', true),
"password" => get_post_meta(get_the_ID(), 'password', true),
"email" => get_post_meta(get_the_ID(), 'email', true),
"phone" => get_post_meta(get_the_ID(), 'phone', true),
);
endwhile;
endif;
echo json_encode($data);