Karem
Karem

Reputation: 18113

PHP: sorting out in a smart coding way?

So i have this videosection, but i want to make a "sorting" option avaible for users.

Sorting Options: ALL (no WHERE in the sql statement), Videoklips (WHERE SCtry = 0), SC try (WHERE SCtry = 1)

Now, i know how to do it "my" way. I would have placed links on index.php: ?sort=video and ?sort=SCtry

Then make 2, if sort video, if sort sctry and then duplicate the whole index.php right now(which displays everything) into the 2 if's and then just edit the SQL statement SELECT, with WHERE SCtry = '0' on ?sort=video, and WHERE SCtry = '1' on ?sort=SCtry.

Now, i KNOW how to sort out, but i want to code it in a smarter way (if it exists, of course), because this seems to be too much to duplicate the whole site and then just only change 1 line...

Example of what i ment with index.php, that i am going to duplicate:

<?php 
$hent_meetup = mysql_query("SELECT * FROM member_film ORDER BY id DESC LIMIT 0,200") or die(mysql_error()); 
while($vis = mysql_Fetch_array($hent_meetup)) { 
?> 

Upvotes: 0

Views: 309

Answers (1)

Mike Sherov
Mike Sherov

Reputation: 13427

without seeing example code, I can tell you this is an example of what I'd do.

<?
//all of the code before the SQL statement here...
$sql= ' SELECT `column` from `tablename`'; //or any other SQL that is appropriate before the conditional
if(isset($_GET['sort'])){
    if($_GET['sort'] == 'video'){
        $sql .= ' WHERE `SCtry` = 0';
    }elseif($_GET['sort'] == 'SCtry'){
        $sql .= ' WHERE `SCtry` = 1';
    }
}
$sql .= ' ORDER BY `whatever`'; //or any other SQL that is appropriate after the conditional
//rest of code... no need for duplication
?>

edited as per OP request...

Upvotes: 2

Related Questions