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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.