Welcome Guest, Not a member yet? Register   Sign In
search in two tables
#1

[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

#2

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

[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
#4

[eluser]skunkbad[/eluser]
This is documented @ http://ellislab.com/codeigniter/user-gui...ecord.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();
#5

[eluser]anyamanggar[/eluser]
[quote author="skunkbad" date="1343010735"]This is documented @ http://ellislab.com/codeigniter/user-gui...ecord.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();
}

#6

[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
#7

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

[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..
#9

[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.
#10

[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





Theme © iAndrew 2016 - Forum software by © MyBB