I have a problem with extracting data from the database only for a given record in Code Igniter.
In my application I have domains to which I can add my comments - everything is overwhelmed.
I can add, edit, delete domains and notes.
However, when I step in on whatever domain card appears, all notes and I would like to display only those for the selected domain.
I think the problem is on the side of the SQL query - which must compare two tables: the id from the domains table and the id_rel_domain from the notes table.
However, I have no idea how to embrace it.
I have a linked relationship in the SQL FOREIGN KEY on ALTER TABLE database: notes.id_domain_rel with domains.id.
My code (CodeIgniter)
My controller:
public function get($id = false)
{
$result = $this->notes_model->get($id);
echo '{"records":' . json_encode( $result ) . '}';
}
My models:
public function get( $id = false)
{
if ( $id == false ) {
$q = $this->db->get('notes');
$q = $q->result();
}
else{
$this->db->where('id', $id);
$q = $this->db->get('notes');
$q = $q->row();
}
return $q;
}
The second condition is needed to edit notes - because it gets their ID. The first one, however, is displayed all notes for all domains.
In my opinion, I must add:
$this->db->where('notes.id_domain_rel', $domain.id);
However, I miss the idea of how to do it.
Gets data for all domains, not for one I have a view of.