Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7651609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:33:01+00:00 2026-05-31T11:33:01+00:00

I have a simple table: — —————————- CREATE TABLE `clothes` ( `id` int(11) NOT

  • 0

I have a simple table:

-- ----------------------------
CREATE TABLE `clothes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `shirt` text NOT NULL,
  `color` varchar(255) NOT NULL,
  PRIMARY KEY (`id`);
-- ----------------------------

Sample table content:

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

My FINAL GOAL is to feed the results into this format:

<ul>
<li>Total Rows: <?php echo $totalRows; ?></li>

<li>total yellow count:   <?php echo $yellowCount; ?></li>
<li>total yellow percent: <?php echo $yellowPercentage; ?></li>

<li>total blue count:    <?php echo $blueCount; ?></li>
<li>total blue percent:  <?php echo $bluePercentage; ?></li>

<li>total white count:    <?php echo $whiteCount; ?></li>
<li>total white percent:  <?php echo $whitePercentage; ?></li>
</ul>

Resulting in (with rounded percentages):

  <ul>
    <li>Total Rows: 10 </li>

    <li>total yellow count:  4</li>
    <li>total yellow percent: 40%</li>

    <li>total blue count:    2</li>
    <li>total blue percent:  20%</li>

    <li>total white count:    4</li>
    <li>total white percent:  40%</li>
    </ul>

I’ve been experimenting with various code but nothing has gotten me where I need to be:

 <?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 "<br />";
}

    ?>

That gets me almost there with this — but I need to be able to separate the results for inserting into HTML:

There are 4 white items.
There are 4 yellow items.
There are 2 blue items.

Any ideas would be appreciated. THANKS.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:33:02+00:00Added an answer on May 31, 2026 at 11:33 am

    Here is what you could do:

    SELECT color, COUNT(*) AS color_count FROM clothes GROUP BY color
    

    You should get a result like:

    color    color_count
    yellow   7
    red      1
    white    9
    

    This data should be enough for you to compute the total number of rows (a sum of all the color_count values) and their percentage (just divide them).

    So, in php you’ll have something like:

    $query = "SELECT color, COUNT(*) AS color_count FROM clothes GROUP BY color";
    $result = mysql_query($query);
    $values = array();
    $pct = array();
    $total = 0;
    
    while ($row = mysql_fetch_assoc($result)) {
        $values[$row["color"]] = $row["color_count"];
        $total += $row["color_count"];
    }
    
    foreach($values as $key => $value) {
        $pct[$key] = $value/$total;
    }
    

    So, in the end what you have is, $values["yellow"] will give you the number of yellow clothes, and $pct["yellow"]*100 will give you the percentage of clothes that is yellow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple table CREATE TABLE a( id int IDENTITY(1,1) NOT NULL, x
I have a simple table as below. CREATE TABLE `stats` ( `id` int(11) NOT
I have a simple table with autoincrementing ID's and one text column. id(int), value(nvarchar)
I have a simple table: CREATE TABLE [dbo].[Users]([Surname] [nvarchar](50) COLLATE Latin1_General_CI_AI NULL) ON [PRIMARY]
I have simple table creating script in Postgres 9.1. I need it to create
I have a simple table in Sybase, let's say it looks as follows: CREATE
I have a simple table not related to any other. It has a not
Suppose I have simple table like this: ID (PRIMARY) time (INT) stage (TINYINT) other
I have a simple table view which is editable. All I need the user
I have a simple table (lets call it Table1) that has a NVARCHAR field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.