Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql PHP row count + percentage
    text
    copied!<p>I have a simple table:</p> <pre><code>-- ---------------------------- CREATE TABLE `clothes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `shirt` text NOT NULL, `color` varchar(255) NOT NULL, PRIMARY KEY (`id`); -- ---------------------------- </code></pre> <p>Sample table content:</p> <pre><code>id shirt color 1 long white 2 short yellow 3 long blue 4 long blue 5 long white 6 short white 7 short white 8 long yellow 9 long yellow 10 short yellow </code></pre> <p>My FINAL GOAL is to feed the results into this format:</p> <pre><code>&lt;ul&gt; &lt;li&gt;Total Rows: &lt;?php echo $totalRows; ?&gt;&lt;/li&gt; &lt;li&gt;total yellow count: &lt;?php echo $yellowCount; ?&gt;&lt;/li&gt; &lt;li&gt;total yellow percent: &lt;?php echo $yellowPercentage; ?&gt;&lt;/li&gt; &lt;li&gt;total blue count: &lt;?php echo $blueCount; ?&gt;&lt;/li&gt; &lt;li&gt;total blue percent: &lt;?php echo $bluePercentage; ?&gt;&lt;/li&gt; &lt;li&gt;total white count: &lt;?php echo $whiteCount; ?&gt;&lt;/li&gt; &lt;li&gt;total white percent: &lt;?php echo $whitePercentage; ?&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Resulting in (with rounded percentages):</p> <pre><code> &lt;ul&gt; &lt;li&gt;Total Rows: 10 &lt;/li&gt; &lt;li&gt;total yellow count: 4&lt;/li&gt; &lt;li&gt;total yellow percent: 40%&lt;/li&gt; &lt;li&gt;total blue count: 2&lt;/li&gt; &lt;li&gt;total blue percent: 20%&lt;/li&gt; &lt;li&gt;total white count: 4&lt;/li&gt; &lt;li&gt;total white percent: 40%&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>I've been experimenting with various code but nothing has gotten me where I need to be:</p> <pre><code> &lt;?php $con = mysql_connect("localhost","root","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("inventory", $con); // Make a MySQL Connection $query = "SELECT color, COUNT(color) FROM clothes GROUP BY color"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "There are ". $row['COUNT(color)'] ." ". $row['color'] ." items."; echo "&lt;br /&gt;"; } ?&gt; </code></pre> <p>That gets me almost there with this -- but I need to be able to separate the results for inserting into HTML:</p> <pre><code>There are 4 white items. There are 4 yellow items. There are 2 blue items. </code></pre> <p>Any ideas would be appreciated. THANKS.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload