问题
<?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:
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 :
回答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