2011-08-25 37 views
0

我從Mysql中爲表獲取數據。每行有5個單元格。如果條件爲true,則合併HTML TABLE ROWS

如果兩行的第4個單元格值相同,那麼我想合併這些行。

我試過使用JavaScript數組,但我無法合併行。任何人都可以分享他們的想法或給我任何示例代碼。提前致謝。

+2

你是什麼意思與「合併行」?嘗試舉一個例子:你有什麼,你想要什麼? –

回答

0

它不是一個好主意,使用JavaScript。首先,一些用戶可能已經停用了js,甚至不可用(例如手機瀏覽器)。

我建議從mysql獲取數據,在php中循環訪問它們(如果使用php),在進行任何必要的合併時將數據複製到新數組中,然後打印新的「正確」數組。

它使用的是PHP嗎?您需要的示例代碼必須是服務器端(例如php)。

+0

感謝您的建議。我只使用PHP。你能提供這樣的示例代碼嗎? –

1

我很喜歡張貼圖片,但由於即時通訊新和犯規都reput,它沒有上傳... 好,here the link of the photo

如果這就是你問什麼...我有一個有點長碼..我不知道這是否是有效的,但它的工作對我來說

int itemsize = 5; //number of rows that is expected to be in merged 
for(int item=0; i<itemsize ; i++){//this jsp 
%> 
    <tr> 
    <td><%=value%></td> 
    <td><%=value%></td> 
    <% 
     if(itemsize > 1 && item==0){ //if the itemsize more than 1, and it is the first row, it will put a rowspan that has the value of the itemsize or the number of row to be merged 
     %> 
     <td rowspan='<%=itemsize %>'><%=value%></td> 
     <td rowspan='<%=itemsize %>'><%=value%></td> 
     <td rowspan='<%=itemsize %>'><%=value%></td> 
     <% 
     }else if(itemsize > 1 && item>0){ 
     //on the second loop this will be executed but bcoz we have rowspan, <td> should be eliminated on the succeeding row* 
     }else{ // but if there is only one row to be displayed, no rowspan is needed, so ordinary <td will be written.. 
     %> 
     <td><%=value%></td> 
     <td><%=value%></td> 
     <td><%=value%></td>   
     <% 
     } 
    %> 
    </tr> 
<% 
} 

編輯:

這是我的新代碼。我使用php做了一個示例。嘗試運行它。對不起,代碼,它是我的第一個PHP。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 

