2011-05-30 96 views
1
Ename Sal  
tom  100 
tom  200 
bill 100 
bill 250 
bill 450 
bill 400 

這是給出上述輸出的查詢和html結構。從數據庫中獲取記錄時的動態rowspan

<?php 
$sql = "select * from emp "; 
$result= mysql_query($sql); 
while($row=mysql_fetch_array($result)) 
{ 
    <tr > 
    <td rowspan="" ><?php echo $row['ename']; ?></td> 
    <td><?php echo $row['esal']?></td> 
    </tr> 
    <? }?> 

我如何能得到以下的輸出:

Ename Sal  
tom  100 
     200 
bill 100 
     250 
     450 
     400 
+2

你能提供您當前的代碼,以便我們有一些工作? – erisco 2011-05-30 07:36:01

回答

0

像這樣的事情?

While data 
echo 
    <tr> 
     <td rowspan="3"><p>numbers</p></td> 
     <td><p>1</p></td> 
     <td><p>2</p></td> 
</tr> 

請發表您的代碼的東西

0

您可以檢查條件像這樣與最後一行和當前行。

$sql = "select * from emp"; 
$result= mysql_query($sql); 
echo "<table>"; 
while($row=mysql_fetch_array($result)) 
{ 
    $now=$row[0]; 
    if($last!=$now) { 
    echo "<tr><td>$row['ename']</td><td>$row['esal']</td></tr>"; 
    }else{ 
    echo "<tr><td>&nbsp;</td><td>$row['esal']</td></tr>"; 
    } 
    $last = $row[0]; 
} 
echo "</table>"; 

我希望這會幫助你。

+0

此代碼按預期方式工作'order by Ename'在sql查詢中是必需的 – krcko 2011-05-30 13:22:34

0

它應該是這樣的

<?php 
    $sql = "SELECT * FROM emp "; 
    $result= mysql_query($sql); 
    while($row=mysql_fetch_array($result)): 
     $ename = $row['ename']; 

     // count the esal in each ename 
     $sql2 = "SELECT * FROM emp WHERE ename=$ename"; 
     $result2 = mysql_query($sql2); 
     $count_result2 = mysql_num_rows($result2); 

    ?> 
     <tr > 
     <td rowspan="<?php echo $count_result2; ?>"><?php echo $row['ename']; ?></td> 
     <?php 
      // loop each esal 
      while($row2 = mysql_fetch_array($result2)): 
      ?> 
       <td><?php echo $row['esal']; ?></td> 
      </tr> 
      <?php 
      endwhile; // endwhile for each esal looping 
    endwhile; // endwhile for the main looping 
    ?> 
5

對不起我的英文不好: 在這裏,我已經回答了這個問題How to show data from database with dynamic rowspan。再次讓我試着回答這個問題。首先讓我們看看mysql的問題。

MySQL的:

在你還沒有查詢訂單的MySQL查詢。因爲在現實生活中,你不能指望湯姆的所有記錄,賬單記錄都會在那裏。例如,採取以下插入。

INSERT INTO test_work(ename, sal) 
       VALUES("tom", 100), 
        ("bill", 450), 
        ("bill", 100), 
        ("tom", 200), 
        ("bill", 250), 
        ("bill", 400), 
        ("James", 50); 
SELECT * FROM test_work; 

結果:

+-------+------+ 
| ename | sal | 
+-------+------+ 
| tom | 100 | 
| bill | 450 | 
| bill | 100 | 
| tom | 200 | 
| bill | 250 | 
| bill | 400 | 
| James | 50 | 
+-------+------+ 

所以你的MySQL查詢應該是爲了用的ename。這裏也應該對每個人的sal進行編配。因此,我們的查詢:

SELECT * FROM emp ORDER BY ename, sal; 

編碼:

  1. 整個任務中,我們可以分爲3個部分。
    1. Mysql數據提取並存儲在數組中。
    2. 計算行跨度
    3. 印刷

MySql的Datafetching:

在數據從MySQL服務器獲取我們總是應該嘗試使用mysql_fetch_assoc函數,而不是mysql_fetch_array的。因爲mysql_fetch_assoc將只返回ename和sal。但mysql_fetch_array將返回索引ename,sal,0,1的數組。

