如何从Joomla 1.5模块中的另一个表获取数据



当前我有组件模块,我想从另一个表中添加一些数据。

这是我当前的助手。php代码:

<?php
defined('_JEXEC') or die('Direct Access to this location is not allowed.'); 
class Modprop_lastHelper
{
function getItems($ShowFeatured,$OrderBy,$Order,$amountShow,$ShowPropertyID)
{        
switch($OrderBy) {
case 0 :
$OrderBy = 'p.id';
break;
case 1 :
$OrderBy = 'p.name';
break;
case 2 :
$OrderBy = 'p.price';
break;
case 3 :
$OrderBy = 'p.hits';
break;
case 4 :
$OrderBy = 'p.refresh_time';
break;
}
switch($Order) {
case 0 :
$Order = 'ASC';
break;
case 1 :
$Order = 'DESC';
break;
}   
if($ShowPropertyID){$where= 'WHERE p.id IN ('.$ShowPropertyID.')';}else{$where=    'WHERE p.published = 1 ';}
    if($ShowFeatured){$sqlFeatured= ' AND p.featured = 1';}
    $db = &JFactory::getDBO();     
    $query = 'SELECT p.*, i.name as imagename,c.name as name_category,t.name as name_type,cy.name as name_country,s.name as name_state,l.name as name_locality,'
            . ' CASE WHEN CHAR_LENGTH(p.alias) THEN CONCAT_WS(":", p.id, p.alias) ELSE p.id END as Pslug,'
    . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as Cslug,'
    . ' CASE WHEN CHAR_LENGTH(cy.alias) THEN CONCAT_WS(":", cy.id, cy.alias) ELSE cy.id END as CYslug,'
    . ' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as Sslug,'      
    . ' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(":", l.id, l.alias) ELSE l.id END as Lslug, ' 
    . ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(":", t.id, t.alias) ELSE t.id END as Tslug '
            . ' FROM #__properties_products AS p '
            . ' LEFT JOIN #__properties_country AS cy ON cy.id = p.cyid '               
            . ' LEFT JOIN #__properties_state AS s ON s.id = p.sid '
            . ' LEFT JOIN #__properties_locality AS l ON l.id = p.lid '
            . ' LEFT JOIN #__properties_category AS c ON c.id = p.cid '
            . ' LEFT JOIN #__properties_type AS t ON t.id = p.type '
            . ' LEFT JOIN #__properties_images AS i ON i.parent = p.id '
            . $where                
            . $sqlFeatured
            . ' GROUP BY p.id ' 
            . ' ORDER BY '.$OrderBy.' '.$Order
            . ' LIMIT '.$amountShow;
    $db->setQuery($query);  
    $items = $db->loadObjectList();

   // $items = ($items = $db->loadObjectList())?$items:array(); 
//echo str_replace('#_','jos',$query);

    return $items;
}  
function NameCategory()
{   
$db     =& JFactory::getDBO();  
$query = 'SELECT * FROM #__properties_category WHERE published = 1 ORDER BY id';        
    $db->setQuery($query);        
    $Categories = $db->loadObjectList();
foreach ($Categories as $row) :
$Category[$row->id] = $row->name;
endforeach; 
return $Category;
}
function NameType()
{   
$db     =& JFactory::getDBO();  
$query = 'SELECT * FROM #__properties_type WHERE published = 1 ORDER BY name';      
    $db->setQuery($query);        
    $Types = $db->loadObjectList();
foreach ($Types as $row) :
$Type[$row->id] = $row->name;
endforeach; 

    return $Type;
}
function NameLocality()
{   
$db     =& JFactory::getDBO();  
$query = 'SELECT * FROM #__properties_locality WHERE published = 1 ORDER BY name';      
    $db->setQuery($query);        
    $Localities = $db->loadObjectList();
foreach ($Localities as $row) :
$Locality[$row->id] = $row->name;
endforeach; 

    return $Locality;
}
function getItemid( $TableName )
{
    $db =& JFactory::getDBO();  
    $query = 'SELECT id FROM #__menu' .
            ' WHERE LOWER( link ) = "index.php? option=com_properties&view='.$TableName.'&id=0"';               
    $db->setQuery( $query );
    $output = $db->loadResult();
    if(!$output){
    $query = 'SELECT id FROM #__menu' .
            ' WHERE LOWER( link ) = "index.php? option=com_properties&view=properties"';                
    $db->setQuery( $query );
    $output = $db->loadResult();
    }
    if(!$output){
    $query = 'SELECT id FROM #__menu' .
            ' WHERE LOWER( link ) = "index.php?option=com_properties&view=properties&cid=0&tid=0"';             
    $db->setQuery( $query );
    $output = $db->loadResult();
    }
    return $output;
}
} 
?>

我将其他功能添加到helper.php中以从另一个表属性属性获取数据:

    function Units()
{   
$db     =& JFactory::getDBO();  
$query = ' SELECT u.* '         
        . ' FROM #__properties_units as u '                 
        . ' WHERE u.published = 1 AND u.productid = p.id'           
        . ' order by u.ordering ';      
    $db->setQuery($query);   
    $Units = $db->loadObjectList();

return $Units;
}

当我尝试从单位表中显示一些东西时,什么也不会出现。我在TMP视图中使用以下代码:

<?php echo $Units->title;?>

代码怎么了?

这是您要做的:

  • 重命名您的函数单位() function getunits()
  • 重命名 $ units = $ db-> loadObjectList(); in $ utput = $ db-> loadObjectList();
  • 重命名返回$ units; in 返回$ output;

上面的3个更改只是为了使您的代码保持一致。

现在,在您的模块PHP文件中,您可以以这样的呼叫助手:

$units = Modprop_lastHelper::getUnits();

var_dump($units);

确保包括助手文件(例如):

require_once(dirname(__FILE__).DS.'helper.php');


  1. 我也强烈建议您学习Joomla!有关创建简单模块的文档。

  2. 使用var_dump()来调试代码。更多,对"您如何调试PHP脚本"的主题进行一些研究。您不会在任何地方找到帮助,只是说"它不起作用……"。

相关内容

  • 没有找到相关文章

最新更新