Some more background:
CREATE TABLE lps (
lp_id mediumint(8) unsigned NOT NULL auto_increment,
fk_member_id mediumint(9) NOT NULL,
lp_origin varchar(3) NOT NULL,
lp_style varchar(20) NOT NULL,
lp_number varchar(10) NOT NULL,
lp_load enum('Top','Side') default NULL,
lp_spine enum('Thick','Thin') default NULL,
lp_emi enum('With','Without') default NULL,
lp_audio enum('Mono','Stereo') default NULL,
lp_label enum('Domestic','Export') default NULL,
lp_factory enum('Scranton, PA','Los Angeles, CA','Jacksonville, IL','Winchester, VA') default NULL,
lp_banded enum('Banded','Not Banded') default NULL,
lp_side1 enum('Original','Corrected') default NULL,
lp_side4 enum('Original','Corrected') default NULL,
lp_img varchar(64) default NULL,
PRIMARY KEY (lp_id),
KEY fk_member_id (fk_member_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=152 ;
This is the code I am using, which is just a sample. It works for the most part. My problem is I will have a lot of statistical data to use and am trying to find a way to do it with out hit the database 50 times.
<?php
$member_id = $_SESSION['member_id'];
require_once('connect.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('Error connecting to MySQL server');
echo '<table class="sample" width="600" border="0" cellpadding="2">';
//new row
echo '<tr><td>';
echo '<table class="innertable" width="300" border="1" cellpadding="2">';
echo ' <tr>';
echo ' <td>LP Origin</td>
';
echo ' <td style="text-align: center;" width="30">Total</td>
';
echo ' <td style="text-align: center;" width="35">Percent</td>
';
echo ' </tr>';
$query = "SELECT COUNT(lp_origin) total, lp_origin FROM whitealbumreg.lps GROUP BY lp_origin";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
$totals = array();
$total= 0;
while ($row = mysqli_fetch_assoc($result))
{
$totals[] = $row;
$total += $row['total'];
}
// Now you can find the total of all results in $total.
foreach ($totals as $v)
{
echo ' <td>' . $v['lp_origin'] . '</td>
';
echo ' <td style="text-align: center;">' . $v['total'] . '</td>
';
echo ' <td style="text-align: center;">' . ROUND(($v['total'] / $total += $row['total']) * 100) . '%' . '</td>
';
echo ' <tr>';
}
echo '</tr>';
echo '</table>';
//new column
echo '</td><td>';
echo '<table class="innertable" width="300" border="1" cellpadding="2">';
echo ' <tr>';
echo ' <td>UK: Thin vs. Thick Spine</td>
';
echo ' <td style="text-align: center;" width="30">Total</td>
';
echo ' <td style="text-align: center;" width="35">Percent</td>
';
echo ' </tr>';
$query = "SELECT COUNT(lp_spine) total,lp_spine FROM whitealbumreg.lps WHERE lp_spine IS NOT NULL GROUP BY lp_spine";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
$totals = array();
$total = 0;
while ($row = mysqli_fetch_assoc($result))
{
$totals[] = $row;
$total += $row['total'];
}
// Now you can find the total of all results in $total.
foreach ($totals as $v)
{
echo ' <tr>';
echo ' <td>' . $v['lp_spine'] . '</td>
';
echo ' <td style="text-align: center;">' . $v['total'] . '</td>
';
echo ' <td style="text-align: center;">' . ROUND(($v['total'] / $total += $row['total']) * 100) . '%' . '</td>
';
}
echo '</tr>';
echo '</table>';
echo ' </td></tr>';
//new row
echo ' <tr><td>';
echo '<table class="innertable" width="300" border="1" cellpadding="2">';
echo ' <tr>';
echo ' <td>UK: "An EMI recording"</td>
';
echo ' <td style="text-align: center;" width="30">Total</td>
';
echo ' <td style="text-align: center;" width="35">Percent</td>
';
echo ' </tr>';
$query = "SELECT COUNT(lp_emi) total,lp_emi FROM whitealbumreg.lps WHERE lp_emi IS NOT NULL GROUP BY lp_emi";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
$totals = array();
$total = 0;
while ($row = mysqli_fetch_assoc($result))
{
$totals[] = $row;
$total += $row['total'];
}
// Now you can find the total of all results in $total.
foreach ($totals as $v)
{
echo ' <td>' . $v['lp_emi'] . '</td>
';
echo ' <td style="text-align: center;">' . $v['total'] . '</td>
';
echo ' <td style="text-align: center;">' . ROUND(($v['total'] / $total += $row['total']) * 100) . '%' . '</td>
';
echo ' <tr>';
}
echo ' </tr>';
echo '</table>';
//new column
echo '</td><td>';
echo '<table class="innertable" width="300" border="1" cellpadding="2">';
echo ' <tr>';
echo ' <td>UK: Mono vs. Stereo</td>
';
echo ' <td style="text-align: center;" width="30">Total</td>
';
echo ' <td style="text-align: center;" width="35">Percent</td>
';
echo ' </tr>';
$query = "SELECT COUNT(lp_audio) total,lp_audio FROM whitealbumreg.lps WHERE lp_audio IS NOT NULL GROUP BY lp_audio";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
$totals = array();
$total = 0;
while ($row = mysqli_fetch_assoc($result))
{
$totals[] = $row;
$total += $row['total'];
}
// Now you can find the total of all results in $total.
foreach ($totals as $v)
{
echo ' <td>' . $v['lp_audio'] . '</td>
';
echo ' <td style="text-align: center;">' . $v['total'] . '</td>
';
echo ' <td style="text-align: center;">' . ROUND(($v['total'] / $total += $row['total']) * 100) . '%' . '</td>
';
echo ' <tr>';
}
echo ' </tr>';
echo '</table></td></tr>';
//end table with
echo '</table>';