# connect to mysql server 
    # and select the database, on which 
    # we will work. 
    $conn = mysql_connect('', 'root', ''); 
    $db = mysql_select_db('test'); 

    # Query the data from database. 
    $query = 'SELECT * FROM test_work ORDER BY ename, sal'; 
    $result = mysql_query($query); 

    # Intialize the array, which will 
    # store the fetched data. 
    $sal = array(); 
    $emp = array(); 

    # Loop over all the fetched data, and save the 
    # data in array. 
    while($row = mysql_fetch_assoc($result)) { 
     array_push($emp, $row['ename']); 
     array_push($sal, $row['sal']); 
    } 

計算行跨度:

# Intialize the array, which will store the 
    # rowspan for the user. 
    $arr = array(); 

    # loop over all the sal array 
    for ($i = 0; $i < sizeof($sal); $i++) { 
     $empName = $emp[$i]; 

     # If there is no array for the employee 
     # then create a elemnt. 
     if (!isset($arr[$empName])) { 
      $arr[$empName] = array(); 
      $arr[$empName]['rowspan'] = 0; 
     } 

     $arr[$empName]['printed'] = "no"; 

     # Increment the row span value. 
     $arr[$empName]['rowspan'] += 1; 
    } 

時,你會print_r的ARR的陣列輸出將是:

Array 
(
    [bill] => Array 
     (
      [rowspan] => 4 
      [printed] => no 
     ) 

    [James] => Array 
     (
      [rowspan] => 1 
      [printed] => no 
     ) 

    [tom] => Array 
     (
      [rowspan] => 2 
      [printed] => no 
     ) 

) 

打印與行跨度:

echo "<table cellspacing='0' cellpadding='0'> 
      <tr> 
       <th>Ename</th> 
       <th>Sal</th> 
      </tr>"; 


    for($i=0; $i < sizeof($sal); $i++) { 
     $empName = $emp[$i]; 
     echo "<tr>"; 

     # If this row is not printed then print. 
     # and make the printed value to "yes", so that 
     # next time it will not printed. 
     if ($arr[$empName]['printed'] == 'no') { 
      echo "<td rowspan='".$arr[$empName]['rowspan']."'>".$empName."</td>"; 
      $arr[$empName]['printed'] = 'yes'; 
     } 
     echo "<td>".$sal[$i]."</td>"; 
     echo "</tr>"; 
    } 
    echo "</table>"; 

代碼優化:

現在我們可以結合行跨度計算和mysql數據抓取。因爲在數組中保存獲取的數據時,我們可以計算rowspan。因此,我們的最終代碼:

<!DOCTYPE html> 
<html> 
    <head> 
     <style> 
      table tr td, table tr th{ 
       border: black 1px solid; 
       padding: 5px; 
      } 
     </style> 
    </head> 
    <body> 
     <?php 
     # connect to mysql server 
     # and select the database, on which 
     # we will work. 
     $conn = mysql_connect('', 'root', ''); 
     $db = mysql_select_db('test'); 

     # Query the data from database. 
     $query = 'SELECT * FROM test_work ORDER BY ename, sal'; 
     $result = mysql_query($query); 

     # $arr is array which will be help ful during 
     # printing 
     $arr = array(); 

     # Intialize the array, which will 
     # store the fetched data. 
     $sal = array(); 
     $emp = array(); 

     #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# 
     #  data saving and rowspan calculation  # 
     #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# 

     # Loop over all the fetched data, and save the 
     # data. 
     while($row = mysql_fetch_assoc($result)) { 
      array_push($emp, $row['ename']); 
      array_push($sal, $row['sal']); 

      if (!isset($arr[$row['ename']])) { 
       $arr[$row['ename']]['rowspan'] = 0; 
      } 
      $arr[$row['ename']]['printed'] = 'no'; 
      $arr[$row['ename']]['rowspan'] += 1; 
     } 


     #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
     #  DATA PRINTING    # 
     #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%# 
     echo "<table cellspacing='0' cellpadding='0'> 
       <tr> 
        <th>Ename</th> 
        <th>Sal</th> 
       </tr>"; 


     for($i=0; $i < sizeof($sal); $i++) { 
      $empName = $emp[$i]; 
      echo "<tr>"; 

      # If this row is not printed then print. 
      # and make the printed value to "yes", so that 
      # next time it will not printed. 
      if ($arr[$empName]['printed'] == 'no') { 
       echo "<td rowspan='".$arr[$empName]['rowspan']."'>".$empName."</td>"; 
       $arr[$empName]['printed'] = 'yes'; 
      } 
      echo "<td>".$sal[$i]."</td>"; 
      echo "</tr>"; 
     } 
     echo "</table>"; 
     ?> 
    </body> 
</html> 

結果:

a busy cat

相關問題