<style> 
    body{ font-family: tahoma, Arial; font-size: 9pt;} 
    table {border-collapse:collapse; font: tahoma, Arial 9pt;} 
    table th{font-family: tahoma, Arial, Geneva, sans-serif; font-size:9pt; width:75px; background-color:#999; color:#000; text-transform:uppercase;} 
    table th, td{ padding:4px 6px; text-transform:capitalize;} 
</style> 

</head> 

<body> 
    <table cellspacing="0" cellpadding="0" border="1" bordercolor="#000000"> 
    <thead> 
     <th style="width:10px; text-align:right">Count</th> 
     <th>Detail 1</th> 
     <th>Head 2</th> 
     <th>Head 3</th> 
     <th>Head 4</th> 
     <th>Head 5</th> 
     <th>Head 6</th> 
     <th>Detail 2</th> 
    </thead> 

    <tbody> 
      <?php    
       $arr_detail_1 = array("angelo", "philip", "raymond", "jonathan", "pedro"); 
       $arr_detail_2 = array("Cavite", "Laguna", "Quezon", "Japan", "Surigao"); 
       $arr_detail_3 = array("10-dec", "11-Sep", "12-Feb", "22-July", "15-Oct"); 
       $arr_detail_4 = array("Pink", "blue", "Green", "White", "Purple"); 
       $arr_detail_5 = array("Computer", "Door Lock", "Pencil", "Cup", "Jeans"); 

     $a=array("screw"); 
     $b=array("ball","paper","Big","Liquid","id"); 
     $c=array("Sewing"); 
     $d=array("Running","Biking","Swimming"); 
     $e=array("Tree","planting","root","fruits","Rain","Grass"); 

     $f=array("driver"); 
     $g=array("Pen","bag","Brother","eraser","lace"); 
     $h=array("machine"); 
     $i=array("barefoot","with no bike","technique"); 
     $j=array("planting","tree","crops","bearig-trees","Forest","Hopper"); 

     $arr1 = array($a, $b, $c, $d, $e); 
       $arr2 = array($f, $g, $h, $i, $j);    


      $rowcount = 1; 
       for ($i=0; $i<count($arr1); $i++){ 
       // echo "The number is " . $i . "<br />"; 

       $temp_arr_1 = $arr1[$i]; 
       $temp_arr_2 = $arr2[$i]; 
       for($x=0; $x<count($temp_arr_1); $x++){ 

        ?> 
         <tr> 
          <td align="right"><?php echo $rowcount; ?>.</td> 
       <td><?php echo $temp_arr_1[$x]; ?></td> 
       <td><?php echo $temp_arr_2[$x]; ?></td> 
       <?php 
          $span_count = count($temp_arr_1); 
       if($span_count>1 && $x==0){ 

           ?> 
        <td rowspan="<?php echo count($temp_arr_1);?>"><?php echo $arr_detail_1[$i]; ?></td> 
        <td rowspan="<?php echo count($temp_arr_1);?>"><?php echo $arr_detail_2[$i]; ?></td> 
        <td rowspan="<?php echo count($temp_arr_1);?>"><?php echo $arr_detail_3[$i]; ?></td> 
        <td rowspan="<?php echo count($temp_arr_1);?>"><?php echo $arr_detail_4[$i]; ?></td> 
        <td rowspan="<?php echo count($temp_arr_1);?>"><?php echo $arr_detail_5[$i]; ?></td> 
           <?php 
          }else if($span_count>1 && $x>0){ 
           // 
          }else{ 
           ?> 
        <td><?php echo $arr_detail_1[$i]; ?></td> 
        <td><?php echo $arr_detail_2[$i]; ?></td> 
        <td><?php echo $arr_detail_3[$i]; ?></td> 
        <td><?php echo $arr_detail_4[$i]; ?></td> 
        <td><?php echo $arr_detail_5[$i]; ?></td> 
           <?php 
          } 
          ?> 
         </tr> 
        <?php 
        $rowcount++; 
        } 
       } 
     ?> 
    </tbody> 
    </table> 
</body> 
</html> 

我沒有使用數組作爲質數據庫...],並做代碼的方式像我用JSP做,所以我希望你能夠理解。即時通訊真的很新的PHP。即使在Java中,即時通訊不知道我做的是正確的!我編碼只要它運行,即使它緩慢..但即時通訊嘗試解決這個問題!祝你的項目福音。

+0

完全一樣我想要的。謝謝..... –

+0

非常受歡迎。我很高興知道我確實幫助過某人.. yehey!也謝謝你。 – amrodelas

+0

您好,我需要相同的輸出。你能給我詳細的細節嗎?我對上述代碼 –

0

您應該給第一行rowspan="2"中的最後一個單元格,並省略下一行中的最後一個單元格。

如果你確實想做到這一點在JavaScript出於某種原因
<table> 
    <tr> 
     <td>some value</td> 
     <td>some value</td> 
     <td>some value</td> 
     <td>some value</td> 
     <td rowspan="2">some value</td> 
    </tr> 
    <tr> 
     <td>some value</td> 
     <td>some value</td> 
     <td>some value</td> 
     <td>some value</td> 
    </tr> 
</table> 
1

一個非常快速的樣品:

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
</head> 
<body> 
    <table border="2" id="aTable"> 
     <tr> 
      <td>First</td> 
      <td>A</td> 
     </tr> 
     <tr> 
      <td>First</td> 
      <td>B</td> 
     </tr> 
     <tr> 
      <td>Second</td> 
      <td>V</td> 
     </tr> 
     <tr> 
      <td>Third</td> 
      <td>D</td> 
     </tr> 
     <tr> 
      <td>Third</td> 
      <td>E</td> 
     </tr> 
    </table> 
</body> 
    <script type="text/javascript"> 
     function MergeCommonRows(table, columnIndexToMerge){ 
      previous = null; 
      cellToExtend = null; 
      table.find("td:nth-child("+columnIndexToMerge+")").each(function(){ 
       jthis = $(this); 
       content = jthis.text() 
       if(previous == content){ 
        jthis.remove(); 
        if(cellToExtend.attr("rowspan") == undefined){ 
         cellToExtend.attr("rowspan", 2); 
        } 
        else{ 
         currentrowspan = parseInt(cellToExtend.attr("rowspan")); 
         cellToExtend.attr("rowspan", currentrowspan+1); 
        } 
       } 
       else{ 
        previous = content; 
        cellToExtend = jthis; 
       } 
      }); 
     }; 

     MergeCommonRows($("#aTable"), 1); 
    </script> 
</html> 
+1

這工作很好,但只是添加parseInt圍繞currentrowspan = parseInt(cellToExtend.attr(「rowspan」)) - 否則你會得到有線結果 –

相關問題