rcowen00
asked on
Array problem
The attached script is supposed to build a chart, but it is not and I get this error
Warning: Division by zero in /home/loanmod1/domains/erm qc.com/pri vate_html/ include/fp df/diag.ph p on line 124
Notice: Array to string conversion in /home/loanmod1/domains/erm qc.com/pri vate_html/ include/fp df/diag.ph p on line 125
Fatal error: Unsupported operand types in /home/loanmod1/domains/erm qc.com/pri vate_html/ include/fp df/diag.ph p on line 77
The query is running correctly and does pull data. The problem (I believe) is that the $value is not in the correct format or am I not associating them correctly? BarDiagram (Line 14) is looking for results like
$data = array('Assets' => 48, 'Income' => 25, 'Employment' => 10);
Warning: Division by zero in /home/loanmod1/domains/erm
Notice: Array to string conversion in /home/loanmod1/domains/erm
Fatal error: Unsupported operand types in /home/loanmod1/domains/erm
The query is running correctly and does pull data. The problem (I believe) is that the $value is not in the correct format or am I not associating them correctly? BarDiagram (Line 14) is looking for results like
$data = array('Assets' => 48, 'Income' => 25, 'Employment' => 10);
$result = mysql_query($allCat) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$value[ $row['Cat'] ] = $row['Ct'];
}
$data = array ($value);
$pdf->SetFont('Arial', 'BIU', 12);
$pdf->Cell(0, 5, '2 - Bar diagram', 0, 1);
$pdf->Ln(8);
$valX = $pdf->GetX();
$valY = $pdf->GetY();
if ( count( $data ) != 0 ){
$pdf->BarDiagram (190, 70, $data, '%l : %v (%p)', array(255,175,100));
$pdf->SetXY($valX, $valY + 80);
}
mysql_fetch_array() creates a superset of the data found in mysql_fetch_assoc() so that may not be the problem. But if the code is correct, the data base is mis-designed. In any case, it is a data-dependent problem and we cannot see your data.
Here is how you can visualize the data coming from the data base and know what you've got. After line 3 add var_dump($row); after line 6 add var_dump($data); you will find the output easier to read if you echo "<pre>" before you call var_dump(). Please do that, and post the output in the code snippet so we can see the data. Then we will be able to suggest a solution based on information instead of speculation!
Here is how you can visualize the data coming from the data base and know what you've got. After line 3 add var_dump($row); after line 6 add var_dump($data); you will find the output easier to read if you echo "<pre>" before you call var_dump(). Please do that, and post the output in the code snippet so we can see the data. Then we will be able to suggest a solution based on information instead of speculation!
ASKER
Ray, here are the results. Could you tell me what is mis-designed in the db? Thank you.
Var_Dump after line 3
Var_Dump after line 6
bool(false)
Var_Dump after line 3
array(4) {
[0]=> string(10) "Regulatory" ["Cat"]=> string(10) "Regulatory" [1]=> string(2) "21" ["Ct"]=> string(2) "21" } array(4) { [0]=> string(11) "Application" ["Cat"]=> string(11) "Application" [1]=> string(2) "16" ["Ct"]=> string(2) "16" } array(4) { [0]=> string(17) "Closing Documents" ["Cat"]=> string(17) "Closing Documents" [1]=> string(1) "5" ["Ct"]=> string(1) "5" } array(4) { [0]=> string(4) "HUD1" ["Cat"]=> string(4) "HUD1" [1]=> string(1) "5" ["Ct"]=> string(1) "5" } array(4) { [0]=> string(9) "Insurance" ["Cat"]=> string(9) "Insurance" [1]=> string(1) "3" ["Ct"]=> string(1) "3" } array(4) { [0]=> string(11) "Disclosures" ["Cat"]=> string(11) "Disclosures" [1]=> string(1) "2" ["Ct"]=> string(1) "2" } array(4) { [0]=> string(9) "Appraisal" ["Cat"]=> string(9) "Appraisal" [1]=> string(1) "2" ["Ct"]=> string(1) "2" } array(4) { [0]=> string(6) "Assets" ["Cat"]=> string(6) "Assets" [1]=> string(1) "2" ["Ct"]=> string(1) "2" } array(4) { [0]=> string(8) "Approval" ["Cat"]=> string(8) "Approval" [1]=> string(1) "2" ["Ct"]=> string(1) "2" } array(4) { [0]=> string(14) "Govt Documents" ["Cat"]=> string(14) "Govt Documents" [1]=> string(1) "1" ["Ct"]=> string(1) "1" }
Var_Dump after line 6
bool(false)
Here's that var_dump() output formatted in a way that makes it a little easier to read. As you can see the numeric indexes are redundant. You can get rid of those by using mysql_fetch_assoc(). I do not think they are hurting you here, just extra unnecessary stuff.
We need two more data visualizations (note the variable names carefully). After line 5 var_dump($value); and after line 6 var_dump($data); -- post those here, thanks.
We need two more data visualizations (note the variable names carefully). After line 5 var_dump($value); and after line 6 var_dump($data); -- post those here, thanks.
array(4) {
[0]=> string(10) "Regulatory"
["Cat"]=> string(10) "Regulatory"
[1]=> string(2) "21"
["Ct"]=> string(2) "21"
}
array(4) {
[0]=> string(11) "Application"
["Cat"]=> string(11) "Application"
[1]=> string(2) "16"
["Ct"]=> string(2) "16"
}
array(4) {
[0]=> string(17) "Closing Documents"
["Cat"]=> string(17) "Closing Documents"
[1]=> string(1) "5"
["Ct"]=> string(1) "5"
}
array(4) {
[0]=> string(4) "HUD1"
["Cat"]=> string(4) "HUD1"
[1]=> string(1) "5"
["Ct"]=> string(1) "5"
}
array(4) {
[0]=> string(9) "Insurance"
["Cat"]=> string(9) "Insurance"
[1]=> string(1) "3"
["Ct"]=> string(1) "3"
}
array(4) {
[0]=> string(11) "Disclosures"
["Cat"]=> string(11) "Disclosures"
[1]=> string(1) "2"
["Ct"]=> string(1) "2"
}
array(4) {
[0]=> string(9) "Appraisal"
["Cat"]=> string(9) "Appraisal"
[1]=> string(1) "2"
["Ct"]=> string(1) "2"
}
array(4) {
[0]=> string(6) "Assets"
["Cat"]=> string(6) "Assets"
[1]=> string(1) "2"
["Ct"]=> string(1) "2"
}
array(4) {
[0]=> string(8) "Approval"
["Cat"]=> string(8) "Approval"
[1]=> string(1) "2"
["Ct"]=> string(1) "2"
}
array(4) {
[0]=> string(14) "Govt Documents"
["Cat"]=> string(14) "Govt Documents"
[1]=> string(1) "1"
["Ct"]=> string(1) "1"
}
ASKER
line 5 var_dump($value);
array(10) {
["Regulatory"]=> string(2) "21"
["Application"]=> string(2) "16"
["Closing Documents"]=> string(1) "5"
["HUD1"]=> string(1) "5"
["Insurance"]=> string(1) "3"
["Approval"]=> string(1) "2"
["Disclosures"]=> string(1) "2"
["Appraisal"]=> string(1) "2"
["Assets"]=> string(1) "2"
["Govt Documents"]=> string(1) "1"
}
line 6 var_dump($data);
array(1) {
[0]=> array(10) {
["Regulatory"]=> string(2) "21"
["Application"]=> string(2) "16"
["Closing Documents"]=> string(1) "5"
["HUD1"]=> string(1) "5"
["Insurance"]=> string(1) "3"
["Approval"]=> string(1) "2"
["Disclosures"]=> string(1) "2"
["Appraisal"]=> string(1) "2"
["Assets"]=> string(1) "2"
["Govt Documents"]=> string(1) "1"
} }
array(10) {
["Regulatory"]=> string(2) "21"
["Application"]=> string(2) "16"
["Closing Documents"]=> string(1) "5"
["HUD1"]=> string(1) "5"
["Insurance"]=> string(1) "3"
["Approval"]=> string(1) "2"
["Disclosures"]=> string(1) "2"
["Appraisal"]=> string(1) "2"
["Assets"]=> string(1) "2"
["Govt Documents"]=> string(1) "1"
}
line 6 var_dump($data);
array(1) {
[0]=> array(10) {
["Regulatory"]=> string(2) "21"
["Application"]=> string(2) "16"
["Closing Documents"]=> string(1) "5"
["HUD1"]=> string(1) "5"
["Insurance"]=> string(1) "3"
["Approval"]=> string(1) "2"
["Disclosures"]=> string(1) "2"
["Appraisal"]=> string(1) "2"
["Assets"]=> string(1) "2"
["Govt Documents"]=> string(1) "1"
} }
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That solved that problem. Thank you Ray.
I am now getting the following when I include lines 57 - 70.
Warning: Cannot modify header information - headers already sent by (output started at /home/loanmod1/domains/erm qc.com/pri vate_html/ include/fp df/font/co urier.php: 9) in /home/loanmod1/domains/erm qc.com/pri vate_html/ include/fp df/fpdf.ph p on line 1017
FPDF error: Some data has already been output, can't send PDF file
I am now getting the following when I include lines 57 - 70.
Warning: Cannot modify header information - headers already sent by (output started at /home/loanmod1/domains/erm
FPDF error: Some data has already been output, can't send PDF file
<?php
$pdf->Image('../../images/logo_color_tcg.png',0,5,210,20);
$pdf->setXY($pdf->GetX(),$pdf->GetY()+20);
$pdf->SetFont('Arial','B',12);
$pdf->Cell(190,5,$mgmtSummary_TitleRow['branchName'],0,0,'C');
$pdf->Ln(4);
$pdf->Cell(190,5,date('m/d/Y', strtotime($beginDate)).' thru '.date('m/d/Y', strtotime($endDate)),0,0,'C');
$pdf->Ln(15);
$pdf->SetFont('Arial','B',10);
$pdf->Cell(190,5,'All Findings by Category',0,0,'C');
$pdf->Ln(5);
$pdf->SetFont('Arial','B',10);
$pdf->Cell(50);
$pdf->Cell(60, 4, 'Category Name', '','', 'L','1');
$pdf->Line($pdf->GetX(),$pdf->GetY(),$pdf->GetX(),$pdf->GetY()+4);
$pdf->Cell('20', 4, '# Findings','', '', 'L', '1','');
$pdf->Ln(4);
$pdf->Cell(50);
$pdf->Line($pdf->GetX(),$pdf->GetY(),$pdf->GetX()+80,$pdf->GetY());
$allCat="SELECT
findingCategoryName AS Cat,
COUNT(loanNumber) AS Ct
FROM loan
JOIN audit USING (loanKey)
JOIN loanFinding USING (auditID)
JOIN finding USING (findingKey)
JOIN findingCategory USING (findingCategoryKey)
JOIN severity stS ON stS.severityKey=loanFinding.stSeverity
LEFT JOIN severity cuS ON cuS.severityKey=loanFinding.customSeverity
WHERE branchKey='$branchKey'
AND loan.active='1'
AND audit.activeStatus='1'
AND loanFinding.active='1'
AND audit.auditTypeKey='$auditTypeKey'
AND audit.trackingDate BETWEEN '$beginDate' AND '$endDate'
GROUP BY findingCategoryName
ORDER BY Count(loanNumber) DESC";
$allCatList = mysql_query($allCat) or die(mysql_error());
while ($allCatRow = mysql_fetch_assoc($allCatList))
{
$pdf->SetFont('Arial','',8);
$catName = $allCatRow['Cat'];
$ct = $allCatRow['Ct'];
$pdf->Line($pdf->GetX(),$pdf->GetY(),$pdf->GetX(),$pdf->GetY()+4);
$pdf->Cell('60', 4,$catName,'', '', 'L', '');
$pdf->Line($pdf->GetX(),$pdf->GetY(),$pdf->GetX(),$pdf->GetY()+4);
$pdf->Cell('20', 4,$ct,'', '', 'C', '');
$pdf->Line($pdf->GetX(),$pdf->GetY(),$pdf->GetX(),$pdf->GetY()+4);
$pdf->Ln(4);
$pdf->Cell(50);
$pdf->Line($pdf->GetX(),$pdf->GetY(),$pdf->GetX()+80,$pdf->GetY());
}
// $result = mysql_query($allCat) or die(mysql_error());
// while ($row = mysql_fetch_assoc($result))
// {
// $value[ $row['Cat'] ] = $row['Ct'];
// }
// $pdf->SetFont('Arial', 'BIU', 12);
// $pdf->Cell(0, 5, '2 - Bar diagram', 0, 1);
// $pdf->Ln(8);
// $valX = $pdf->GetX();
// $valY = $pdf->GetY();
// if ( count( $value ) != 0 ){
// $pdf->BarDiagram (190, 70, $value, '%l : %v (%p)', array(255,175,100));
// $pdf->SetXY($valX, $valY + 80);
// }
?>
Well, that is apparently a very different issue. It is a law of HTTP that all headers must come first and be complete before any browser output at all, even invisible whitespace. So you would want to examine the logic and look at the lines identified in the error messages to see what is producing browser output before FPDF tries to send the header.
A common cause of this situation is an error message sent by some included class or function.
Another cause is something like this. Because of starting and stopping PHP on two different lines, there is an invisible end-of-line character between these two PHP scripts.
<?php include ('common.php'); ?>
<?php header("Location: /"); ?>
A common workaround is to add ob_start() to the top of the main script.
http://php.net/manual/en/function.ob-start.php
A common cause of this situation is an error message sent by some included class or function.
Another cause is something like this. Because of starting and stopping PHP on two different lines, there is an invisible end-of-line character between these two PHP scripts.
<?php include ('common.php'); ?>
<?php header("Location: /"); ?>
A common workaround is to add ob_start() to the top of the main script.
http://php.net/manual/en/function.ob-start.php
mysql_fetch_assoc
instead of
mysql_fetch_array