PHP推入数组

时间:2016-12-29 01:18:57

标签: php arrays

我有一个PHP循环,它从我的表中获取数据并将其推送到数组中。

$children = mysql_query("SELECT c.id, c.name, c.age, c.photoName, c.panelColor FROM children as c");
$temp = array();

while ($child = mysql_fetch_assoc($children)) { 

    // Get the child's reatings
    $ratings = mysql_query('
        SELECT r.behaviourID, t.points, t.typeName 
        FROM  behaviourRatings as r 
        JOIN  behaviourTypes as t 
        ON    r.behaviourID = t.typeID 
        WHERE r.childID = ' . $child['id']);

    // Loop over the ratings
     $totalPoints = 0;
     while ($childRatings = mysql_fetch_array($ratings)){
       $totalPoints = ($totalPoints + $childRatings['points']);
     }

     // We can only max out at our set max
     if(($totalPoints + $maxPoints) > $maxPoints) {
        $total = $maxPoints;
     } else if($totalPoints < 0){
        $total = ($maxPoints + $totalPoints);
     }else{
        $total = ($maxPoints - $totalPoints);
     }

     // Set the child array
     $temp[] = $child;
 }

$response = array();
$response['timestamp'] = $currentmodif;
$response['children'] = $temp;
echo json_encode($response, JSON_PRETTY_PRINT);

我想在名为points的数组中添加另一个键/值,并将其值指定为$total

我尝试过$temp['points'] = $total,但是它把它放在数组之外而不是外部循环数据。

这是该功能的结果:

{
    "timestamp": 1482918104,
    "children": [
        {
            "id": "1",
            "name": "Maya",
            "age": "5",
            "photoName": "maya.png",
            "panelColor": ""
        },
        {
            "id": "2",
            "name": "Brynlee",
            "age": "3",
            "photoName": "brynlee.png",
            "panelColor": "green"
        }
    ]
}

我想显示每个孩子的分数,但我不确定如何将它添加到数组的那一部分。

2 个答案:

答案 0 :(得分:3)

在将该变量添加到<?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } ?> 数组之前,您应该将其添加到$child变量中:

$temp

答案 1 :(得分:0)

此外,您可以通过在一个查询中获取所有孩子的评分来减少对数据库的请求。

这样的事情:

$children = mysql_query("SELECT c.id, c.name, c.age, c.photoName, c.panelColor FROM children as c");

$childrenIds = [];

while ($child = mysql_fetch_assoc($children)) {
     $childrenIds[] = $child['id'];
}

if (!empty($childrenIds)) {
    $ratings = mysql_query('
         SELECT r.behaviourID, t.points, t.typeName 
         FROM  behaviourRatings as r 
         JOIN  behaviourTypes as t 
         ON    r.behaviourID = t.typeID 
         WHERE r.childID IN (' . implode(',', $childrenIds) . ')');
}
相关问题