2016-12-22 9 views
0

私はAJAXから、名付けみんなの集計し、PHPスクリプト持つ「リチャード」。 $theirageに示したように、私は、さらにいくつかの入力値の年齢に合わせリチャードという名前の人を、カウントすることによって、これを変更したいと思います。jsonエンコード配列数をフィルタリングする方法は?私の例で

具体的には、私は2つの年代を総括し、唯一の私のアレイで20と等しいものを表示したいと思います。 $theirageがユーザ入力である$row[age]は、私のDBの値ageに相当します。

終わりに私はこのケースでは、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 
));  

どうすればよいですか?特定の名前'Richard'と特定の年齢$_GET['theirage']

答えて

1

は、私はあなたの条件に一致しているすべての人をカウントするmembersテーブルを照会示唆

<?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) 
+0

はい]を、私は2歳を合計し、もし結果が= 20を見ています。これがわかっている限り、私はこれをmySQLで行うことはできません。 – abrahamlinkedin

+1

それを明確にしていただけますか?あなたは年齢+ someVariableが20に等しいすべてのリチャーズの数を取得したいですか? –

+1

コードを更新しました。私たちは年齢+ theirAge = 20のRichardsだけを数えます。 –

関連する問題