CodeIgniter Forums
Search from 3 tables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Search from 3 tables (/showthread.php?tid=12122)



Search from 3 tables - El Forum - 10-07-2008

[eluser]Yash[/eluser]
I need to search in 3 tables and then show result as a single one here is code for i.e. used to search one table at a time depending user option

Help to remove switches and show result from all 3 tables onto single page.

Thank you ..

Code goes here

Code:
function Search($keyword)
    {
    
        

        if(empty($keyword))
        {
            return "No keyword to search";
        }
        
            
            
            
            switch($place)
            {
                case 'post':    
                                $this->db->like('Content', $keyword);
                                $this->db->from('posts');                                
                                break;
                
                case 'page':    $this->db->like('Content', $keyword);
                                $this->db->from('pages');
                                break;
                                
                case 'tag':        $this->db->like('TagClouds', $keyword);
                                $this->db->from('posts');
                                break;                

                default:        return "NO such area found";
                                
            }
            
            $cnt=$this->db->count_all_results();
            
            if($cnt==0)
            {    
            
                $data['title']="Information Page";
                $data['header']="Information";
                $data['url']="/blog/";
                $data['time']="20";
                $data['message']="<h3>Your search did not return any results.<h3><p><a href='[removed]history.go(-1)'>Return to Previous Page</a></p>
";
                $this->load->view('info',$data);
            }

            $config['base_url'] = site_url().'blog/search/'.$place.'/'.$keyword;
            $config['total_rows'] =$cnt;
            $config['per_page'] = '10';
            $config['num_links'] = '3';
            $config['uri_segment'] = '5';
            
            $this->pagination->initialize($config);
            $strr='<div id="post">
                        <div class="top">&nbsp;</div>
                        <div class="middle">&nbsp;<h3>Search Result for : <em>'.$keyword.'</em></h3>';
            //$this->db->order_by("PostID", "desc");        
            switch($place)
            {
                case 'post':    $this->db->order_by("PostID", "desc");
                                $this->db->like('Content', $keyword);    //$this->db->or_like('desc', $keyword);
                                $query = $this->db->get('posts',$config['per_page'],$this->uri->segment(5));                                
                                
                                foreach ($query->result() as $row)
                                {
                                    //echo  $row->Title."    By : ".$row->Author."<br>";
                                    $strr.= "<p>".anchor('blog/post/'.$row->URLTitle,$row->Title)."</p>";
                                    
                                }
                                break;

                
                case 'page':    $this->db->order_by("PageID", "desc");
                                $this->db->like('Content', $keyword);
                                $query = $this->db->get('pages',$config['per_page'],$this->uri->segment(5));

                                
                                foreach ($query->result() as $row)
                                {
                                    //echo  $row->Title."    By : ".$row->Author."<br>";
                                    $strr.= "<p>".anchor('blog/page/'.$row->URLTitle,$row->Title)."</p>";
                                    
                                }
                                break;
                                
                case 'tag':        $this->db->order_by("PostID", "desc");
                                $this->db->like('TagClouds', $keyword);
                                $query = $this->db->get('posts',$config['per_page'],$this->uri->segment(5));
                                foreach ($query->result() as $row)
                                {
                                    //echo  $row->Title."    By : ".$row->Author."<br>";
                                    $strr.= "<p>".anchor('blog/post/'.$row->URLTitle,$row->Title)."</p>";
                                    
                                }
                                break;                

                default:        return "N0 such area found";
                                
            }
    
          
        
        $strr.= '&nbsp;</div><div class="bottom"></div>
                 </div>
                
                 <div id="post">
                        <div class="top">&nbsp;</div>
                        <div class="middle"><span class="nav-link">'.$this->pagination->create_links().'</span></div>
                        <div class="bottom"></div>
                 </div>';
        return $strr;
        
        
        
            
             //return $output;

        }
        
        
      
    }