【问题标题】:auto generate table and grouped values by name and get multiple values按名称自动生成表和分组值并获取多个值
【发布时间】:2015-01-28 05:42:33
【问题描述】:

大家好,我有问题要问你

我正在开发商店管理系统,我想从商店表中选择所有数据并以 html 表格格式显示它到目前为止我已经尝试使用下面的代码显示它

名称............商店......数量............单位

门............ store1............970...... pcs......决定

办公桌........... store2.............10000.......pcs.... ..决定

门............ store1............50...... ..pcs......决定

					 <table class="table table-striped table-bordered bootstrap-datatable datatable responsive">
    <thead>
    <tr>	
		
        		<th>Item</th>
				
				<th>name</th>
				
				<th>location</th>
								
				<th>Quantity</th>
				
				<th>Unit</th>
				
    </tr>
    </thead>
    <tbody>
  
					 <?php
					 if (isset($_POST['search'])){
					 
					 foreach ($_POST['name'] as $attrib) { 
					 
                        $query = mysql_query("select * from store where name= '".$attrib."'") or die(mysql_error());
						
						$num_row = mysql_num_rows($query);
						
						while ($row = mysql_fetch_array($query)) {
                            $id = $row['id'];
							
                            ?>
							 
                            <tr class="del<?php echo $id ?>">
								
                                <td><?php echo $row['id']; ?></td>
								
                                <td><?php echo $row['name']; ?></td>
								
								<td><?php echo $row['location']; ?></td>
								
								<td><?php echo $row['quantity']; ?></td>
								
								<td><?php echo $row['unit']; ?></td>
								
							<td>	
                        <a href="inventorysearch.php<?php echo '?id='.$id; ?>"><i class="glyphicon glyphicon-ok"></i> Decide</a></li>
                       </td>

但我需要显示的是仅从数据库中为那些重复的名称选择分组名称,并像下面那样显示它。

名称............Store1........Store2........Store3

门............ 970............50...... ..........0........决定

办公桌............ 10............10000............ ......0........决定

【问题讨论】:

    标签: mysql


    【解决方案1】:

    您可以使用 GROUP BY 聚合结果,并可选择对组中的所有项目执行算术运算(例如 SUM)

    文档: http://dev.mysql.com/doc/refman/5.6/en/group-by-functions.html

    然而,这并不能让您轻松地将行转换为列,您可能会使用 GROUP_CONCAT 将所有值转换为单个列,但它很丑陋并且容易出现解析特殊字符等问题。

    最好的情况是在您的 PHP 中执行这项工作。我的建议是:

    (1)首先生成一个store列表,例如

    SELECT DISTINCT location FROM store WHERE name=x

    (2) 使用此信息创建位置数组,并打印表格标题

    (3) 运行实际查询时,首先按位置对结果进行排序,然后是任何其他参数。这意味着您将按顺序获取每个给定项目的所有位置。

    SELECT * FROM store WHERE name=x ORDER BY location

    (4) 遍历每一行直到位置发生变化,现在您有多个相同位置的行,然后您可以通过遍历它们并确保每个位置在正确的列中输出来创建组合行输出/p>

    为确保两个查询之间的位置列表不会改变,我建议确保 tx_isolation 是可重复读取的,然后在两个选择周围使用 BEGIN TRANSACTION 和 COMMIT 以确保它们从相同的数据中读取。

    【讨论】:

      【解决方案2】:

      我认为您想将列值转置为行

      Step 1 .从数据库中选择所有记录

      Step 2 .按Store分组记录

      你的数据数组看起来像

      $array =Array (0 => Array
                          (
                              'id' => 1,
                              'name' => 'Door',
                              'location' => 'store1',
                              'quantity' =>970,
                              'unit' =>'pcs'
                          ),
                      1 => Array
                          (
                              'id' => 2,
                              'name' => 'Desk',
                              'location' => 'store2',
                              'quantity' =>1000,
                              'unit' =>'pcs'
                          ),
                      2 => Array
                          (
                              'id' => 3,
                              'name' => 'Door',
                              'location' => 'store1',
                              'quantity' =>50,
                              'unit' =>'pcs'
                          ),
                      3 => Array
                          (   
                              'id' => 4,
                              'name' => 'Desk',
                              'location' => 'store2',
                              'quantity' =>970,
                              'unit' =>'pcs'
                          ),
                      4 => Array
                      (   
                          'id' => 5,
                          'name' => 'Desk',
                          'location' => 'store2',
                          'quantity' =>70,
                          'unit' =>'pcs'
                      ),
                      5 => Array
                      (
                          'id' => 6,
                          'name' => 'Door',
                          'location' => 'store1',
                          'quantity' =>150,
                          'unit' =>'pcs'
                      ),
                      6 => Array
                          (
                              'id' => 7,
                              'name' => 'Door',
                              'location' => 'store2',
                              'quantity' =>120,
                              'unit' =>'pcs'
                          ),
                      7 => Array
                          (
                              'id' => 8,
                              'name' => 'Desk',
                              'location' => 'store1',
                              'quantity' =>230,
                              'unit' =>'pcs'
                          ),
                      8 => Array
                          (
                              'id' => 9,
                              'name' => 'Desk',
                              'location' => 'store1',
                              'quantity' =>50,
                              'unit' =>'pcs'
                          ),
                      9 => Array
                          (   
                              'id' => 10,
                              'name' => 'Desk',
                              'location' => 'store3',
                              'quantity' =>970,
                              'unit' =>'pcs'
                          ),
                      10=> Array
                      (   
                          'id' => 11,
                          'name' => 'Desk',
                          'location' => 'store3',
                          'quantity' =>70,
                          'unit' =>'pcs'
                      ),
                      11 => Array
                      (
                          'id' => 12,
                          'name' => 'pankaj',
                          'location' => 'store1',
                          'quantity' =>150,
                          'unit' =>'pcs'
                      ),
                  );
      

      分组列值后所需的结果数组看起来像

      $resultArray =array();
                  foreach($array as $key =>$value){
                       $itemKey =$value['location'];
                       @$resultArray[$value['name']][$itemKey] +=$value['quantity'];
                  }
      

      最终数组

      Array
      (
          [Door] => Array
              (
                  [store1] => 1170
                  [store2] => 120
              )
      
          [Desk] => Array
              (
                  [store2] => 2040
                  [store1] => 280
                  [store3] => 1040
              )
      
          [pankaj] => Array
              (
                  [store1] => 150
              )
      
      )
      

      现在迭代resultArray键将是表的标题,值将是表的行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-25
        • 2021-12-11
        • 1970-01-01
        • 2014-04-29
        相关资源
        最近更新 更多