0

May be stupid question, but I'm not sure how to extend this code to order for example by published name, by size, by name and etc, just make more ordering buttons.

This is code:

$query ="SELECT newsvid.id, newsvid.addName, newsvid.vidTitle, newsvid.url, newsvid.vidSD, newsvid.published, videoinformation.vidLD, videoinformation.vidYear, videoinformation.vidCity, videoinformation.vidZanr, videoinformation.vidZanr2, videoinformation.vidZanr3, videoinformation.vidQuality, videoinformation.vidTranslated, videoinformation.vidTime  FROM newsvid, videoinformation WHERE newsvid.id = videoinformation.id";
$order = isset($_GET['order']) ? $_GET['order'] : 'ASC';
$goodParam = array("ASC", "DESC");

if (in_array($order, $goodParam)) {
if($order == 'ASC'){
     $query .= " ORDER BY newsvid.id ASC"; 
}else{
     $query .= " ORDER BY newsvid.id DESC"; 
    }
}

And this is my buttons:

<a href="view.php?order=ASC">ASC</a> 
<a href="view.php?order=DESC">DESC</a>

P.S. I use samples from internet and adopt to my website and now can't extended, not sure how.

Thank you

What exactly I need to change in php code to use this links?

<a href="view.php?order=ASC">ASC</a> 
<a href="view.php?order=name">BY Name</a>
<a href="view.php?order=year">By Year</a>
<a href="view.php?order=published">By Publishing</a>
<a href="view.php?order=size">By Size</a>

DO I have to using like this??

if (in_array($order, $goodParam)) {
if($order == 'ASC'){
     $query .= " ORDER BY newsvid.id ASC"; 
}else if{
     $query .= " ORDER BY newsvid.vidTitle DESC"; 
}else if{
     $query .= " ORDER BY newsvid.published DESC"; 
}else{
     $query .= " ORDER BY videoinformation.vidZard DESC"; 
    }
}
Denis
  • 103
  • 3
  • 10

2 Answers2

1

You can use more than one ORDER BY like that:

ORDER BY newsvid.id ASC, newsvid.addName DESC

That sort first the ID and with 2 equal ID the Name.

If you just try to add new sort, you just can change the name in your ORDER BY...

You can do:

if($order == 'ASC'){
     $query .= " ORDER BY newsvid.id ASC"; 
} elseif ($order == 'name') {
     $query .= " ORDER BY newsvid.addName ASC"; 
} elseif ($order == 'year') {
     $query .= " ORDER BY newsvid.vidYear ASC"; 
}
Croises
  • 18,570
  • 4
  • 30
  • 47
0

You should read up on general SQL. ORDER BY can take multiple arguments, like ORDER BY id, date_created, upvotecount.

Also: SQL multiple column ordering

Community
  • 1
  • 1
Jimtrim
  • 60
  • 6