Rowspan table if values is the same in PHP

删除回忆录丶 提交于 2020-07-15 05:59:25

问题


<?php
        $id=$_POST['itinerary_id'];
        $query=mysql_query("select * from tblitinerarydetails where tblid='$id' order by date_of_travel asc");
        $i = 0;
        $prevValue = NULL;
        while($rows=mysql_fetch_array($query)){
            $date=$rows['date_of_travel'];
            $mydate = strtotime($date);
            $newdate = date('F j, Y', $mydate);
            $activities=$rows['activities'];
            $time_departure=$rows['time_departure'];
            $time_arrival=$rows['time_arrival'];
            $new_time_departure = date('g:i A', strtotime( $time_departure));
            $new_time_arrival = date('g:i A', strtotime( $time_arrival));
            $means_of_transportation= $rows['means_of_transportation'];
            $travelling_allowance = number_format($rows['travelling_allowance'],2);
            $destination = $rows['destination'];    
            $i++;
            echo "<tr>";
            if ($newdate == $prevValue) {
                echo "<td align='left' rowspan='$i'>$prevValue</td>";
             }
            $prevValue = $newdate;

            echo "<td align='left'>$destination</td>";
            echo "<td align='center'>$new_time_departure</td>";
            echo "<td align='center'>$new_time_arrival</td>";
            echo "<td align='center'>$activities</td>";
            echo "<td align='center'>$means_of_transportation</td>";
            echo "<td align='right'>$travelling_allowance</td>";
            echo "</tr>";
        }
?>

I just want to add rowspan in my table if the values are the same. Let's say like this:

enter image description here

I wanted all the dates with February 4 will be group and will occur once only (rowspan).


回答1:


Try with jQuery - 

<table border=1 id="myTable">
    <tr>
        <td>A</td>
        <td>1</td>
    </tr>
    <tr>
        <td>A</td>
        <td>2</td>
    </tr>
    <tr>
        <td>b</td>
        <td>1</td>
    </tr>
    <tr>
        <td>c</td>
        <td>1</td>
    </tr>  
    <tr>
        <td>c</td>
        <td>1</td>
    </tr>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(document).ready(function() {
       var span = 1;
       var prevTD = "";
       var prevTDVal = "";
       $("#myTable tr td:first-child").each(function() { //for each first td in every tr
          var $this = $(this);
          if ($this.text() == prevTDVal) { // check value of previous td text
             span++;
             if (prevTD != "") {
                prevTD.attr("rowspan", span); // add attribute to previous td
                $this.remove(); // remove current td
             }
          } else {
             prevTD     = $this; // store current td 
             prevTDVal  = $this.text();
             span       = 1;
          }
       });
    });
</script>

Output :

enter image description here




回答2:


it works as text but it not works with

<img src='picture/".$row["j_pic"]."' width='90' height='90' border='0' />

How to ?



来源:https://stackoverflow.com/questions/28359117/rowspan-table-if-values-is-the-same-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!