Skip to main content

cara membuat pencarian dengan database yang sudah ada

November 1, 2010 by zyka_pratama

zyka_pratama's picture

mas, saya udah buat web sekolah dengan CI, yang salah satu tabelnya seperti ini
CREATE TABLE IF NOT EXISTS `artikel` (
`IDArtikel` int(11) NOT NULL auto_increment,
`judul` varchar(100) collate latin1_general_ci NOT NULL default '',
`isi` text collate latin1_general_ci NOT NULL,
`ringkasan` text collate latin1_general_ci NOT NULL,
`tgl` datetime NOT NULL default '0000-00-00 00:00:00',
`status` enum('draft','published') collate latin1_general_ci NOT NULL default 'draft',
`user` varchar(30) collate latin1_general_ci NOT NULL default '',
`gambar` text collate latin1_general_ci NOT NULL,
`hits` int(250) NOT NULL default '0',
`IDKategori` tinyint(11) NOT NULL default '0',
PRIMARY KEY (`IDArtikel`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=57 ;

bagaimana ya cara membuat pencarian dengan pagination ???
mohon bimbingannya,,,
terima kasih,,,

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

artikel untuk pagingnya

November 3, 2010 by zyka_pratama, 26 weeks 5 days ago
Comment: 7329

zyka_pratama's picture

artikel untuk pagingnya juga sama mas???

itu baru yang control,

November 3, 2010 by zyka_pratama, 26 weeks 5 days ago
Comment: 7328

zyka_pratama's picture

itu baru yang control, terus yang viewnya gimana mas???
maklum new bie,,,
hehehe,,,,

formnya juga ya mas,,,

BLS:

November 3, 2010 by syabac, 26 weeks 5 days ago
Comment: 7326

syabac's picture

kira2 begini, tinggal customize dikid lagi sesuai kebutuhan. :)

 class Artikel_model extends Model{
   var $_table = 'artikel';
 
   //search di judul, isi, dan ringkasan
   function _search_query($keyword){
      $tokens   = explode(' ', trim($keyword));
 
      foreach($tokens as $token){
         $token = trim($token);
         if(! $token)
            continue;
         $this->db->or_like('judul', $token)
               ->or_like('isi', $token)
               ->or_like('ringkasan', $token);
      }
 
      $this->db->from($this->_table);
      return $this->db;
   }
 
   function search($keyword, $offset, $limit){
      $db   = $this->_search_query($keyword);
      return $db->limit($limit)
            ->offset($offset)
            ->get()->result();
   }
 
   function count_search($keyword){
      $db   = $this->_search_query($keyword);
      return $db->count_all_results();
   }
}
 
class Artikel extends Controller{
   function search(){
      // . . . . . . . . . . . . . . . . . . . .
      //preproses, bla bla bla bla
      // . . . . . . . . . . . . . . . . . . . .
      $results   = $this->artikel_model->search($keyword, $offset, $limit);
      $total      = $this->artikel_model->count_search($keyword);
      // . . . . . . . . 
      $this->load->helper('url');
      $baseurl   = site_url('artikel/search') . '/' ;
 
      $cfg   = array(
         'base_url' => $base_url,
         'per_page' => $limit,
         'total_rows'] => $total
      );
      $this->load->library('pagination', $cfg);
      $pagination = $this->pagination->create_links();
      // . . . . . . . . . . . . . . . . . . . .
      // . . . . . . . . . . . . . . . . . . . .
 
      $data['result']   = $results;
      $data['pagination'] = $pagination;
      $this->load->view('search_result', $data);
   }
}

Premium Drupal Themes by Adaptivethemes