沃梦达 / 编程问答 / php问题 / 正文

如果值在 PHP 中相同,则行跨表

Rowspan table if values is the same in PHP(如果值在 PHP 中相同,则行跨表)

本文介绍了如果值在 PHP 中相同,则行跨表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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:

我希望 2 月 4 日的所有日期都是分组的,并且只会出现一次(rowspan).

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

推荐答案

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>

输出:

这篇关于如果值在 PHP 中相同,则行跨表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如果值在 PHP 中相同,则行跨表

基础教程推荐