chithon chithon - 1 month ago 9
MySQL Question

Cannot sort column on clicking column header in php

I have written code to sort column by clicking header column.
I have created array of column name to be sorted then created select query with

$sort
and
$order
. In query string it passes the columname and ASC or DESC but problem it takes me to blank page and it if I pass name of page then also it shows blank page. Can anyone help me to find error.

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


This is what I get in url::::

/mainpage.php?sort=first_name&order= DESC
and blank page is displayed.

I got to my problem is that as I am displaying html table by function list_users() which is called in following way::

if ((empty($_POST))&&(empty($_GET)))
{
list_users();
die();
}


If I remove if condition then it problems to my other function add contact form is displayed below list. If only
list_user()
is called it sorts column.

Answer

Solution:

As I called displayed result in a function earlier so now I directly displayed result set rather than any function so the sort function which I have written for column header is working now properly.And my other function are also working properly.

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