I'm upgrading all my mysql_* to PDO. I can handle simple SELECTs and INSERTs. But I have one complex SELECT using nested loops that I am making a dog's dinner of. I'll post the mysql_* code, which outputs a two-column table with total registrations at A1 level in the first column and total registrations at A1 level who have paid in brackets in the second column.
-------------------------
Smithsville : A1 | |
10 | (7) |
-------------------------
Grange : A1 | |
4 | (4) |
-------------------------
Beau Ridge : A1 | |
23 | (16)|
-------------------------
Jonesboro : A1 | |
9 | (9) |
-------------------------
Lexing : A1 | |
3 | (1) |
-------------------------
In the full application further tables are genertated across the page for the other levels.
$levels = array('A1', 'A2', 'B1', 'B2');
$centres = array('Smithsville', 'Grange', 'Plateau Ridge', 'Jonesboro', 'Lexing');
for ($j = 0; $j < 1; $j++) /* Does not loop; selects only A1 in queries below*/
{
for ($k=0; $k<5; $k++) /* Cycles through the centres */
{
$show_count = mysql_query("SELECT COUNT(Centre)
FROM exam
WHERE exam.Centre='".$centres[$k]."'
AND exam.Level='".$levels[$j]."'");
$show_paid = mysql_query("SELECT COUNT(Centre)
FROM exam
JOIN personal
ON exam.P_ID=personal.P_ID
WHERE personal.Paid='1'
AND exam.Centre='".$centres[$k]."'
AND exam.Level='".$levels[$j]."'");
$result_rows = mysql_num_rows($show_count);
$result_rows2 = mysql_num_rows($show_paid);
if ($result_rows == 0)
{ echo 'No registrations';}
if ($result_rows2 == 0)
{ echo 'No payments';}
echo '<table class="Font3White">
<tr class="Title">
<td class="width8">'.$centres[$k].' : '.$levels[$j].'</td>
<tr>';
while (($row = mysql_fetch_row($show_count)) && ($row2 = mysql_fetch_row($show_paid)))
{
foreach ($row as $key => $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
foreach ($row2 as $key2 => $value2)
{
echo '<td> (';
echo $value2;
echo ')</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
My PDO attempt is
for ($j = 0; $j < 1; $j++) /* Does not loop; selects only A1 */
{
for ($k=0; $k<5; $k++) /* Cycles through the centres */
{
/*//////////////////////////*/
/* Prepares statements */
/*//////////////////////////*/
$stmt1 = $db->query("SELECT COUNT(Centre)
FROM exam
WHERE exam.Centre=:centre
AND exam.Level=:level");
$stmt2 = $db->query("SELECT COUNT(Centre)
FROM exam
JOIN personal
ON exam.P_ID=personal.P_ID
WHERE personal.Paid='1'
AND exam.Centre=:centre
AND exam.Level=:level");
/*/////////////////////*/
/* Binds parameters */
/*/////////////////////*/
$stmt1->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt1->bindParam(':level', $level, PDO::PARAM_STR);
$stmt2->bindParam(':centre', $centre, PDO::PARAM_STR);
$stmt2->bindParam(':level', $level, PDO::PARAM_STR);
/*//////////////////////////*/
/* Executes statements */
/*//////////////////////////*/
$stmt1->execute(array($centre, $level));
$stmt2->execute(array($centre, $level));
echo '<table class="Font3White">
<tr class="Title">
<td class="width8">'.$centres[$k].' > '.$levels[$j].'</td>
<tr>';
while (($row = $stmt1->fetch(PDO::FETCH_ASSOC)) && ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)))
{
foreach ($row as $key => $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
foreach ($row2 as $key2 => $value2)
{
echo '<td> (';
echo $value2;
echo ')</td>';
}
echo '</tr>';
}
echo '</table>';
}
}
I know this is terrible but I hope it can be worked with.