简体   繁体   English

mysql PHP行数+百分比

[英]mysql PHP row count + percentage

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: 这几乎可以解决我的问题-但我需要能够分离出要插入HTML的结果:

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

Any ideas would be appreciated. 任何想法,将不胜感激。 THANKS. 谢谢。

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). 此数据应足以计算总行数(所有color_count值的总和)及其百分比(将它们除以)。

So, in php you'll have something like: 因此,在php中,您将获得以下内容:

$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. 因此,最终您拥有的是$values["yellow"]会给您提供黄色衣服的数量,而$pct["yellow"]*100会给您带来黄色衣服的百分比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM