Jump to content

[SOLVED] Table Sorting


Zergman

Recommended Posts

Trying to allow my users to sort the data in a table based on what column they click.  Im not good with coding so please bare with me.

 

I created a variable to get the column name from a link tied to the column header.

$orderedby = $_GET['sortby'];

 

The table column headers look like this

<a href="http://www.mysite.com/admin/main.php?sortby=level1">Level 1</a>

 

Then my query to show the data

"SELECT * FROM `data` WHERE tdate = '$date' ORDER BY '$orderedby'";

 

Only thing is it doesn't work.  Can't figure out what im doing wrong.

 

Suggestions?

Link to comment
Share on other sites

Try this and tell use what you get:

$orderedby = $_GET['sortby'];
echo $orderedby;

Then write:

$query = mysql_query("SELECT * FROM `data` WHERE tdate = '$date' ORDER BY '$orderedby'") or die("Error was: " . mysql_error());

If it comes up with "Error was: blah blah blah", then your problem should be explained in that. If not, tell use what showed up.

Link to comment
Share on other sites

tried what you said trecool999, no errors.

 

If I do this, it doesn't work

$query_rstodayincidents = "SELECT * FROM `data` WHERE tdate = '$date' ORDER BY '$orderedby'";

 

If I do this, it works

$query_rstodayincidents = "SELECT * FROM `data` WHERE tdate = '$date' ORDER BY level1";

 

So im assuming its my variable that isn't working properly

Link to comment
Share on other sites

That makes sense.

Replace the 's around $orderby with `s.

If that doesn't work either, just take the quotes around $orderby out completely.

 

I get a unknown column error when I use `s.  If I remove the quotes completely, I get syntax errors.

Link to comment
Share on other sites

That makes sense.

Replace the 's around $orderby with `s.

If that doesn't work either, just take the quotes around $orderby out completely.

 

I get a unknown column error when I use `s.  If I remove the quotes completely, I get syntax errors.

:S

Try a query using non-variables and just strings.

Like just trying "SELECT * FROM `data` WHERE `tdate` = '06/10/08' ORDER BY 'level1";

Link to comment
Share on other sites

Doing this works perfectly

$query_rstodayincidents = "SELECT * FROM `data` WHERE tdate = '2008-10-06' ORDER BY level1";

 

I echo'd $orderedby at the bottom of the page, and it does show level1

 

If I put the 's around level1, it doesn't sort.

Link to comment
Share on other sites

try this

 

"SELECT * FROM `data` WHERE tdate = '2008-10-06' ORDER BY '". $orderby ."'";

Good thinking Batman, but wouldn't they return the same thing even still?

 

sigh, still won't sort but the query executes.

 

im noticing that the variable is working.

 

Perhaps its how its being put in the url?

This is how it shows in firefox

http://www.mywebsite.com/admin/main.php?sortby=level1

 

 

Link to comment
Share on other sites

I put in 3 entries to test.

 

This is how the level1 column looks like with no sorting.  Also what it looks like when I use the $orderedby

 

Level1

Found Working

Valid

Previously Working

 

 

 

When I use "ORDER BY level1" in my sql query, it sorts properly like this

 

Level1

Found Working

Previously Working

Valid

Link to comment
Share on other sites

LOL, my god why won't this work  ???

 

So I know the variable works when I click the column header, I have it echo on the page and it does change depending on what header I click.

 

I don't have any other sorting in the sql statement

 

I think its boiling down to the sql statement and syntax.

When I use this, it does sort

"SELECT * FROM `data` WHERE tdate = '$date' ORDER BY level1"

 

When I use this, it won't sort

"SELECT * FROM `data` WHERE tdate = '$date' ORDER BY '$orderedby'"

or this

"SELECT * FROM `data` WHERE tdate = '$date' ORDER BY 'level1'"

 

Any other suggestions?

Link to comment
Share on other sites

Table

[pre]

+--------+------------------+------+-----+---------+----------------+

| Field  | Type            | Null | Key | Default | Extra          |

+--------+------------------+------+-----+---------+----------------+

| iddata | int(10) unsigned | NO  | PRI | NULL    | auto_increment |

| level1 | varchar(15)      | NO  |    | NULL    |                |

| tdate  | date            | NO  |    | NULL    |                |

+--------+------------------+------+-----+---------+----------------+

[/pre]

 

Data

[pre]

+--------+--------+------------+

| iddata | level1 | tdate      |

+--------+--------+------------+

|      1 | aa    | 2008-10-01 |

|      2 | bb    | 2008-10-01 |

|      3 | dd    | 2008-10-01 |

|      4 | gg    | 2008-10-02 |

|      5 | ee    | 2008-10-01 |

|      6 | cc    | 2008-10-01 |

+--------+--------+------------+

[/pre]

 

Code - works

<?php
include 'db.inc.php';
if (isset($_GET['sortby']))
{
    $orderby = $_GET['sortby'];
    $sql = "SELECT level1 FROM data WHERE tdate = '2008-10-01' ORDER BY $orderby";
    $res = mysql_query($sql);
    while ($row = mysql_fetch_row($res))
    {
        echo $row[0], '<br/>';
    }
}
?>
<form>
Sort by <input type="text" name="sortby" value="level1" size="12">
<br/>
<input type="submit" name="btnSubmit" value="Sort">
</form>

 

Sorted results

[pre]

aa

bb

cc

dd

ee

[/pre]

 

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.