-2

我已经编写了代码来通过单击标题列对列进行排序。我创建了要排序的列名数组,然后使用$sortand创建了选择查询$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>&nbsp;&nbsp;<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()被称为排序列。

4

1 回答 1

0

解决方案:

正如我之前在函数中调用显示的结果,所以现在我直接显示结果集而不是任何函数,所以我为列标题编写的排序函数现在可以正常工作。我的其他函数也可以正常工作。

echo "<form method='post'>
       <table width='50' align='right' border='0' cellspacing='0' cellpadding='0'> 
          <tr>
              <td colspan='2' align='center'>"; ?>
              <?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='500' align='center' cellpadding='0' cellspacing='0' border='1'> 
        <tr><td colspan='6' align='center' style='font-size:18px; font-weight:bold;'>Displayed Data</td></tr> 
        <tr><td colspan='6'><a href='".$_SERVER['PHP_SELF']."?action=add'>Add a new contact</a></td></tr>";

        $y = 0;  
    $sortDefault = 'id'; 

    // select array
    $sortColumns = array('id','first_name','last_name');

    // select query with sort 
    $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()); 

     if (mysql_num_rows($result)){ 
     (($y % 2) == 0) ? $bgcolor = "#8FBC8F" : $bgcolor=" #9ACD32";        
       echo " 
       <tr style='background-color:#5CB3FF' align=center>";?>
      <!-- <td><input type='checkbox' id='all' name='mainchk' /></td>-->
      <td>Chk</td>
       <td><a href='?sort=first_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><a href='?sort=email&order=<?php echo $order =='DESC' ? 'ASC' : 'DESC' ?>'>Email</td>
       <td>Status</td>
       <td id="actid">Action</td>
    <?php "</tr>";
      while($rows = mysql_fetch_assoc($result)){       

        $name   = $rows['first_name'];
        $lname  = $rows['last_name']; 
        $email  = $rows['email']; 
        $status = $rows['contact_status']; 
        $id     = $rows['id']; 

        ($status == 0) ? $status = "Available to contact" : $status = "Do not contact at present."; 

        echo"<tr align=center style='background-color:#C0C0C0'>    
        <td><input type='checkbox' value='$id' name='data[]' id='data'></td>
        <td>$name</td>
        <td>$lname</td>
        <td>$email</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>&nbsp;&nbsp;
        <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' width='50' ><a href='#' id='all' name='mainchk'>CheckAll/UnCheckAll</a>&nbsp;&nbsp;
  <input type='image' src='delete.png' alt='Submit' width='16px' height='16px' id='delete'>&nbsp;&nbsp;
  </div></form>";
于 2013-05-07T01:56:37.877 回答