Siggles Posted August 27, 2008 Share Posted August 27, 2008 I have found this very handy for backgrounds but I am trying to use it on column ordering and it doesn't work. For example... $sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'pid'; switch ($sort) { case 'un': $order_by = ($order_by=="predictions.username ASC" ? 'predictions.username DESC' : 'predictions.username ASC'); break; } and then.. SELECT...... FROM ..... fixtures.id ORDER BY $order_by and finally <td width="27%"><a href="admin.php?sort=un"><u>Username</u></a></td> Is it to do with me refreshing the page? Maybe I need a global or session variable? Link to comment https://forums.phpfreaks.com/topic/121553-solved-the-ternary-operator/ Share on other sites More sharing options...
Jabop Posted August 27, 2008 Share Posted August 27, 2008 I'm not sure of the question? I have a question though. If you're doing a switch and you've already met your scenario, how come you're doing another if/else statement within the result? Link to comment https://forums.phpfreaks.com/topic/121553-solved-the-ternary-operator/#findComment-626885 Share on other sites More sharing options...
obsidian Posted August 27, 2008 Share Posted August 27, 2008 Yes, when your page refreshes, you lose the value of your $order_by variable. I wold recommend something like this to pass both the sort column and the order by: <?php $sort = isset($_GET['sort']) ? $_GET['sort'] : 'un'; // default to whatever column $ord = isset($_GET['ord']) && in_array($_GET['ord'], array('ASC', 'DESC')) ? $_GET['ord'] : 'ASC'; switch ($sort) { case 'un': $col = 'predictions.username'; break; } $q = "SELECT * FROM table_name ORDER BY $col $ord"; ?> Hope that helps. Link to comment https://forums.phpfreaks.com/topic/121553-solved-the-ternary-operator/#findComment-626890 Share on other sites More sharing options...
Mchl Posted August 27, 2008 Share Posted August 27, 2008 Do you want $order_by to change value each time you reload script? I.e. change sorting order? Then yes, a session variable would be needed. Link to comment https://forums.phpfreaks.com/topic/121553-solved-the-ternary-operator/#findComment-626891 Share on other sites More sharing options...
Siggles Posted August 27, 2008 Author Share Posted August 27, 2008 I'm not sure of the question? I have a question though. If you're doing a switch and you've already met your scenario, how come you're doing another if/else statement within the result? Not sure I get you? I know that the ternary operator is like an if statement if that is what you mean. The question is, I would like the $order_by varaible to alternate between ASC and DESC. There is probably a much quicker route than using a switch statment but I started this way and am trying to get it to work. Any suggestions would be great. Link to comment https://forums.phpfreaks.com/topic/121553-solved-the-ternary-operator/#findComment-626892 Share on other sites More sharing options...
Siggles Posted August 27, 2008 Author Share Posted August 27, 2008 Thank you for your help. In the end I went for... $sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'id'; switch ($sort) { case 'id': $order_by = ($_SESSION['$orderby'] =="predictions.id ASC" ? 'predictions.id DESC' : 'predictions.id ASC'); break; case 'un': $order_by = ($_SESSION['$orderby'] =="predictions.username ASC" ? 'predictions.username DESC' : 'predictions.username ASC'); break; } $_SESSION['$orderby'] = $order_by; Whether that is correct or the quickets way, I don't know. BUt it seems to work :-) Thanks for you help. Link to comment https://forums.phpfreaks.com/topic/121553-solved-the-ternary-operator/#findComment-626923 Share on other sites More sharing options...
obsidian Posted August 27, 2008 Share Posted August 27, 2008 Whether that is correct or the quickets way, I don't know. BUt it seems to work :-) Out of curiosity, did you see my previous post? Sessions is not a horrible way to go, but I didn't see any reference to my code, so I wasn't sure if you saw it at all. Link to comment https://forums.phpfreaks.com/topic/121553-solved-the-ternary-operator/#findComment-626931 Share on other sites More sharing options...
Siggles Posted August 27, 2008 Author Share Posted August 27, 2008 Whether that is correct or the quickets way, I don't know. BUt it seems to work :-) Out of curiosity, did you see my previous post? Sessions is not a horrible way to go, but I didn't see any reference to my code, so I wasn't sure if you saw it at all. Hi, I used a session variable with the new code. Cheers Link to comment https://forums.phpfreaks.com/topic/121553-solved-the-ternary-operator/#findComment-626974 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.