我有代码进行搜索。但搜索结果不在同一张表中。所有搜索结果都显示在不同的表格中。如何让它们出现在一个表格中?
屏幕截图:
这是我的代码:
> search.php
<?php
$query = $_GET['query'];
$min_length = 1;
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM barang WHERE (`tanggal` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_assoc($raw_results)){
?>
<table width="107%" class="view">
<thead>
<tr>
<th width="180">Tanggal</th>
<th width="150">Barang Masuk</th>
<th width="90">Bijih Keluar</th>
<th width="120">Kantong Hitam Keluar</th>
<th width="120">Kantong Putih Keluar</th>
<th width="90">Stok Bijih</th>
<th width="90">Stok Kantong Hitam</th>
<th width="90">Stok Kantong Putih</th>
<th width="130">Catatan</th>
</tr>
</thead>
<td><?php echo $results['tanggal']; ?></td>
<td><?php echo $results['barang_in']; ?></td>
<td><?php echo $results['bijih_out']; ?></td>
<td><?php echo $results['htm_out']; ?></td>
<td><?php echo $results['pth_out']; ?></td>
<td><?php echo $results['bijih']; ?></td>
<td><?php echo $results['kantong_htm']; ?></td>
<td><?php echo $results['kantong_pth']; ?></td>
<td><?php echo $results['note']; ?></td>
<?php
}
}
else{ // if there is no matching rows do following
echo "Hasil tidak bisa ditemukan atau tidak ada di dalam database.";
}
}
else{
echo "Minimum length is ".$min_length;
}
?>
那么如何让搜索结果只显示在一个表中呢?我把表格代码错了吗?或者是其他东西?还有一个问题,如何为每个结果添加数字?提前感谢您的时间和帮助。
答案 0 :(得分:1)
将<table>
标记移到while
循环之外。
应该是这样的..
echo "<table width="107%" class=/"view/">";
while($results = mysql_fetch_assoc($raw_results)){
?>
<!-- Comment this
<table width="107%" class="view">
-->
<thead>
答案 1 :(得分:1)
将<table>
标记移到while循环之外,并在while循环中添加<TR>
标记,不要忘记关闭标记。
对于序列号,您必须将另一个变量作为计数器引入。在下面给出的代码中,我添加了$i
。如果您已在代码中使用$ i,请更改它。
我认为更改代码后会看起来像
?>
<table width="107%" class="view">
<thead>
<tr>
<th>SN</th>//new Line
<th width="180">Tanggal</th>
<th width="150">Barang Masuk</th>
<th width="90">Bijih Keluar</th>
<th width="120">Kantong Hitam Keluar</th>
<th width="120">Kantong Putih Keluar</th>
<th width="90">Stok Bijih</th>
<th width="90">Stok Kantong Hitam</th>
<th width="90">Stok Kantong Putih</th>
<th width="130">Catatan</th>
</tr>
</thead>
<?php
$i=1;//new line
while($results = mysql_fetch_assoc($raw_results)){
?>
<tr>
<td><?php echo $i; ?> </td>//new line
<td><?php echo $results['tanggal']; ?></td>
<td><?php echo $results['barang_in']; ?></td>
<td><?php echo $results['bijih_out']; ?></td>
<td><?php echo $results['htm_out']; ?></td>
<td><?php echo $results['pth_out']; ?></td>
<td><?php echo $results['bijih']; ?></td>
<td><?php echo $results['kantong_htm']; ?></td>
<td><?php echo $results['kantong_pth']; ?></td>
<td><?php echo $results['note']; ?></td>
</tr>
<?php
$i++;//new line
}
?>
</table>
<?php
}
else{ // if there is no matching rows do following
echo "Hasil tidak bisa ditemukan atau tidak ada di dalam database.";
}
答案 2 :(得分:1)
使用此
<?php
$query = $_GET['query'];
$min_length = 1;
if(strlen($query) >= $min_length)
{
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM barang WHERE (`tanggal` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0)
{
?>
<table width="107%" class="view">
<thead>
<tr>
<th width="180">Tanggal</th>
<th width="150">Barang Masuk</th>
<th width="90">Bijih Keluar</th>
<th width="120">Kantong Hitam Keluar</th>
<th width="120">Kantong Putih Keluar</th>
<th width="90">Stok Bijih</th>
<th width="90">Stok Kantong Hitam</th>
<th width="90">Stok Kantong Putih</th>
<th width="130">Catatan</th>
</tr>
</thead>
<?php
while($results = mysql_fetch_assoc($raw_results))
{
<tr>
<td><?php echo $results['tanggal']; ?></td>
<td><?php echo $results['barang_in']; ?></td>
<td><?php echo $results['bijih_out']; ?></td>
<td><?php echo $results['htm_out']; ?></td>
<td><?php echo $results['pth_out']; ?></td>
<td><?php echo $results['bijih']; ?></td>
<td><?php echo $results['kantong_htm']; ?></td>
<td><?php echo $results['kantong_pth']; ?></td>
<td><?php echo $results['note']; ?></td>
</tr>
<?php
}
?>
</table>
<?php
}
else
{ // if there is no matching rows do following
echo "Hasil tidak bisa ditemukan atau tidak ada di dalam database.";
}
}
else
{
echo "Minimum length is ".$min_length;
}
?>