pquinn Posted March 14, 2011 Share Posted March 14, 2011 Hello, I have a slight issue with some of my code; basically I have used an array_sum to calculate all entries made to users UPNs (a unique identifier). The array is adding or subtracting points where appropriate, but is displaying at the top of my webpage; 'Warning: array_sum() [function.array-sum]: The argument should be an array in /home/pquinn/public_html/demo/index.php on line 74' code on line 72 - 74: foreach($users as $row => $value){ $content .= "<tr>"; $current_points = array_sum($total_rewards[$row]) - array_sum($total_sanctions[$row]); But where I have the php code the code is working and displays: Surname - User Forename - Demo Form Class - 8de Points - 535 So the array is working in some way. The code I am using is, <?php $starting_points = 0; /* Connecting, selecting database */ include_once '/home/pquinn/public_html/demo/includes/db.php'; dbConnect("pquinn_demo"); //$result = mysql_query($sql) or die("Query failed : " . mysql_error()); $sql = "SELECT * FROM users ORDER BY formclass"; $result = mysql_query($sql) or die("Query failed : " . mysql_error()); while($row = mysql_fetch_array($result)){ $id = $row[upn]; $users[$id][surname] = "<a href=\"user.php?id=$row[username]\"><u>$row[surname]</u></a>"; $users[$id][forename] = $row[forename]; $yeargroup = $row[yeargroup]; switch($yeargroup){ case ($yeargroup == date('Y')): $stryeargroup = '8'; break; case ($yeargroup == date('Y')+1): $stryeargroup = '9'; break; case ($yeargroup == date('Y')+2): $stryeargroup = '10'; break; case ($yeargroup == date('Y')+3): $stryeargroup = '11'; break; case ($yeargroup == date('Y')+4): $stryeargroup = '12'; break; case ($yeargroup == date('Y')+5): $stryeargroup = '13'; break; case ($yeargroup == date('Y')+6): $stryeargroup = '14'; break; } $users[$id][fomclass] = $stryeargroup.$row[formclass]; } $sql = "SELECT upn, points FROM rewards"; $result = mysql_query($sql); while($row = mysql_fetch_array($result, MYSQL_NUM)){ $id = $row[0]; $total_rewards[$id][] = $row[1]; } $sql = "SELECT upn, points FROM sanctions"; $result = mysql_query($sql); while($row = mysql_fetch_array($result, MYSQL_NUM)){ $id = $row[0]; $total_sanctions[$id][] = $row[1]; } // Get points $current_points = array_sum($total_rewards) - array_sum($total_sanctions); $points = $current_points + $starting_points; // Print out the rewards $content .= "<tr>\n"; $content .= "<th align=\"left\">Surname</th>\t\n"; $content .= "<th>Forename</th>\t\n"; $content .= "<th>Form Class</th>\t\n"; $content .= "<th>Points</th>\t\n"; $content .= "</tr>\n"; foreach($users as $row => $value){ $content .= "<tr>"; $current_points = array_sum($total_rewards[$row]) - array_sum($total_sanctions[$row]); $points = $current_points + $starting_points; foreach($value as $var => $value){ $content .= "<td>$value</td>\t\n"; } $users[$row][points] = $points; $content .= "<td>$points</td>\t\n"; $content .= "</tr>"; } ?> Any help would be greatly appreciated, Many Thanks and Kindest Regards, PQ Quote Link to comment https://forums.phpfreaks.com/topic/230580-array-sum-function-problem/ Share on other sites More sharing options...
RussellReal Posted March 14, 2011 Share Posted March 14, 2011 its a warning not a fatal error, the problem isn't with your code, its the fact that when there ARE no results to tally together, for example, you have a user, but that user hasn't earned any rewards.. his rewards won't be present, which means $total_rewards[hisUserId] will not be an array, and when you pass it into the function array_sum, it expects an array, and doesn't get it, to avoid this error message.. Try this: $current_points = @array_sum($total_rewards[$row]) - @array_sum($total_sanctions[$row]); Quote Link to comment https://forums.phpfreaks.com/topic/230580-array-sum-function-problem/#findComment-1187310 Share on other sites More sharing options...
pquinn Posted March 14, 2011 Author Share Posted March 14, 2011 Thanks dude really appreciate the help and another lesson learnt Thanks again, P Quinn Quote Link to comment https://forums.phpfreaks.com/topic/230580-array-sum-function-problem/#findComment-1187380 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.