1
urbanspacema
problem with getAllChild

Hello
I'm trying to build a module and have some problem to retrive child from parent category.

this is the pice of code

$mytree = new XoopsObjectTree($table$id$pid);
$child_array $mytree->getAllChild($id);


$id is the id of parent category.


Debug:
Warning: array_keys() [function.array-keys]: The first argument should be an array in file /class/tree.php line 79
Warning: Invalid argument supplied for foreach() in file /class/tree.php line 79
Warning: array_keys() [function.array-keys]: The first argument should be an array in file /class/tree.php line 79
Warning: Invalid argument supplied for foreach() in file /class/tree.php line 79

2
ghia
Re: problem with getAllChild
  • 2009/8/19 17:36

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


In the first line $id and $pid should be the field names for child and parent id.
That is not consistent with the second line where $id should be a value.

3
urbanspacema
Re: problem with getAllChild

Ghia
I did not understand, I sent you a pm

4
urbanspacema
Re: problem with getAllChild

Here is my code (i'm trying to build a new sitemap module called xsitemap)

<?php
/**
 * ****************************************************************************
 * Module généré par TDMCreate de la TDM "http://www.tdmxoops.net"
 * ****************************************************************************
 * xsitemap - MODULE FOR XOOPS AND IMPRESS CMS
 * Copyright (c) Urbanspaceman (http://www.takeaweb.it)
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       Urbanspaceman (http://www.takeaweb.it)
 * @license         GPL
 * @package         xsitemap
 * @author             Urbanspaceman (http://www.takeaweb.it)
 *
 * Version : 1.00:
 * ****************************************************************************
 */
     

//Show xsitemap
function xsitemap_get_map($table$id_name$pid$title_name$url$order ""){
    global 
$sitemap_configs;
    
    
$xoopsDB =& Database::getInstance();
    
    
$xsitemap = array();
    
$myts =& MyTextSanitizer::getInstance();

    
$i 0;
    
$sql "SELECT `$id_name`,`$title_name` FROM ".$xoopsDB->prefix."_"."$table WHERE `$pid`= 0" ;
    if (
$order != '')
    {
        
$sql .= " ORDER BY `$order`" ;
    }
    
    
$result $xoopsDB->query($sql);
    
    
$mytree = new XoopsObjectTree($table$id_name$pid);
        
    while (list(
$catid$name) = $xoopsDB->fetchRow($result))
    {
        
        
$xsitemap['parent'][$i]['id'] = $catid;
        
$xsitemap['parent'][$i]['title'] = $myts->htmlSpecialChars$name ) ;
        
$xsitemap['parent'][$i]['url'] = $url.$catid;

        
        
//if($xsitemap_configs["show_subcategories"]){ 
            
$j 0;
            
            
$child_array $mytree->getAllChild($catid);
                echo 
"+++";
                
print_r($child_array);
                echo 
"+++";
            foreach (
$child_array as $child)
            {
                
$xsitemap['parent'][$i]['child'][$j]['id'] = $child[$id_name];
                
$xsitemap['parent'][$i]['child'][$j]['title'] = $myts->htmlSpecialChars$child[$title_name] ) ;
                
$xsitemap['parent'][$i]['child'][$j]['url'] = $url.$child[$id_name];
                
                
$j++;
            }
        
//}
        
$i++;
    }
    return 
$xsitemap;
    
}

?>


my problem are in getAllChild. I proviede a numeric id to getAllChild but does not return any value.

the first voice in
$mytree = new XoopsObjectTree($table, $id_name, $pid);
should be an array, but I did not understand where to take this array ...


any advice?
Tnx

5
trabis
Re: problem with getAllChild
  • 2009/8/20 13:55

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


It should be :
$mytree = new XoopsTree($table, $id_name, $pid);

XoopsObjectTree needs an object to work.

6
urbanspacema
Re: problem with getAllChild

But xoopstree is deprecated... or not?

7
trabis
Re: problem with getAllChild
  • 2009/8/20 14:20

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Quote:

urbanspacema wrote:
But xoopstree is deprecated... or not?


Yes.
This is a problem for modules that do not use XOOPS object handlers.
However you can fix it by creating a dummy object that you can use in XoopsObjectTree.

