我使用AJAX将数据库条目从SQL返回到HTML。
我想使用tablesorter JS来安排数据库条目。
我遇到的最初问题是我的表中的表行都是单独包装在TBODY中,我认为这会导致tablesorter JS无法工作。
这是HTML
<table id="results" class="tablesorter">
<thead>
<tr>
<th>Event</th>
<th>Venue</th>
<th>Genre</th>
<th>Date</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>egg presents</td>
<td>egg</td>
<td>techno</td>
<td>2018-01-08</td>
<td>0.00</td>
</tr>
</tbody>
<tbody>
<tr>
<td>fabric presents</td>
<td>fabric</td>
<td>techno</td>
<td>2018-01-08</td>
<td>0.00</td>
</tr>
</tbody>
<tbody>
<tr>
<td>fabric presents</td>
<td>fabric</td>
<td>techno</td>
<td>2018-01-08</td>
<td>20.00</td>
</tr>
</tbody>
<tbody>
<tr>
<td>EYOE pres. Kite Base</td>
<td>corsica studios</td>
<td>techno</td>
<td>2018-01-08</td>
<td>11.00</td>
</tr>
</tbody>
<tbody>
<tr>
<td>egg presents</td>
<td>egg</td>
<td>techno</td>
<td>2018-01-08</td>
<td>10.00</td>
</tr>
</tbody>
这是我的PHP / SQL请求
$query = "
SELECT * FROM event
WHERE event_date = '".$_POST["to_date"]."' AND genre = '".$_POST["request"]."' AND price <= '".$_POST["request1"]."'
";
$result = mysqli_query($connect, $query);
$output .= '
<table>
<thead>
<tr>
<th>Event</th>
<th>Venue</th>
<th>Genre</th>
<th>Date</th>
<th>Price</th>
</tr>
</thead>
';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tbody>
<tr>
<td >'. $row["event_name"] .'</td>
<td >'. $row["venue_name"] .'</td>
<td >'. $row["genre"] .'</td>
<td >'. $row["event_date"] .'</td>
<td >'. $row["price"] .'</td>
</tr>
</tbody>
</table>
';
}
}
我在Ajax请求中做错了什么?
答案 0 :(得分:1)
正如您从评论中看到的那样,尝试将<tbody>
和</tbody>
从while
循环中删除:
$query = "SELECT * FROM event
WHERE event_date = '".$_POST["to_date"]."'
AND genre = '".$_POST["request"]."'
AND price <= '".$_POST["request1"]."'";
$result = mysqli_query($connect, $query);
$output .= '<table>
<thead>
<tr>
<th>Event</th>
<th>Venue</th>
<th>Genre</th>
<th>Date</th>
<th>Price</th>
</tr>
</thead>
<tbody>';
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$output .= '<tr>
<td >'. $row["event_name"] .'</td>
<td >'. $row["venue_name"] .'</td>
<td >'. $row["genre"] .'</td>
<td >'. $row["event_date"] .'</td>
<td >'. $row["price"] .'</td>
</tr>';
}
$output.='</tbody>';
}
else{
//You can add some code if there are no result and then close </TDOBY>
$output.='</tbody>';
}
//I suggest to add <tfoot> also before close <table>
$output .= '<tfoot>
<tr>
<th>Event</th>
<th>Venue</th>
<th>Genre</th>
<th>Date</th>
<th>Price</th>
</tr>
</tfoot>
</table>';