Estou com esse problema porém não estou conseguindo resolver. (acontece que esta mostrando cônjuge que não é do titular)
Tenho duas tabelas. users e Conjuge; O ID do conjuge é o mesmo do usuário;
Tabelas:
CREATE TABLE `users` (
`id` int(11) UNSIGNED NOT NULL,
`username` varchar(255) NOT NULL DEFAULT '',
`nome` varchar(255) NOT NULL,
`sobrenome` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL DEFAULT '',
`password` varchar(255) NOT NULL DEFAULT '',
`avatar` varchar(255) DEFAULT 'default.jpg',
`created_at` datetime NOT NULL,
`updated_at` datetime DEFAULT NULL,
`is_admin` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
`is_confirmed` tinyint(1) UNSIGNED NOT NULL DEFAULT '0',
`is_deleted` tinyint(1) UNSIGNED NOT NULL DEFAULT '0'
) ENGINE=innoDB DEFAULT CHARSET=latin1;
CREATE TABLE `conjuge` (
`id` int(11) UNSIGNED NOT NULL,
`nome` varchar(30) NOT NULL DEFAULT '',
`sobrenome` varchar(255) NOT NULL DEFAULT '',
`nascimento` date DEFAULT NULL,
`cpf` varchar(255) NOT NULL DEFAULT '',
`rg` varchar(255) NOT NULL DEFAULT '',
`sex` enum('M','F','U') NOT NULL DEFAULT 'U'
) ENGINE=innoDB DEFAULT CHARSET=latin1;
Model:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function create_user($nome, $sobrenome, $username, $email, $password) {
$data = array(
'nome' => $nome,
'sobrenome' => $sobrenome,
'username' => $username,
'email' => $email,
'password' => $this->hash_password($password),
'created_at' => date('j-m-Y H:i:s'),
);
return $this->db->insert('users', $data);
}
public function resolve_user_login($username, $password) {
$this->db->select('password');
$this->db->from('users');
$this->db->where('username', $username);
$hash = $this->db->get()->row('password');
return $this->verify_password_hash($password, $hash);
}
public function get_user_id_from_username($username) {
$this->db->select('id');
$this->db->from('users');
$this->db->where('username', $username);
return $this->db->get()->row('id');
}
public function get_user($user_id) {
$this->db->from('users');
$this->db->where('id', $user_id);
return $this->db->get()->row();
}
private function hash_password($password) {
return password_hash($password, PASSWORD_BCRYPT);
}
private function verify_password_hash($password, $hash) {
return password_verify($password, $hash);
}
public function get_conjuge(){
$this->db->select('conjuge.id, conjuge.nome, conjuge.sobrenome, conjuge.nascimento, conjuge.cpf, conjuge.rg');
$this->db->from('conjuge');
$this->db->join('users', 'conjuge.id = users.id', 'inner');
$this->db->where('users.id');
$query = $this->db->get();
return $query->result();
}
}
controllers:
public function conjuge(){
$data['conjuge'] = $this->user_model->get_conjuge();
$this->load->view('conjuge', $data);
}
views
<?php if($conjuge): ?>
<div class="ibox-content">
<?php foreach ($conjuge as $row): ?>
<form method="get" class="form-horizontal">
<div class="form-group"><label class="col-sm-2 control-label">Nome</label>
<div class="col-lg-10"><input type="text" disabled="" placeholder="<?php echo strtoupper($row->nome); ?>" class="form-control"></div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group"><label class="col-sm-2 control-label">Sobrenome</label>
<div class="col-lg-10"><input type="text" disabled="" placeholder="<?php echo strtoupper($row->sobrenome); ?>" class="form-control"></div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group"><label class="col-sm-2 control-label">CPF</label>
<div class="col-lg-10"><input type="text" disabled="" placeholder="<?php echo $row->cpf; ?>" class="form-control"></div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group"><label class="col-sm-2 control-label">RG</label>
<div class="col-lg-10"><input type="text" disabled="" placeholder="<?php echo $row->rg; ?>" class="form-control"></div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group"><label class="col-sm-2 control-label">Data de Nascimento</label>
<div class="col-lg-10"><input type="text" disabled="" placeholder="<?php echo date("d/m/Y", strtotime($row->nascimento)); ?>" class="form-control">
<span class="help-block">dd/mm/yyyy</span>
</div>
</div>
</form>
<?php endforeach; ?>
</div>
<?php else: ?>
<div class="ibox-content">
<form method="get" class="form-horizontal">
<div class="form-group">
<div class="col-sm-4 col-sm-offset-2">
<span class="help-block">Você não possui cônjuge cadastrado</span>
<button class="btn btn-primary" type="submit">Adicionar</button>
<?php //$this->load->view('cadastrar'); ?>
</div>
</div>
</form>
</div>
<?php endif; ?>