views:

47

answers:

1

Hello. 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)) { 
?> 
+2  A: 

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...

Mike Sherov
This is good, exactly what i was asking for.. but how do i do, if i also have ORDER BY id ?
Karem
I've edited my example as you've requested
Mike Sherov
i would go for something like $sort_options = array("video" => " where `SCtry` = 0", ...); if( isset($sort_options[$_GET['sort']]) ) $sql .= $sort_option[$_GET['sort']];but it would be much nicer to use something like doctrine or zend_db_table anyway
roman
Roman, I agree. However, the nature of this question is so basic, I wanted to tailor the answer to code the OP could understand and use as is.
Mike Sherov
Thanks Mike....
Karem