Go Back   Pligg CMS Forum > Pligg Help > Customization Assistance

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2008, 05:27 PM
New Pligger
 
Join Date: Jul 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy How can I filter links based on selected set of categories?

I modified the code so each user can select a list of favorite categories. Now I wanna display links for each user only if the category is in his list of favorite categories. How can I do that please?
Reply With Quote
  #2 (permalink)  
Old 07-24-2008, 12:26 AM
Casual Pligger
 
Join Date: Jul 2008
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
did you get further?
Reply With Quote
  #3 (permalink)  
Old 08-09-2008, 01:40 AM
Casual Pligger
 
Join Date: Jul 2008
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Can you post your code? I would like to extend what you have. I believe what you need to change is in libs/search.php.

Inside doSearch(), there is a place where it recursively look for subcategories. What you need is to filter out categories which the person has deselected. Of course, you need to handle the "All" case.
Reply With Quote
  #4 (permalink)  
Old 08-09-2008, 04:45 AM
Casual Pligger
 
Join Date: Jul 2008
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
I have some code to get it to work, but of course there need to have some tweaks between All and frontpage/upcoming. This is because frontpage/upcoming means filtering will be applied (if category is not specified) but when All is selected, it really mean all without filtering.
Anyway, this is the changes I made based on cookie as I haven't done the user profile setting that you have mentioned

In libs/search.php, I changed from
PHP Code:
            //$catId = $db->get_var("SELECT category_id from " . table_categories . " where category_name = '" . $this->category . "';");
            
$catId get_category_id($this->category);
            if(isset(
$catId)){
                
$child_cats '';
                
// do we also search the subcategories? 
                
if($this->search_subcats == true){
                    
$child_array '';
                    
// get a list of all children and put them in $child_array.
                    
children_id_to_array($child_arraytable_categories$catId);
                    if (
$child_array != '') {
                        
// build the sql
                        
foreach($child_array as $child_cat_id) {
                            
$child_cat_sql .= ' OR `link_category` = ' $child_cat_id ' ';
                        }
                    }
                }
                
$from_where .= " AND (link_category=$catId " $child_cat_sql ")"
and added an else block
PHP Code:
            //$catId = $db->get_var("SELECT category_id from " . table_categories . " where category_name = '" . $this->category . "';");
            
$catId get_category_id($this->category);
            if(isset(
$catId)){
                
$child_cats '';
                
// do we also search the subcategories? 
                
if($this->search_subcats == true){
                    
$child_array '';
                    
// get a list of all children and put them in $child_array.
                    
children_id_to_array($child_arraytable_categories$catId);
                    if (
$child_array != '') {
                        
// build the sql
                        
foreach($child_array as $child_cat_id) {
                            
$child_cat_sql .= ' OR `link_category` = ' $child_cat_id ' ';
                        }
                    }
                }
                
$from_where .= " AND (link_category=$catId " $child_cat_sql ")";
            } else {
                if (
$_COOKIE['TestCookie'] != "" ) {
                    
//echo '<br>TestCookie' . $_COOKIE['TestCookie'] . '<br>';
                    
$child_array '';
                    
children_id_to_array_with_filter($child_arraytable_categories0';' $_COOKIE['TestCookie'] );
                    if (
$child_array != '') {
                        
// build the sql
                        
$counter 0;
                        foreach(
$child_array as $child_cat_id) {
                            
                            if (
$counter 0) {
                                
$child_cat_sql .= ' OR `link_category` = ' $child_cat_id ' ';
                            }  else {
                                
$child_cat_sql .= '`link_category` = ' $child_cat_id ' ';
                            }
                            
$counter $counter 1;
                        }
                        
$from_where .= " AND (" $child_cat_sql ")";
                        
                    }
                
                }
            } 
As you see, the filter is based on a cookie named TestCookie. ie the value set to 1;3;5 means that category 1,3,5 and its descendants will be filtered.

I also added one method to dbtree.php
PHP Code:
function children_id_to_array_with_filter(&$child_array$table$parent$filter) {
    global 
$db;
    
// retrieve all children of $parent
    
$sql 'SELECT category__auto_id FROM '.$table.' WHERE category_parent="'.$parent.'" and category__auto_id <> 0;';
    
$result $db->get_results($sql);

    if(
$result){
        foreach (
$result as $row){
        
            
//echo $filter . ' , '. $row->category__auto_id . ',' . stripos($filter,';'.$row->category__auto_id . ';') . '<br>';
            
if (!(strpos($filter,';'.$row->category__auto_id ';'))) {
                
//echo 'matched <br>';

                
$child_array[] = $row->category__auto_id;

                
// call this function again to display this child's children
                
children_id_to_array_with_filter($child_array$table$row->category__auto_id$filter);
            } 
        }
    }


For testing purpose right now, I made a test.php to set the filter

PHP Code:
<html>
<body>
<?php

echo "old value " $_COOKIE['TestCookie'] . "<br>";
if(
$_GET['filter'] == "")
#
{
    echo 
"test cookie =  " $_COOKIE['TestCookie'] . "<br>";
    echo 
"a" stripos($_COOKIE['TestCookie'], ';1;'). "<br>";
#
} else if ($_GET['filter'] == "e") {
    echo 
"You did not enter a name.";
    
setcookie("TestCookie"""time()-3600);  /* expire it */
} else {

    echo 
"You entered a name." $_GET['filter']. "<br>";
    if (
$_COOKIE['TestCookie'] != "") {
        
setcookie("TestCookie"$_COOKIE['TestCookie'] . $_GET['filter'] . ';');  /* expire it */
    
} else  {
        
setcookie("TestCookie"';' $_GET['filter'] . ';') ;  /* expire it */
    
}
    echo 
"new value " $_COOKIE['TestCookie'] . "<br>";
}

echo 
"<br>" ;



?>
</body>
</html>
I put it to the root and
http://<your host>/test.php to see the filter
http://<your host>/test.php?filter=1 to add filter category 1
http://<your host>/test.php?filter=e to erase the cookie.



Can anyone gives me the UI? I am really bad at UI setup and I can finish the rest and post it here.
Reply With Quote
Reply

Thread Tools
Display Modes
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Similar Threads
Thread Thread Starter Forum Replies Last Post
Categories links to Upcoming only... computerbar "yget" 1 01-19-2008 01:23 AM
Links to categories show all of them LordMaX General Help 1 01-03-2008 04:59 AM
Displaying specific 'extra fields' based on category selected on Submit page aaronpais General Help 0 05-16-2007 03:41 PM
Categories - links broke move55 General Help 3 02-23-2007 11:22 AM
Help with Categories / Links Efflux Template Support 0 07-25-2006 10:10 AM


Search Engine Friendly URLs by vBSEO 3.2.0