-2

I have an index page that I'm enhancing with its search functionality by adding multiple choices of search. I can either type in a name in the search bar or click a link that would list items of certain group. All of this works, but I do get undefined variable error.

I am sending one variable with a value and another without in every search, but I am ordering the controller to pick both up from the URL. One will always be empty and the other might not be.

How do I "solve" this? Do I just suppress the error or ?

Controller

    public function index()
    {
        $search = $this->input->get('searchbox');
        $catsrch = $this->input->get('catname');
        $this->load->view('header');
            if (isset($search) && empty($catsrch)) {
                $this->searchByPostName($search);
            }
            elseif (isset($catsrch) && empty($search)) {
                $this->searchByCategory($catsrch);
            }
            else{
                $this->getAllPosts();   
            }
        $data['cat'] = $this->blog_model->getCategoryDropdown();            
        $this->load->view('categorysearch', $data);
        $this->load->view('footer');
    }

In the views, I'm doing stuff like this:

<?php
                foreach ($cat as $key) {?>
                    <tr><th><a href="<?php echo base_url()."welcome/index?searchbox=&catname=".$key['catname']; ?>"><?php echo $key['catname']; ?></a></th></tr>

                    <?php }?>
David Dr.
  • 87
  • 9
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Epodax Nov 24 '15 at 12:58
  • I looked at that question before posting mine. My problem was that I was using both empty and isset in the same condition – David Dr. Nov 24 '15 at 13:02
  • You are not sending value of `searchbox` here `searchbox=&catname` – Saty Nov 24 '15 at 13:04
  • I just gave the view as an example – David Dr. Nov 24 '15 at 13:10

1 Answers1

0

Apparently it was the empty in both if else that caused this. Only using isset() solved it.

David Dr.
  • 87
  • 9