Jump to content

Returning results in order


soycharliente

Recommended Posts

On my users.php page, I'm getting the user information based on their user id in the query: users.php?id=398457934

 

I'm trying to create prev/next links to move from user to user. I need to to move back and forth based on lastname alphabetically.

 

The uid is assigned by the system and cannot be changed to assign in alphabetical order. The users are not stored in alphabetical order either.

 

This query returns nothing in phpMyAdmin (no results, no empty set, no error):

(SELECT `uid`
   FROM `users`
   WHERE (SELECT `lastname`
           FROM `users`
           WHERE `uid`<{$id}
           ORDER BY `uid` DESC
           LIMIT 1)
)
UNION
(SELECT `uid`
   FROM `users`
   WHERE (SELECT `lastname`
           FROM `user`
           WHERE `uid`>{$id}
           ORDER BY `uid` DESC
           LIMIT 1)
)

 

I don't know where to go from here. It used to be a lot longer and more wrong. I've managed to think through it and eliminate things to this point.

 

Any thoughts or help?

Link to comment
https://forums.phpfreaks.com/topic/189371-returning-results-in-order/
Share on other sites

Hi

 

Think you need something closer to:-

 

(SELECT `uid`
   FROM `users`
   WHERE `lastname` < (SELECT lastname FROM `users` WHERE `uid` = {$id})
   ORDER BY `lastname` DESC
   LIMIT 1
)
UNION
(SELECT `uid`
   FROM `users`
   WHERE `lastname` > (SELECT lastname FROM `users` WHERE `uid` = {$id})
   ORDER BY `lastname` ASC
   LIMIT 1
)

 

All the best

 

Keith

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.