Try something like this:

global $xoopsDB;
//include once the file where XoopsDummyObject class is.
$table_name 'xoopstube_cat';
$id_name 'cid';
$pid_name 'pid';
$title_name 'title';

$sql "SELECT * FROM " $xoopsDB->prefix($table_name);  // here goes the table name
$result $xoopsDB->query($sql);
while (
$row $xoopsDB->fetchArray($result)) {
    
$objsArray[] = new XoopsDummyObject($row$id_name$pid_name$title_name);
}
$topic_tree = new XoopsObjectTree($objsArray$id_name$pid_name);


// better place this on a separate file so you can reuse it
class XoopsDummyObject extends XoopsObject
{
    
/**
     * constructor
     */
    
function XoopsDummyObject($row$id_name 'cid'$pid_name 'pid'$title_name 'title')
    {
        
$this->XoopsObject();
        
$this->initVar($id_nameXOBJ_DTYPE_INT$row[$id_name]);
        
$this->initVar($pid_nameXOBJ_DTYPE_INT$row[$pid_name]);
        
$this->initVar($title_nameXOBJ_DTYPE_TXTBOX$row[$title_name]);
    }
}

8
urbanspacema
Re: problem with getAllChild

ok... i made the mods but... seems not work

here is a complete code

index.php
<?php
/**
 * ****************************************************************************
 * Module généré par TDMCreate de la TDM "http://www.tdmxoops.net"
 * ****************************************************************************
 * xsitemap - MODULE FOR XOOPS AND IMPRESS CMS
 * Copyright (c) Urbanspaceman (http://www.takeaweb.it)
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       Urbanspaceman (http://www.takeaweb.it)
 * @license         GPL
 * @package         xsitemap
 * @author             Urbanspaceman (http://www.takeaweb.it)
 *
 * Version : 1.00:
 * ****************************************************************************
 */
 
include "../../mainfile.php";

//template assign
$xoopsOption['template_main'] = 'xsitemap_index.html' ;

include_once 
XOOPS_ROOT_PATH."/header.php";
include_once(
XOOPS_ROOT_PATH "/class/tree.php");
include_once 
XOOPS_ROOT_PATH."/modules/xsitemap/class/plugin.php";
include_once 
XOOPS_ROOT_PATH."/modules/xsitemap/include/functions.php";
include_once(
XOOPS_ROOT_PATH "/modules/xsitemap/class/xsitemap_class.php");

$xsitemap_configs $xoopsModuleConfig ;
$pluginHandler =& xoops_getModuleHandler("xsitemap_plugin""xsitemap");

    
$criteria = new CriteriaCompo();
    
$criteria->setSort("plugin_id");
    
$criteria->setOrder("ASC");
    
$numrows $pluginHandler->getCount();
    
$plugin_arr $pluginHandler->getall($criteria);
    
    foreach (
array_keys($plugin_arr) as $i) {    
                if ( 
$plugin_arr[$i]->getVar("topic_pid") == 0)
                {
                    
$module_name $plugin_arr[$i]->getVar("plugin_name");    
                    
$table $plugin_arr[$i]->getVar("plugin_mod_table");    
                    
$id_name $plugin_arr[$i]->getVar("plugin_cat_id");    
                    
$pid $plugin_arr[$i]->getVar("plugin_cat_pid");    
                    
$title $plugin_arr[$i]->getVar("plugin_cat_name");    
                    
$url $plugin_arr[$i]->getVar("plugin_call");    
                    
$order$plugin_arr[$i]->getVar("plugin_weight");    
                    
$online $plugin_arr[$i]->getVar("plugin_online");
                        
                    if( 
$online == ) {
                        
$xsitemap xsitemap_get_map ($table$id_name$pid$title$url$order);            
                    } 
                }
            }        
include_once 
XOOPS_ROOT_PATH."/footer.php";    
?>


functions.php
<?php
/**
 * ****************************************************************************
 * Module généré par TDMCreate de la TDM "http://www.tdmxoops.net"
 * ****************************************************************************
 * xsitemap - MODULE FOR XOOPS AND IMPRESS CMS
 * Copyright (c) Urbanspaceman (http://www.takeaweb.it)
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       Urbanspaceman (http://www.takeaweb.it)
 * @license         GPL
 * @package         xsitemap
 * @author             Urbanspaceman (http://www.takeaweb.it)
 *
 * Version : 1.00:
 * ****************************************************************************
 */
     

