-1

I am new to YII, I am getting error on site load

PHP notice
Undefined index: title
PATH\TO\WEBSITE\protected\controllers\SiteController.php(35)

Below is the code.

/**
25      * This is the default 'index' action that is invoked
26      * when an action is not explicitly requested by users.
27      */
28     public function actionIndex()
29     {
30         // renders the view file 'protected/views/site/index.php'
31         // using the default layout 'protected/views/layouts/main.php'
32         $criteria = new CDbCriteria;
33 
34         $criteria->condition = "title LIKE :title";
35         $criteria->params[':title'] =  '%' . trim($_GET['title']) . '%';
36 
37         $criteria->addCondition("category_id LIKE :category_id");
38         if( !is_null($_GET['category_id'] ) && ( $_GET['category_id'] != "" ) ) {
39             $criteria->params[':category_id'] = (int)$_GET['category_id'];
40         } else {
41             $criteria->params[':category_id'] = '%';
42         }
43         
44         if( $_GET['hightRate'] == 1 ) {
45             $criteria->addCondition("user_id IN (SELECT id FROM `yb_user` WHERE rating >= 8)");
46         }

How can I fix this error and run my website in localhost?

MH2K9
  • 11,951
  • 7
  • 32
  • 49
Nawaz
  • 303
  • 1
  • 4
  • 15
  • You are Right Jocelyn, I put **error_reporting(E_ALL ^ E_NOTICE);** in top of index.php, it worked!! Thanks a lot! – Nawaz Jul 14 '14 at 12:28
  • However, the best way is to fix the problem, not to hide it. A simple "Undefinex index" notice on top of your script could turn into a major error further down your code. – Jocelyn Jul 14 '14 at 12:30
  • Absolutely right, but its a quick fix to keep working on local system. I gotta improve it and find the main problem and fix it for sure. – Nawaz Jul 14 '14 at 12:31

2 Answers2

0

define the array first:

$criteria->params = array();

then you can add to it

$criteria->params[':title'] =  '%' . trim($_GET['title']) . '%';

Update:

this might also be because of level of error reporting in php, try changing it to

if(isset( $_GET['title'] )) // check if title is available before using it
    $criteria->params[':title'] =  '%' . trim($_GET['title']) . '%';
Developerium
  • 7,155
  • 5
  • 36
  • 56
  • Thanks @tinybyte, actually the website is running fine online, but when I tried to setup locally, created db in local and changed config/main.php for database settings and tested website is not running locally. Its running live OK, but not locally – Nawaz Jul 14 '14 at 12:20
  • also I defined param with empty array as you said, it still not working. Getting same error – Nawaz Jul 14 '14 at 12:22
0

If this is the line 35 which is causing the error :

$criteria->params[':title'] =  '%' . trim($_GET['title']) . '%';

I believe the only option here to cause the $_GET['title'].

You should not rely on $_GET data with this confidence. Try rewrite this code like this:

if(isset($_GET['title'])){
        $criteria->condition = "title LIKE :title";
        $criteria->params[':title'] =  '%' . trim($_GET['title']) . '%';
}

N.B. You should not do this kind of heavy operation in controller. Do db operations on model.

MD. Sahib Bin Mahboob
  • 20,246
  • 2
  • 23
  • 45