在json Response中包含函数结果

时间:2017-09-02 13:36:54

标签: php json

我不是PHP开发人员。我有一个Android应用程序,其中我使用以下API显示共享和类似计数

mysqli_query($conn,"SET NAMES utf8");
$sql = "SELECT tbl_quotes.*,au_picture as picture FROM tbl_fav INNER JOIN tbl_quotes INNER JOIN tbl_category  ON user_id='".$userId."' AND qu_author=_auid  AND fav_qu_id=_quid Order By '".$order."' DESC LIMIT ".$limit." OFFSET ".$offset;


$result = mysqli_query($conn, $sql);

$arr = array();
while($r = mysqli_fetch_assoc($result)) {

    $arr[] = $r;
}

echo json_encode($arr, JSON_UNESCAPED_UNICODE);

mysqli_close($conn);

现在我想转换K风格的数字,如下面的代码

if ($value > 999 && $value <= 999999) {
    $result = floor($value / 1000) . ' K';
} elseif ($value > 999999) {
    $result = floor($value / 1000000) . ' M';
} else {
    $result = $value;
}

like field是qu_like,share field是qu_share。任何人都可以请帮助我如何使用此功能转换数字并将其包含在我的json?

由于

2 个答案:

答案 0 :(得分:0)

我已完成以下代码......

function Get_convert_to($value){


    if ($value > 999 && $value <= 999999) {
    $result = floor($value / 1000) . ' K';
} elseif ($value > 999999) {
    $result = floor($value / 1000000) . ' M';
} else {
    $result = $value;
}


    return $result;
    }

工作正常......

答案 1 :(得分:0)

要在JSON编码之前修改数据库中返回的数组,您可以执行以下操作。

<?php
//1. create the formatting function
function formatValue($value) {
    if ($value > 999 && $value <= 999999) {
        return $result = floor($value / 1000) . ' K';
    } elseif ($value > 999999) {
        return $result = floor($value / 1000000) . ' M';
    } else {
        return $result = $value;
    }
}

mysqli_query($conn,"SET NAMES utf8");
$sql = "SELECT tbl_quotes.*,au_picture as picture FROM tbl_fav INNER JOIN tbl_quotes INNER JOIN tbl_category  ON user_id='".$userId."' AND qu_author=_auid  AND fav_qu_id=_quid Order By '".$order."' DESC LIMIT ".$limit." OFFSET ".$offset;


$result = mysqli_query($conn, $sql);
//2. check if there's a result
if($result){
    $arr = array();
    while($r = mysqli_fetch_assoc($result)) {

        $arr[] = $r;
    }
    //3. free result set
    mysqli_free_result($result);

    //4. update qu_like and qu_share keys in the $arr array before creating the json
    foreach ($arr as &$value) {
        $value['qu_like'] = formatValue($value['qu_like']);
        $value['qu_share'] = formatValue($value['qu_share']);
    }

    echo json_encode($arr, JSON_UNESCAPED_UNICODE);
}

mysqli_close($conn);

另外我认为你应该在SQL语句中绑定参数。检查下面的sql是否有效。有关详细信息,请查看http://php.net/manual/en/mysqli-stmt.bind-param.php

$sql = "SELECT tbl_quotes.*,au_picture as picture FROM tbl_fav INNER JOIN tbl_quotes INNER JOIN tbl_category  ON user_id=? AND qu_author=?  AND fav_qu_id=? Order By ? DESC LIMIT ? OFFSET ?";
if($stmt = $mysqli->prepare($sql)) {
    /*i - integer
      d - double
      s - string
      b - BLOB
      change first argument of bind_params to suit your variables :)
    */
    $stmt->bind_params("siisii",$userId,_auid,_auid,$order,$limit,$offset);
    $stmt->execute();
    /* bind result variables */
     $stmt->bind_result($col1,$col2,$col2);
    //then replace while statement above as below
    while ($stmt->fetch()) {
      //... echo $col1 etc
    }
}