I am trying to construct a table which shows price on product, but I cannot figure out how to match the data based on the key values.
I query the SQL db and get the following result. Date is actually the month
Product name Price date
care bear 11 4
care bear 12.5 5
care bear 9 6
car 123 3
car 344 4
.etc
The sql result is read to an array. Similar to table example.
Now I am trying to make a table that shows the name and price for the whole year, but I am stuck how to do this. The idea is the following.
Product jan feb mar apr may jun jul aug oct nov dec
care bear 0 0 0 11 12.5 9 0 0 0 0 0
car 0 0 3 4 0 0 0 0 0 0 0
// while there are products
while (!empty($results[$k]['product']))
{
// fill first cell with name.
echo '<td>'.$results[$k]['product'].'</td>';
// while the product has the same name
while ($results[$i]['product'] = $results[$i-1]['product'] )
{
//generate values for each month
while ($i< 12) {
if ($results[0]['month'] == $i){echo '<td>'.$results[0]['price'].'</td>';}
else if ($results[1]['month'] == $i){echo '<td>'.$results[1]['price'].'</td>';}
else if ($results[2]['month'] == $i){echo '<td>'.$results[2]['price'].'</td>';}
else if ($results[3]['month'] == $i){echo '<td>'.$results[3]['price'].'</td>';}
else if ($results[4]['month'] == $i){echo '<td>'.$results[4]['price'].'</td>';}
else if ($results[5]['month'] == $i){echo '<td>'.$results[5]['price'].'</td>';}
else if ($results[6]['month'] == $i){echo '<td>'.$results[6]['price'].'</td>';}
else if ($results[7]['month'] == $i){echo '<td>'.$results[7]['price'].'</td>';}
else if ($results[8]['month'] == $i){echo '<td>'.$results[8]['price'].'</td>';}
else if ($results[9]['month'] == $i){echo '<td>'.$results[9]['price'].'</td>';}
else if ($results[10]['month'] == $i){echo '<td>'.$results[10]['price'].'</td>';}
else if ($results[11]['month'] == $i){echo '<td>'.$results[11]['price'].'</td>';}
else if ($results[12]['month'] == $i){echo '<td>'.$results[12]['price'].'</td>';}
//else if ($results[13]['month'] == $i){echo '<td>'.$results[13]['price'].'</td>';}
else echo '<td>0,00</td>';
If($i==12)
{
echo '</tr><tr>';
}
$i++;
}
}
$k++;
}
There should be better ways of doing this and getting it actually to work, but for the death of me I can not come up with any...
Any help would be much appreciated.