Jump to content

MySQL COUNT -> PHP Arrays


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi everyone

 

I have a requirement to create some reports and outputting these to a PDF. I'm using FPDF which seems to be rather user-friendly. I was hoping someone could help with me a problem I have regarding MySQL COUNT and PHP arrays.

 

I have a table named 'faults' and within this a row called 'level'. There are three 'levels' = P1, P2, P3 that could populate this row. I wish to display the output into a PDF so it looks something like this:

 

Total number of faults;

 

P1           P2           P3

 

1              2              3

 

Where I'm falling short is how to perform the COUNT and then grab the data stick it into an array and output it to the PDF. The MySQL statement you see below, if put directly into phpMyAdmin, seems to do the trick, so I'm hoping I'm on the right track. So far I have this:

<----OUTPUT OMITTED---->

$result=mysqli_query($con, 


"SELECT
 COUNT(IF(level='P1',1, NULL)) 'P1', 
 COUNT(IF(level='P2',1, NULL)) 'P2', 
 COUNT(IF(level='P3',1, NULL)) 'P3' 
 FROM faults");


//initialize counter
$i = 0;


//Set maximum rows per page
$max = 25;


while($row = mysqli_fetch_array($result))
{
//If the current row is the last one, create new page and print column title
if ($i == $max)
{
$pdf->AddPage();


//print column titles for the current page
$pdf->SetY($y_axis);
$pdf->SetX(25);
$pdf->Cell(50,6,'Total P1s',1,0,'L',1);
$pdf->Cell(50,6,'Total P2s',1,0,'L',1);
$pdf->Cell(50,6,'Total P3s',1,0,'L',1);


//Go to next row
$y_axis = $y_axis + $row_height;


//Set $i variable to 0 (first row)
$i = 0;
}


//This bit definitely ain't right
$p1 = $row['level'];
$p2 = $row['level'];
$p3 = $row['level'];




$pdf->SetY($y_axis);
$pdf->SetX(25);
$pdf->Cell(50,6,$p1,1,0,'L',1);
$pdf->Cell(50,6,$p2,1,0,'L',1);
$pdf->Cell(50,6,$p3,1,0,'R',1);


//Go to next row
$y_axis = $y_axis + $row_height;
$i = $i + 1;
}


//Send file
$pdf->Output();


?>
Appreciate it must look a mess, but would appreciate someone cleverer than me pointing me in the right direction.
 
Thanks for this and for your help so far!
Link to comment
https://forums.phpfreaks.com/topic/293524-mysql-count-php-arrays/
Share on other sites

  • Solution

Your query is fine. The problem is here

//This bit definitely ain't right
$p1 = $row['level'];
$p2 = $row['level'];
$p3 = $row['level'];

You need to use P1, P2, or P3 in place of level. Example

$p1 = $row['P1'];
$p2 = $row['P2'];
$p3 = $row['P3'];
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.