在点击php中的列标题时无法对列进行排序

时间:2013-04-29 07:14:38

标签: php mysql sorting html-table

我已经通过单击标题列编写了对列进行排序的代码。 我创建了要排序的列名称数组,然后使用$sort$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(),则对列进行排序。

1 个答案:

答案 0 :(得分:0)

解决方案:

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

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>";