Fügen Sie jeder Zeile eines mehrdimensionalen Array -Arrays eine neue berechnete Spalte hinzuPhp

PHP-Programmierer chatten hier
Guest
 Fügen Sie jeder Zeile eines mehrdimensionalen Array -Arrays eine neue berechnete Spalte hinzu

Post by Guest »

Ich habe eine PHP -Schleife, die Daten aus meiner Tabelle erhält und sie in ein Array drückt. < /p>

Code: Select all

$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);
Ich möchte dem Array mit dem Namen Points einen weiteren Schlüssel/Wert hinzufügen und seinen Wert als $ Total .

Ich habe versucht, $ temp ['Punkte'] = $ Total < /code> zu machen, aber das hat es außerhalb des Arrays und nicht mit den äußeren Schleifendaten gestellt. < /p>

Dies ist das Ergebnis der Funktion: < /p>

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

Ich möchte die Punkte für jedes dieser Kinder zeigen, aber ich bin mir nicht sicher, wie ich es dem Teil des Arrays hinzufügen soll. < /p>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post