简体   繁体   English

如何将MYSQL COUNT(*)语句的结果返回到HTML表中的单独列中

[英]How to return result of MYSQL COUNT(*) Statement into separate columns within a HTML Table

How to return result of MYSQL COUNT( ) Statement into separate columns within a HTML Table* 如何将MYSQL COUNT( )语句的结果返回到HTML表中的单独列中*

MYSQL - PHP MYSQL-PHP

$sql= mysql_query("select count(*), shop_order_action from shop_orders o
                  WHERE shop_order_action IN ('Sale', 'Transfer', 'Delivery', 'Return')
                  AND shop_order_day = '28' AND shop_order_month = '08' AND shop_order_year = '2014'
                  AND shop_order_location = 'Bawtry'
                  GROUP BY FIELD (shop_order_action, 'Sale', 'Transfer', 'Delivery', 'Return')");


while ($row=mysql_fetch_row($sql)) {
echo "<pre>";
print_r($row);
echo "</pre>";

}

RESULT OF PRINT_R PRINT_R的结果

Array
(
    [0] => 8
    [1] => Sale
)
Array
(
    [0] => 2
    [1] => Transfer
)
Array
(
    [0] => 1
    [1] => Return
)

TABLE

Shop Action    Count       

SALE           <? echo $row[0] ?> 
TRANSFER       <? echo $row[0] ?> 
DELIVERY       <? echo $row[0] ?>            
RETURN         <? echo $row[0] ?>

So I need to return the correct row value ie Sale, Transfer ... etc in the corresponding column within the table. 因此,我需要在表中的相应列中返回正确的行值,即Sale,Transfer ...等。

I could iterate through the results with a while loop, however, I'm having trouble figuring out how to structure the loop so that it prints out each row in order within the while loop without having to go to the top of the loop. 我可以使用while循环遍历结果,但是,我在弄清楚如何构造循环时遇到了麻烦,以便不必在循环顶部就可以按顺序打印出while循环中的每一行。

Maybe I should use a while loop and the continue condition, so that every time $row[0] is echoed it returns the next row. 也许我应该使用while循环和continue条件,以便每次回显$ row [0]时,它都会返回下一行。

I came up with a solution which may not be optimal but it performs as required: 我想出了一个可能不是最优的解决方案,但是它可以按要求执行:

Solution

    $sql= mysql_query("select count(*), shop_order_action from shop_orders o
                  WHERE shop_order_action IN ('Sale', 'Transfer', 'Delivery', 'Return')
                  AND shop_order_day = '".$d."' AND shop_order_month = '".$m."' AND shop_order_year = '".$y."'
                  AND shop_order_location = 'Bawtry'
                  GROUP BY FIELD (shop_order_action, 'Sale', 'Transfer', 'Delivery', 'Return')");


while ($result=mysql_fetch_row($sql)) {

  if ($result[1] == 'Sale') { $sale = $result[0]; } 
  if ($result[1] == 'Transfer') { $transfer = $result[0]; } 
  if ($result[1] == 'Delivery') { $delivery = $result[0]; } 
  if ($result[1] == 'Return') { $return = $result[0]; } 

}
  if (isset($sale)) { echo $sale; }
  echo '<br />';
  if (isset($transfer)) { echo $transfer; }
  echo '<br />';

  etc ...

The code within the while loop stores each action value as a variable which can be called outside of the loop. while循环中的代码将每个操作值存储为变量,可以在循环外部调用该变量。

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

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