繁体   English   中英

如何过滤json编码数组计数?

[英]How to filter json encode array count?

在我的示例中,我有一个来自AJAX的PHP脚本,可对名为“ Richard”的每个人进行汇总。 我想通过计算与某些输入值的年龄相匹配的名叫Richard的人来进一步修改它,如$theirage所示。

具体来说,我想将这两个年龄相加,仅在我的数组中显示等于20的年龄。 $row[age]对应于我数据库中的值age ,而$theirage是用户输入。

最后,在这种情况下,我只想统计等于20的总年龄。

    $theirage = $_GET['theirage'];
    $query = mysqli_query($con, "SELECT count(*) as cnt FROM members WHERE
    name = 'Richard'
    ");
    $row = mysqli_fetch_assoc($query);
    $bow = $row['age'] + $theirage;
    $if ($bow = 20){
    $person = $row['cnt'];} 

echo json_encode( array(
    'person' => $person
) );    

我该怎么办?

我建议您查询members表以计算符合条件的所有人:特定名称'Richard'和特定年龄$_GET['theirage']

<?php
$theirAge = $_GET['theirage'];

// This is important: since we're using $theirAge in query string, we need to escape it
$theirAge = mysqli_escape_string($theirAge);

// Let's query all members whose name is Richard and age is equal to $_GET['theirage']
$query = mysqli_query($con, "SELECT count(*) FROM members WHERE name = 'Richard' AND age + $theirAge = 20");

$row = $query->fetch_row();

$totalRichardsOfGivenAge = $row[0]; // here is your result.

// And now let's return JSON
echo json_encode($totalRichardsOfGivenAge)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM