3

Am learning & executing php by working on Joomla project How to Improve this code & resolve the PHP Notices - Any suggestions - solutions - well appreciated !!

Notice: Undefined variable: cond in*/home/mygames/public_html/components/com_toys/models/category.php on line 140 (which is $sql line)*

   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";

Notice: Trying to get property of non-object in /home/truecar7/public_html/components/com_toys/models/category.php on line 200

Line 200 is 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;
}

Edit

Hello Lodder & Elin, it works but like this, else it's showing undefined variable notice for row on return $row line.

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;
}
Ruchika
  • 503
  • 1
  • 8
  • 26
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – John Conde Aug 24 '13 at 12:26
  • Don't use special characters like `#` in table names to avoid such problems `#__menu` – Vahid Hallaji Aug 24 '13 at 12:29
  • @VahidHallaji - This is how Joomla database table are designed. It's a prefix – Lodder Aug 24 '13 at 12:31
  • I don't really understand what you are doing with the return of $row given that you have hard coded it to an empty string. If you just want to return one string for one row you should not be doing $db->loadObjectList() which is giving you an array of objects. Instead load the result you want .. don't select '*' select 'msg' and just return $db->loadResult() http://docs.joomla.org/JDatabase/loadResult – Elin Aug 25 '13 at 13:26
  • Thanks Elin, works Awesome !! – Ruchika Aug 25 '13 at 14:15

2 Answers2

2

For your Undefined Notice, You have to modify your codes like this

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

For Trying to get property of non-object Notice : I think $row is empty that is why throws notice.Check $row

var_dump($row);

Problem :

$database->loadObject(); // This line
som
  • 4,650
  • 2
  • 21
  • 36
  • Thanks. But, after Implementing its rather now showing Notice: Undefined variable: cond on line. Also, for second part shall i remove the line of $database->loadobject(). pl advise – Ruchika Aug 24 '13 at 12:56
  • Hello, I think i made an error as your solution to undefined variable works perfectly. Thanks !! – Ruchika Aug 25 '13 at 03:53
2

Try using the following. I have made some changes to your function and used Joomla 2.5 coding standards for the database query.

$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
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Lodder
  • 19,758
  • 10
  • 59
  • 100
  • Thanks, however after executing the page is showing error - Notice: Undefined variable: database in on line Fatal error: Call to a member function loadObjectList() on a non-object on line – Ruchika Aug 24 '13 at 12:59
  • ahh sorry. change `$database->loadObjectList();` to `$db->loadObjectList();` – Lodder Aug 24 '13 at 13:00
  • Yep done, however - it now shows new notices - Warning: Invalid argument supplied for foreach() on line Notice: Undefined variable: row on line – Ruchika Aug 24 '13 at 13:12
  • You need to use $db->quote($Name) not $Name (assuming you want to match the string). Are you trying to return a list or one row? You should be sure to use the appropriate return type. – Elin Aug 24 '13 at 14:11
  • @RuchikaModi - Please see my updated answer. I have tested the code and it works. I've also used `$db->quote($Name)` as pointed out by Elin – Lodder Aug 24 '13 at 16:14
  • Thanks, it works with slight modification. Have added the modification in edit, could you pl advise if correct. – Ruchika Aug 25 '13 at 01:40
  • @RuchikaModi - The `return` should go after the `foreach` loop – Lodder Aug 25 '13 at 10:38
  • Ok done, but its then showing - Notice: Undefined variable: row in line with return $row. If i modiy the edit by definining empty string then its going on fine – Ruchika Aug 25 '13 at 11:20