Forum Moderators: coopster

Message Too Old, No Replies

Sorting when some one click on arrows

         

suresheva31

3:47 pm on Sep 22, 2004 (gmt 0)

10+ Year Member




\/ [U]Topic Subject[/U] /\ \/ [U]views[/U] /\
123669993 264958999
264958998 264958999
264958fdf9 2649ere99

Hey guys,

As you could as above I have titles and its data's where it's populating from oracle database (writeen in php and sql plus) , and arrows pointing up and down. At the moment I do not have arrow on my titles yet. The reason I have those arrows is that if someone click on those arrows, it has to sort files: \/ = descending /\ = ascending. I do not know where to start, I know it's weird. I am just wondering if anyone did something like this before, if so please help on me where to start?

Do i need more than one SELECT QUERY?

Thanks guys

Suresh

Timotheos

5:43 pm on Sep 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you do is make the down arrow unto a link like this.

thispage.php?Order=Topic&Dir=DESC

up arrow like this
thispage.php?Order=Topic&Dir=ASC

With these variables you can then build up your query

$sql = "select * from yourtable orderby " . $_GET['Order'] . " " . $_GET['Dir'];

This is the basic principle. Of course, if you're concerned about security then you'll want to mask your field names and check you variables.

Tim

helenp

11:11 pm on Oct 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,
Just found this post,
I am trying the same but keep getting this error:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

this is my query:

$result = mysql_query ("select id, etc, from mytable order by $_GET['Order'] . " " . $_GET['Dir']");

If I change to:
$result = mysql_query ("select id, etc from mytable order by " .$_GET['Order'] . " " . $_GET['Dir']); I get this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

jatar_k

4:22 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that means that you query returned an error

try this

$sql = "select id, etc from mytable order by " .$_GET['Order'] . " " . $_GET['Dir'];
echo '<p>',$sql;
$result = mysql_query($sql) or die ('<p>'.mysql_error);

that will echo the actual query so you can see if it is missing anything and the 'or ide' will echo the actual error from mysql

helenp

7:24 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thanks, doing the or die query, I get this result:
select llegada, salida, hora_llegada, hora_salida, propiedad from bookings order by

mysql_error

And if I just take order by and this away: .$_GET['Order'] . " " . $_GET['Dir'] the select work just fine........

mincklerstraat

7:53 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try jatar's code but with this modification :

$result = mysql_query($sql) or die ('<p>'.mysql_error());

jatar just left out the parentheses (I'm making all sorts of parse errors in code I post here too; most people who help you won't actually re-check their code since that takes time). You'll be able to identify these errors soon enough if you keep your nose in the php manual when you're coding.

helenp

10:25 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thanks, now I get error:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

But the error must be in: " .$_GET['Order'] . " " . $_GET['Dir'];

Without it there are no errors, the select is just fine.

Tried changing it, but only other errors.

mincklerstraat

11:51 am on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jatar wisely changed your code to first put the query into the variable $sql instead of directly building the query with $result = mysql_query('various code here') - a very good coding habit - one of the things you'll get used to in debugging is:

echo $sql;

in fact, you might want to do:


$result = mysql_query($sql) or die ('<p>query: '.$sql.'<br />error: '.mysql_error());

(delete this or modify when you go into production).

helenp

12:38 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



nearly the same error:

select id, llegada from bookings order by

query: select id, llegada from bookings order by
error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

mincklerstraat

12:54 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$sql = "select id, llegada from bookings";
if(!empty($_GET['Order']) ¦¦!empty($_GET['dir'])) $sql .= " order by " .$_GET['Order'] . " " . $_GET['Dir'];

this is just to help you on a little further, you'll probably encounter more errors - I have no idea what the " " is doing between the two get variables, but that's your business ;) (order by [NOTHING] - this should have triggered that little question, 'order by what?) - your $_GET variables apparently aren't showing up here. However, i'd spend some more time in sql tutorials, learning about variable scope, etc., you'll probably be drawing blanks like this a whole lot until you've brushed up some more. Focus on simpler code experiments where you understand more or less everything that's happening, and then move on to more challenging material.

helenp

1:03 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>>>I have no idea what the " " is doing between the two get variables>>>
neither do I, I just copied from this post, I tried to take it away and get error........
It looked so simple on this post.
I keep on trying.

jatar_k

3:08 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry about the missing parentheses helenp

ok let's recap

Timotheos' suggestion in msg 2 is a solid one. You add those values to your links and then you can resort the query based on the $_GET values.

the thing is those links need to be properly constructed

thispage.php?Order=Topic&Dir=DESC

this would be the link to a descending search on whatever column you chose, where 'Topic' would be the column name and 'Dir' would be the direction in which to sort them.

then

$sql = "select * from yourtable orderby " . $_GET['Order'] . " " . $_GET['Dir'];

you can read here about why that query is tructured that way
[dev.mysql.com...]

helenp

4:49 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I donīt know if though use just sql=, or if instead of order by I use orderby but now I only get error if I click on the link ...

The link is properbly constructed, at least it works although give an error
echo "<td><b><a href=reservas_ordenar.php?Order=llegada&Dir=ASC>Llegada</a></b></td> \n";

$sql = "select * from bookings orderby " . $_GET['Order'] . " " . $_GET['Dir'];
$result = mysql_query($sql);
if ($row = mysql_fetch_array($result)){

But it still donīt work....
On the link you gave me they explain normal order by, but not order rows from links.
[dev.mysql.com...]
Thanks,