CodeIgniter Forums
search in two tables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: search in two tables (/thread-53383.html)

Pages: 1 2


search in two tables - El Forum - 07-21-2012

[eluser]anyamanggar[/eluser]
hello, i am from indonesia,
i have a small problem for search something in database,

firs of all, i have one database with two table(info, berita), this is my view

Code:
<? echo form_open('login/cariinfo'); ?>

<h1 class="buatberitabaru">Masukan kata pencarian :</h1> &lt;?php echo form_input(array('name' => 'keyword',  'class' => 'formnewsinput','value' => set_value('keyword'))); ?&gt;
&lt;?php $i = 1 ?&gt;
&lt;?php
if($query != "")
{
?&gt;

<table class="tableberita" width="800" border="0" cellspacing="1" cellpadding="3">
  <tr>
    <td width="29" align="center" valign="middle"><strong>ID</strong></td>
    <td width="383" align="center" valign="middle"><strong>Judul Berita</strong></td>
    <td width="210" align="center" valign="middle"><strong>Kategori</strong></td>
    <td width="149" align="center" valign="middle"><strong>Keterangan</strong></td>
  </tr>
&lt;?php
        foreach($query as $row)
        {
    ?&gt;
  <tr>
    <td align="center" valign="middle">&lt;?php echo $i++ ?&gt;</td>
    <td align="left" valign="middle">&lt;?php echo $row->title; ?&gt;</td>
    <td align="center" valign="middle">&lt;?php echo $row->kategori ?&gt;</td>
    <td align="center" valign="middle">| &lt;?php echo anchor('login/editinfo/'.$row->id,'Edit');?&gt; | &lt;?php echo anchor('login/hapusberita/'.$row->id,'Hapus',array('onClick' => "return confirm('Apakah anda yakin ??')"));?&gt; |</td>
  </tr>
    &lt;?php
        }
    ?&gt;
</table>
    &lt;?php
        }
    ?&gt;
&lt;? echo form_close(); ?&gt;

my controller

Code:
public function cariinfo(){
  $this->load->model('cariinfo_model');
$keyword = $this->input->post('keyword');
  
  if($keyword != "")
  {
   $data['query'] = $this->cariinfo_model->cariinfo($keyword);
   $this->load->view('backend/cariinfo_view',$data);
  }else{
   $data['query'] = "";
   $this->load->view('backend/cariinfo_view',$data);
  }
  }

model
Code:
function cariinfo($keyword) {
  $this->db->like('title',$keyword);
  return $this->db->get('info')->result();
}

i used that code for search one table its work for seacrh word "title" in table info, but when i want to search in two tables, i have nothing show the result

model
function cariinfo($keyword) {
$this->db->like('title',$keyword);
return $this->db->get('info','berita')->result();
}

please help, thank you




search in two tables - El Forum - 07-21-2012

[eluser]solid9[/eluser]
Are you trying to access a join table?
Did you used foreign keys?
If yes is it InnoDB?



search in two tables - El Forum - 07-22-2012

[eluser]anyamanggar[/eluser]
[quote author="solid9" date="1342927790"]Are you trying to access a join table?
Did you used foreign keys?
If yes is it InnoDB?
[/quote]

yes, i want to access a join table, for foreign keys i dont what this mean


search in two tables - El Forum - 07-22-2012

[eluser]skunkbad[/eluser]
This is documented @ http://ellislab.com/codeigniter/user-guide/database/active_record.html :

Code:
$this->db->select('*');
$this->db->from('info');
$this->db->join('berita', 'info.id = berita.id');
$this->db->like('info.title',$keyword);
$query = $this->db->get();



search in two tables - El Forum - 07-22-2012

[eluser]anyamanggar[/eluser]
[quote author="skunkbad" date="1343010735"]This is documented @ http://ellislab.com/codeigniter/user-guide/database/active_record.html :

Code:
$this->db->select('*');
$this->db->from('info');
$this->db->join('berita', 'info.id = berita.id');
$this->db->like('info.title',$keyword);
$query = $this->db->get();
[/quote]

i try your code, the results is didnt show,

Code:
function cariinfo($keyword) {
$this->db->select('*');
$this->db->from('info');
$this->db->join('berita', 'info.id = berita.id');
$this->db->like('info.title',$keyword);
$query = $this->db->get();
}

but if i try like this (search in one table) its work, the result for what i search its show

Code:
function cariinfo($keyword) {
  
  $this->db->like('title',$keyword);
  return $this->db->get('info')->result();
}




search in two tables - El Forum - 07-22-2012

[eluser]anyamanggar[/eluser]
if i used return like this

Code:
function cariinfo($keyword) {
$this->db->select('*');
$this->db->from('info');
$this->db->join('berita', 'info.id = berita.id');
$this->db->like('info.title',$keyword);
return $this->db->get();
}

erorr like this

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: backend/cariinfo_view.php

Line Number: 38



search in two tables - El Forum - 07-22-2012

[eluser]anyamanggar[/eluser]
what i miss ? please help me thank you ..


search in two tables - El Forum - 07-22-2012

[eluser]anyamanggar[/eluser]
i have been update my code to like this

Code:
function cariinfo($keyword) {
$this->db->select('*');
$this->db->from('info');
$this->db->join('berita', 'info.id = berita.id');
$this->db->like('info.title',$keyword);
return $this->db->get()->result();
}

stil didnt show the data what i search..


search in two tables - El Forum - 07-22-2012

[eluser]skunkbad[/eluser]
You're going to need to post the structure of your tables. The proposed code was just an example, as I know nothing about your database.


search in two tables - El Forum - 07-22-2012

[eluser]anyamanggar[/eluser]
[quote author="skunkbad" date="1343017364"]You're going to need to post the structure of your tables. The proposed code was just an example, as I know nothing about your database.[/quote]

this my database, i have been attach files for this post,

http://ollatois.com/files/tnt.sql