Jump to content

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

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.