Re: Array problem
| From: | noginn | Date: | Mon, 22 Mar 2004 11:30:51 +0000 |
| Subject: | Re: Array problem | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-181122@lists.php.net to get a copy of this message | ||
Ye that is true, however to create the totals of each columns I had to somehow do this.
I have fixed the problem in a way, but it was probably a very crude way of doing so.
$rowtotal = $rowtotal + $sum;
if(!empty($columntotals[$count])) {
$columntotals[$count] = $columntotals[$count] + $sum;
}
else {
$columntotals[$count] = $sum;
}
$count++;
So now it won't try and add itself if it is empty.
But if anyone has more ideas of how I can create a cleaner piece of code then please let me know! :)
-noginn
Firman Wandayandi wrote:
Hi noginn, $columntotals[$count] = $columntotals[$count] + $sum;^---------------------^ ERROR HERESeem you tried to assign columntotal[index] with itself and you never defined it. You should tried this one. => $columntotals[$count] = $sum; Is right? Sorry, I confuse with your words "total of totals"? Good Luck, Firman ----- Original Message ----- From: "noginn" <php@noginn.com> To: <php-general@lists.php.net> Sent: Monday, March 22, 2004 12:33 AM Subject: [PHP] Array problemThis has been confusing me a little for a few hours now. Heres a snip of my code which is causing the problem: $content = "<table border='0' cellspacing='0' cellpadding='5'>\n"; $content .= "<tr>\n"; $content .= "<td> </td>\n"; $tasks = new dbconnect; $tasks->connect(); $tasks->query("SELECT tid, tname FROM tasks"); while(list($tid, $tname) = $tasks->fetch_rows()) {$content .= "<td valign='top' align='center'><spanclass='highlight'>$tname</span></td>\n"; } $content .= "<td> </td>\n"; $content .= "</tr>\n"; $projects = new dbconnect; $projects->connect(); $projects->query("SELECT pid, pname FROM projects"); * $columntotals = array();* $colour = 0; while(list($pid, $pname) = $projects->fetch_rows()) {$tasks->data_seek(0); $rowtotal = 0; $count = 0; if ($colour % 2) { $bgcolour = "#FFFFFF"; } else { $bgcolour = "#F9F9F9"; } $colour++; $content .= "<tr>\n"; $content .= "<td valign='top' align='center'><spanclass='highlight'>$pname</span></td>\n";while(list($tid, $tname) = $tasks->fetch_rows()) { $logs = new dbconnect; $logs->connect(); $logs->query("SELECT SUM(hours) from logs WHERE pid = '$pid'AND tid = '$tid' AND date >= '$sdate' AND date <= '$edate'");list($sum) = $logs->fetch_rows();if (!$sum) { $sum = 0; }$rowtotal = $rowtotal + $sum; *$columntotals[$count] = $columntotals[$count] + $sum;* $count++;$content .= "<td bgcolor='$bgcolour'align='center'>$sum</td>\n";} $content .= "<td align='center'><b>$rowtotal</b></td>\n";} $content .= "</tr>\n"; $content .= "<tr>\n"; $content .= "<td> </td>\n"; $sumofcolumntotals = 0; for ($i=0; $i<sizeof($columntotals); $i++) {$sumofcolumntotals = $sumofcolumntotals + $columntotals[$i]; $content .= "<td align='center'><b>$columntotals[$i]</b></td>\n";} $content .= "<td align='center'><b>$sumofcolumntotals</b></td>\n"; $content .= "</tr>\n"; $content .= "</table>\n"; I have made the lines inwhich I know are causing problems in bold. Basicly, I am creating a report of some data and need to count up totals of each column and then again total the totals if you get me. Here is the errors I am getting. *Notice*: Undefined offset: 0 in *C:\WWW\Apache2\htdocs\php\coursework\reports_projects.php* on line *58* *Notice*: Undefined offset: 1 in *C:\WWW\Apache2\htdocs\php\coursework\reports_projects.php* on line *58* *Notice*: Undefined offset: 2 in *C:\WWW\Apache2\htdocs\php\coursework\reports_projects.php* on line *58* *Notice*: Undefined offset: 3 in *C:\WWW\Apache2\htdocs\php\coursework\reports_projects.php* on line *58 *Now I understand in a way why its happening, because im trying to insert into $columntotals something which isnt valid, but I can't think of a way to stop this at the moment. Hope to hear soon, thanks in advance guys. -noginn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php