Jump to content

Recommended Posts

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

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.

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.

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.

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.

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

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.