Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to sort column by clicking on column header iin PHP

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 6.50k
    Comment on it

    Below code will gives us a table on which when we  first time click the headers of column, it display data in ascending order.  if we click the header second time than it display values in descending  order.

     

    <?php
    
        // first creating database connection
    $servername = "localhost"; 
    $username = "root";
    $password = "pass";
    $database = "demo"; //this will contain name of a database
    // Create connection
    $conn = new mysqli($servername, $username, $password, $database);
    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
        
    
    
    $action = '';    
    $sql = "SELECT * from employee";
    $where = '';
    if(isset($_GET["id"]))
    {
    
    	
         $id     = $_GET["id"];   //geting id value which we are passing from table headers
         $action = $_GET["action"]; // geting action value which we are passing from table headers
    
    	
        //we are checking condition if $action value is ASC then $action will set to DESC otherwise it will remain ASC
    	if($action == 'ASC')
    	 { 
    		$action='DESC';
    	 }
    	 else  
    	 { 
    		$action='ASC';
    	 }
    
    	if($_GET['id']=='id') 
    	{
    		$id = "e_id";
    	}
    	elseif($_GET['id']=='name') 
    	{
    		$id = "name";
    	}
    	elseif($_GET['id']=='department') 
    	{
    		$id="department";
    	}
    	elseif($_GET['id']=='salary') 
    	{
    		$id="salary";
    	}
    		$where = " ORDER BY  $id $action ";
    		$sql = "SELECT * FROM employee " . $where;
        }
    
       
    ?>
    <html>
    <body>
    		
    	<table>
            <tr>
    
              //on click on <th> tag we are passing id and action values
    
    		<th><a href="employee_record.php?id=<?php echo 'id';?>&action=<?php echo $action;?>">ID</a></th>
    		<th><a href="employee_record.php?id=<?php echo 'name';?>&action=<?php echo $action;?">NAME</a></th>
    		<th><a href="employee_record.php?id=<?php echo 'department';?>&action=<?php echo $action;?>">DEPARTMENT</a></th>
    		<th><a href="employee_record.php?id=<?php echo 'salary';?>&action=<?php echo $action;?>">SALARY</a></th>
    	</tr>
    	
    <?php
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) { 
      
    // Fetch a result row as an associative array
    
         while($row = $result->fetch_assoc()) { ?>
         	 <tr>
     	    	<td><?php echo $row["e_id"];?></td>
     			<td><?php echo $row["name"];?></td>
     			<td><?php echo $row["department"];?></td>
     			<td><?php echo "$".$row["salary"];?></td>
     		</tr> 
    		
    <?php 
            
         }
    	     echo '</table>';
    	     echo '</div>';
         } 
     	else 
         {
           echo "0 results";
         }
       		
    $conn->close();
    
    
    ?>
    
    </body>
    </html>

 1 Comment(s)

  • Super :) Es lst mein Problem auf, aber in der Zeile 70 fehlt ">"

    <th><a href="employee&#95;record.php?id=<?php echo 'name';?>&action=<?php echo $action;?">NAME</a></th>
    
    <th><a href="employee&#95;record.php?id=<?php echo 'name';?>&action=<?php echo $action;?>">NAME</a></th>
    

    und wegen

    echo '</table>';
    

    im der Zeile 93 zeigt mir Unkompatibilitt mit HTML5

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: