tags:

views:

40

answers:

3

Hello,

$sql_where = '';
$exclude = '30,35,36,122,123,124,125'; 

if($exclude != '') 
{     
    $exclude_forums = explode(',', $exclude); 
    foreach ($exclude_forums as $id) 
    { 
        if ($id > 0) 
        { 
            $sql_where = ' AND forum_id <> ' . trim($id); 
        } 
    }
} 

$sql = 'SELECT topic_title, forum_id, topic_id, topic_type, topic_last_poster_id, topic_last_poster_name, topic_last_poster_colour, topic_last_post_time
   FROM ' . TOPICS_TABLE . '
   WHERE topic_status <> 2
      AND topic_approved = 1
      ' . $sql_where . '
   ORDER BY topic_time DESC';

The above code i use to exclude the id of forum to be displayed on sql queries.

Why doesn't it work and still display it?

Any solution

+1  A: 

You are overwriting your $sql_where variable on each loop iteration. You probably want to be using the concatenation operator (.) in the middle of your code instead:

if ($id > 0) 
{ 
    $sql_where .= ' AND forum_id <> ' . trim($id);
}

Note the change of the = to .=

zombat
+4  A: 

You're missing the dot before =

$sql_where .= ' AND forum_id <> ' . trim($id);
Silvio Donnini
A: 
if($exclude != '') $sql_where = 'AND forum_id NOT IN ($exclude); 

instead of all this messy code :)

Col. Shrapnel