duanchazhou6779 2016-10-24 07:28
浏览 58
已采纳

在Wordpress查询中显示具有特定参数的数据

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 ?

  • 写回答

1条回答 默认 最新

  • dongzhang3482 2016-10-24 07:43
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!