Jump to content

Sort table


hazz995

Recommended Posts

Trying to sort a table out by certain tags, like username, date registered and member type...

No matter how many times I click the links the data wont sort, I had it working with only sorting 2 table columns but it broke when I attempted to add a 3rd.

 

This is the code that starts the sort of, it works by extracting the sort variable from the url then using it to decide what column is being sorted, unfortunately it does not work. :(

<?php

$sort = $_GET['sort'];
$connect=mysql_connect("localhost","root","root") or die("Could not connect");
mysql_select_db("dbmembers") or die("Could not find database");
	if ($sort=0) 
		{$query = "SELECT * FROM users ORDER BY date";}
	if ($sort=1) 
		{$query = "SELECT * FROM users ORDER BY username";}
	if ($sort=2) 
		{$query = "SELECT * FROM users ORDER BY admin";}

   echo "<center><table width=100% style=padding:5;><tr><td><b><a href='?sort=0'>Username</a></b></td><td><b><a href='?sort=1'>Date Registered</a></b></td>
   <td><b><a href='?sort=2'>Type</a></b></td>";
   
//Rest of table code... 

   ?>

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/190257-sort-table/
Share on other sites

your conditions are incorrectly written:

 

if ($sort=0)

 

you're evaluating whether $sort=0 is true. it always will be, since you're just checking whether $sort can be successfully assigned a value of 0. if you want to COMPARE the values, you need to use two equal signs. read up in the PHP manual on operators.

Link to comment
https://forums.phpfreaks.com/topic/190257-sort-table/#findComment-1003793
Share on other sites

your conditions are incorrectly written:

 

if ($sort=0)

 

you're evaluating whether $sort=0 is true. it always will be, since you're just checking whether $sort can be successfully assigned a value of 0. if you want to COMPARE the values, you need to use two equal signs. read up in the PHP manual on operators.

 

*facepalm* It completely slipped my mind...

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/190257-sort-table/#findComment-1003797
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.