我已经编写了代码来通过单击标题列对列进行排序。我创建了要排序的列名数组,然后使用$sort
and创建了选择查询$order
。在查询字符串中,它传递了列名和 ASC 或 DESC,但问题是将我带到空白页面,如果我传递页面名称,它也会显示空白页面。谁能帮我找出错误。
<?php
function list_users()
{
$y = 0;
$sortDefault = 'id';
// select array for columns
$sortColumns = array('id','first_name','last_name');
// select query with sort and orderby
$sort = isset($_GET['sort']) && in_array($_GET['sort'], $sortColumns) ? $_GET['sort'] : $sortDefault;
$order = (isset($_GET['order']) && strcasecmp($_GET['order'], 'DESC') == 0) ? 'DESC' : 'ASC';
$sql = "select * from contacts ORDER BY $sort $order";
$result = mysql_query($sql) or die ("Can't run query because ". mysql_error());
echo "<form method='post'>
<table width='50' align='right' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td colspan='2' align='center' style='font-size:18px; font-weight:bold;'>"; ?>
<?php if(isset($_SESSION['username']))
{
$s="Hello,".$_SESSION["username"];
$r=$_SESSION["userrole"];
echo $s;
}
echo "<a href='logout.php' id='logout'>Logout</a></td>
</tr>
</table>
<table width='400' align='center' cellpadding='0' cellspacing='0' border='1'>
<tr><td colspan='2' align='center' style='font-size:18px; font-weight:bold;'>Displayed Data</td></tr>
<tr><td colspan='2'></td></tr>
<tr><td colspan='2'><a href='".$_SERVER['PHP_SELF']."?action=add'>Add a new contact</a></td></tr>
<tr><td colspan='2'> </td></tr>
</table>
<br/>
<br/>";
if (mysql_num_rows($result)){
(($y % 2) == 0) ? $bgcolor = "#8FBC8F" : $bgcolor=" #9ACD32";
echo "<table width='400' align='center' cellpadding='0' cellspacing='0' border='1'>
<tr style='background-color:$bgcolor;' align=center>
<td><input type='checkbox' id='all' name='mainchk' /></td>";?>
<td><a href='?sort=name&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>Name</a></td>
<td><a href='?sort=last_name&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>LastName</td>
<td>Status</td>
<td>Action</td>
<?php "</tr>";
while($rows = mysql_fetch_array($result)){
$name = $rows['first_name'];
$lname = $rows['last_name'];
$status = $rows['contact_status'];
$id = $rows['id'];
($status == 0) ? $status = "Available to contact" : $status = "Do not contact at present.";
echo"<tr align=center>
<td><input type='checkbox' value='$id' name='data[]' id='data'></td>
<td>$name</td>
<td>$lname</td>
<td>$status</td>
<td><a href='".$_SERVER['PHP_SELF']."?action=delete&id=$id' class='confirmation'><img src='delete.png' height='16px' width='16px'></a> <a href='".$_SERVER['PHP_SELF']."?action=edit&id=$id'><img src='write.png' height='16px' width='16px'></a></td>
<tr>";
$y++;
}
echo "</table>";
}else{
echo "<tr><td colspan='2' align='center'><b>No data found.</b></td></tr>";
}
echo"<div align='center'><input name='delete' type='submit' value='Delete' id='delete'></a></form>";
}
?>
这就是我在 url:::: 中得到的
/mainpage.php?sort=first_name&order= DESC
并显示空白页。
我遇到的问题是,当我通过以下方式调用的函数 list_users() 显示 html 表时:
if ((empty($_POST))&&(empty($_GET)))
{
list_users();
die();
}
如果我删除 if 条件,那么它对我的其他功能的问题添加联系表显示在列表下方。如果仅list_user()
被称为排序列。