3

我正在通过在 Joomla 项目上工作来学习和执行 php 如何改进此代码并解决 PHP 通知 - 任何建议 - 解决方案 - 非常感谢!

注意:未定义的变量:cond in*/home/mygames/public_html/components/com_toys/models/category.php 在第 140 行(即 $sql 行)*

   function loadSubCat($id,$Carmodel,$minprice,$maxprice){
   $mainframe =& JFactory::getApplication();
   $option = JRequest::getCmd('option');
   $database =& JFactory::getDBO();
   global $Itemid;  
   if($Carmodel!="")
   $cond=" and prod_id='$Carmodel' ";
   $sql = "Select * from #__toycar_products Where prod_cat_id='".$id."' $cond and prod_status='1' and prod_id in (select v_prod_id from #__toycar_variants)  Order By prod_sorder";

注意:尝试在第 200 行的 /home/truecar7/public_html/components/com_toys/models/category.php 中获取非对象的属性

第 200 行是 return $row->id;

   function getItemIdByName($Name){
   $mainframe =& JFactory::getApplication();
   $option = JRequest::getCmd('option');
   $database =& JFactory::getDBO();
   $sql = "Select id  from #__menu Where name = '".$Name."'";
   $database->setQuery($sql);
   $row = $database->loadObject();
   return $row->id;
}

编辑

你好 Lodder & Elin,它可以工作,但就像这样,否则它会在返回 $row 行上显示未定义的变量通知。

function getItemIdByName($Name){
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
 ->from('#__menu')      
 ->where('id = ' . $db->quote($Name));      
$db->setQuery($query);
$rows = $db->loadObjectList();

foreach ($rows as $row){
    $row = $row->msg;
     }    
$row='';
return $row;
}
4

2 回答 2

2

对于您的Undefined Notice,您必须像这样修改您的代码

$cond = '';
if($Carmodel!="") {
   $cond = " and prod_id='$Carmodel' ";
}

对于Trying to get property of non-object通知:我认为$row是空的,这就是抛出通知的原因。检查$row

var_dump($row);

问题 :

$database->loadObject(); // This line
于 2013-08-24T12:28:39.777 回答
2

尝试使用以下内容。我对您的函数进行了一些更改,并使用 Joomla 2.5 编码标准进行数据库查询。

$Name = "XXXXXXXXX";  //define the name variable

function getItemIdByName($Name){
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query->select('*')
     ->from('#__menu')      
     ->where('id = ' . $db->quote($Name));      
    $db->setQuery($query);
    $rows = $db->loadObjectList();

    foreach ($rows as $row){
        $row = $row->msg;
    }
    return $row;
}

echo getItemIdByName($Name); //echo the result of the function
于 2013-08-24T12:30:50.307 回答