使用PHP对表进行排序,但不按数字排序

时间:2014-06-15 08:02:36

标签: php sql

我想创建数据库,我需要的只是在php中完美(添加数据,编辑和删除)。我昨天刚刚学习了php,但是老师给我的任务是疯狂的bcz截止日期是今晚。添加数据后,我的数据库表无法按数字排序:(我使用xampp和notepad ++创建此项,请帮我修复在表格中添加数据后对结果进行排序的结果...

  <html>
<head>
<title>Data Perikanan TPI Pondokdadap, Sendangbiru, Malang</title>
</head>
<body>
<?php
$sambung = mysql_connect("localhost", "root", "") or die ("Gagal konek ke server.");
mysql_select_db("data_ikan") or die ("Gagal membuka database.");
?>
<table border="3">
 </select>
<tr>
<th>Nomor</th>
<th>Tanggal Input</th>
<th>Pukul</th>
<th>Nama Kapal</th>
<th>Nama Nelayan</th>
<th>Bobot Tangkapan</th>
<th>Jenis Tangkapan</th>
<th>Nama Penginput</th>
<th colspan="3">Aksi</th>
</tr>
<?php
$query = "select * from ikan";
$result = mysql_query($query, $sambung);
//$no = 0;
while ($buff = mysql_fetch_array($result)){
//$no++;
?>
<tr>
<td><?php echo $buff['nomor']; ?></td>
<td><?php echo $buff['tanggal']; ?></td>
<td><?php echo $buff['pukul']; ?></td>
<td><?php echo $buff['namakapal']; ?></td>
<td><?php echo $buff['namanelayan']; ?></td>
<td><?php echo $buff['bobottangkapan']; ?></td>
<td><?php echo $buff['jenistangkapan']; ?></td>
<td><?php echo $buff['namapenginput']; ?></td>
<td><a href="edit.php?nomor=<?php echo $buff['nomor']; ?>">Edit</a></td>
<td><a href="hapus.php?nomor=<?php echo $buff['nomor']; ?>">Hapus</a></td>
</tr>
<?php
}
mysql_close($sambung);
?>
</table>
<p align="left"><a href="tambah.html">Tambah Data</a></p>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

使用MySQL的ORDER BY子句对数据进行排序。变化

$query = "select * from ikan";

$query = "select * from ikan order by nomor";

或指定方向:

$query = "select * from ikan order by nomor asc";
$query = "select * from ikan order by nomor desc";