php - How do I fetch the data from database and then post it -
this program let login first need have edit profile/info. cannot fetch data database, when click page profile , nothing shown.
model
function login($username, $password) { $this->db->select('username, password'); $this->db->from('tblsec'); $this->db->where('username', $username); $this->db->where('password', md5($password)); $this->db->limit(1); $query = $this->db->get(); if ($query->num_rows() == 1) { return $query->result(); } else { return false; } } public function get_id($username) { $query = $this->db->query("select * tblsec username = '$username'"); $r = $query->result(); return $r; }
view
<?php foreach ($username $user): ?> <div class="form-group"> <label class="col-sm-2 control-label">name</label> <div class="col-sm-5"> <?php echo $user->firstname; ?> <?php echo $user->lastname; ?> </div> </div> <br> <div class="form-group"> <label class="col-sm-2 control-label">password</label> <div class="col-sm-5"> ********** </div> </div> <br> <div class="form-group"> <label class="col-sm-2 control-label">last name</label> <div class="col-sm-5"> <?php echo $user->lastname; ?> </div> </div> <br> <br> <div class="form-group"> <label class="col-sm-2 control-label">email</label> <div class="col-sm-5"> <?php echo $user->email; ?> </div> </div> <br> <?php endforeach; ?> </div> </div>
controller
function __construct() { parent::__construct(); $this->load->model('secretary_model', '', true); $this->load->library('form_validation'); $this->load->helper('date'); } public function index() { if ($this->session->userdata('logged_in')) { $this->header(); $this->load->view('secretary/sec_login_view'); } else { redirect('secretary/sec_login_view'); } } public function profile() { if ($this->session->userdata('logged_in')) { $username = $this->session->userdata('username'); $data['username'] = $this->secretary_model->get_id($username); $this->header2(); $this->load->view('secretary/secretary_profile', $data); } else { redirect('secretary/login', 'refresh'); } }
first of all, make sure have set logged_in
session, ensure user logged in or not.
// after checking username , password $this->session->set_userdata('logged_in', true);
second if username or related data not found return false or appropriate error in model.
/** *@return mixed object|false */ function get_id($username) { $query = $this->db->query("select * tblsec username = '$username'"); return $query->num_rows() > 0 ? $query->result() : false; }
then check in view value in view this:
if( is_array($username) && count($username) > 0 ) { foreach ($username $user): // set data accordingly endforeach; } else { echo "sorry! related data not found!"; }
its approach fetch , debug things accordingly.
Comments
Post a Comment