0

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 ) :

here

have someone help me improve my code so my code can be work like what i want ?

APSB
  • 577
  • 4
  • 11
  • 28

1 Answers1

1

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);
Noman
  • 4,088
  • 1
  • 21
  • 36
  • oke, its can get the the id but im still get wrong response have u help me ? check my edit script thanks or iam still wrong to place it ? – APSB Oct 24 '16 at 07:54
  • @AdamProjo it seems your file contains `var_dump` find var_dump and remove it. then you will get the proper json response. – Noman Oct 24 '16 at 08:14