//Show xsitemap
function xsitemap_get_map($table$id_name$pid$title_name$url$order ""){
    global 
$sitemap_configs;
    
    
$xoopsDB =& Database::getInstance();
    
    
$xsitemap = array();
    
$myts =& MyTextSanitizer::getInstance();

    
$i 0;
    
$sql "SELECT `$id_name`,`$title_name` FROM ".$xoopsDB->prefix."_"."$table WHERE `$pid`= 0" ;
    if (
$order != '')
    {
        
$sql .= " ORDER BY `$order`" ;
    }
    
    
$result $xoopsDB->query($sql);
    
    while (
$row $xoopsDB->fetchArray($result)) {
    
$objsArray[] = new XoopsDummyObject($row$id_name$pid_name$title_name);
    }

    
$mytree = new XoopsObjectTree($objsArray$id_name$pid);
    
    while (list(
$catid$name) = $xoopsDB->fetchRow($result))
    {
        
        
$xsitemap['parent'][$i]['id'] = $catid;
        
$xsitemap['parent'][$i]['title'] = $myts->htmlSpecialChars$name ) ;
        
$xsitemap['parent'][$i]['url'] = $url.$catid;

        
        
//if($xsitemap_configs["show_subcategories"]){ 
            
$j 0;
            
            
$child_array $mytree->getAllChild($catid);
                
            foreach (
$child_array as $child)
            {
                
//$count = strlen($child['prefix']) + 1;
                
$xsitemap['parent'][$i]['child'][$j]['id'] = $child[$id_name];
                
$xsitemap['parent'][$i]['child'][$j]['title'] = $myts->htmlSpecialChars$child[$title_name] ) ;
                
//$xsitemap['parent'][$i]['child'][$j]['image'] = (($count > 3) ? 4 : $count);
                
$xsitemap['parent'][$i]['child'][$j]['url'] = $url.$child[$id_name];
                
                
$j++;
            }
        
//}
        
$i++;
    }
    return 
$xsitemap;
    
}

?>


xsitemap_class.php
<?php
/**
 * ****************************************************************************
 * Module généré par TDMCreate de la TDM "http://www.tdmxoops.net"
 * ****************************************************************************
 * xsitemap - MODULE FOR XOOPS AND IMPRESS CMS
 * Copyright (c) Urbanspaceman (http://www.takeaweb.it)
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       Urbanspaceman (http://www.takeaweb.it)
 * @license         GPL
 * @package         xsitemap
 * @author             Urbanspaceman (http://www.takeaweb.it)
 *
 * Version : 1.00:
 * ****************************************************************************
 */
     
class XoopsDummyObject extends XoopsObject
{
    
/**
     * constructor
     */
    
function XoopsDummyObject($row$id_name 'cid'$pid_name 'pid'$title_name 'title')
    {
        
$this->XoopsObject();
        
$this->initVar($id_nameXOBJ_DTYPE_INT$row[$id_name]);
        
$this->initVar($pid_nameXOBJ_DTYPE_INT$row[$pid_name]);
        
$this->initVar($title_nameXOBJ_DTYPE_TXTBOX$row[$title_name]);
    }
}
?>

9
trabis
Re: problem with getAllChild
  • 2009/8/20 14:49

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Since your not using pid_name but pid, it should be
$objsArray[] = new XoopsDummyObject($row$id_name$pid$title_name);


Also I don't know if this is correct:
$xoopsDB->prefix."_"."$table


I think it should be
$xoopsDB->prefix($table)


or just

$table


It depends on the value that is on $table. Check in queries log to see if there are errors.


10
urbanspacema
Re: problem with getAllChild

Tnx Trabis for your time, query is ok
(??SELECT `topic_id`,`topic_title` FROM x858_topics WHERE `topic_pid`= 0)

i have changed pid with pid_name

but $pid_name in first while cycle is empty...

Login

Who's Online

151 user(s) are online (105 user(s) are browsing Support Forums)


Members: 0


Guests: 151


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Mar 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits