I'm trying to get first name and last name of registered users.
This is my getfirstname , getlastname function in the model
public function getf()
{
$email = $this->input->post('email');
$this->db->where('email', $this->input->post('email'));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
$row = $query->row();
echo $row->fname;
}
}
public function getl()
{
$this->db->where('email', $this->input->post('email'));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
$row = $query->row();
echo $row->lname;
}
}
In My Contoller:
public function members()
{
if($this->session->userdata('is_logged_in'))
{
$data['fname'] = $this->getf();
$data['lname'] = $this->getl();
$this->load->view('members', $data);
}
else
{
redirect('main/restricted');
}
}
Im echoing the fname and lname variables in my view which prints 'Array' and not actual firstname and lastname
echo $fname;
echo $lname;
Your call to model is totally wrong and you are not
following the standard procedure. Try it like this.
Seperate the logics
Controller
public function members()
{
if($this->session->userdata('is_logged_in'))
{
$email = $this->input->post('email');
$result = $this->mymodel->getnames($email);
if($result){
$data['fname'] = $result->first_name;
$data['lname'] = $result->last_name;
$this->load->view('members', $data);
}
}
else
{
redirect('main/restricted');
}
}
And create single function for model not two functions
for two values that could be retrieved with single function
Also return the object instead of echoing
public function getnames($email)
{
$this->db->where('email',$email);
$query = $this->db->get('users');
if($query->num_rows == 1)
{
return $query->row();
}
return false;
}
From what I can see you haven't actually declared the 'fname' and 'lname' variables, unless I've missed it, but if it keeps returning 'Array' then it is because you need to serialise the array with
$variable = json_encode($arrayName);
This will turn the array into a string, hope this helps.
UserModel
class UserModel extends CI_Model
{
public function getItemByEmail($email)
{
// check email
$this->load->helper('email');
if( ! $email OR ! valid_email($email))
return array();
$this->db->where('email', $email);
$this->db->limit(1);
$item = $this->db->get('users')->row();
return $item;
}
}
Controller:
public function members()
{
if( ! $this->session->userdata('is_logged_in'))
redirect('main/restricted');
$this->load->model('UserModel');
$this->load->view('members', array(
'userRow' => $this->UserModel->getItemByEmail($this->input->post('email', TRUE))
));
}
View:
<?= (isset($userRow->fname) ? $userRow->fname : ''); ?>
Related
i'm trying to learn codeigniter and i have problem here. There was an error Undefined variable and Invalid argument supplied for foreach() on view. here is the code :
Model (Model_Users.php) :
class Model_users extends CI_Model {
function __construct() {
parent::__construct();
}
function getFirstNames(){
$query = $this->db->query('SELECT firstname FROM users');
if($query->num_rows() > 0) {
return $query->result();
} else {
return NULL;
}
}
function getUsers(){
$query = $this->db->query('SELECT * FROM users');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return NULL;
}
}
}
Controller (welcome.php) :
class Welcome extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
$this->load->model('model_users');
$data['title'] = 'MVC Cool Title';
$data['page_header']='Intro to MVC Design';
$data['firstnames'] = $this->model_users->getFirstNames();
$data['users'] = $this->model_users->getUsers();
$this->load->view('welcome_message',$data);
}
}
View (Welcome_message.php) :
<div id="container">
<h1><?php echo $page_header ?></h1>
<div id="body">
<?php
foreach ($firstnames as $object) {
echo $object->firstname .'<br/>';
}
echo '<br/><hr/><br/>';
foreach ($users as $object){
echo $object->firstname . '\'s email address is'. $object->email . '<br/>';
}
?>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
Please help me what is wrong with my code ?
First check value return any value or not write this code in controller
use
var_dump($firstnames);
and
var_dump($users);
I think no data return from model
check any data in table
Thanks
You can eliminate the error by using return []; instead of return null; instead of checking for that case in every foreach call, because it appears that your user table might be empty based on the code provided.
Modify your model functions like below
function getFirstNames(){
$query = $this->db->get('users')->row()->firstname;
if($query->num_rows() > 0) {
return $query->result_array();
} else {
return NULL;
}
}
function getUsers(){
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return NULL;
}
}
hope this will help
Hello any one can tell that how to load two function from model class in controller one method. I want to run multiple select quires in one page using codeigniter:
Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$select1 = $this->insertmodel->find($id);
$select2 = $this->insertmodel->detail_list();
$data = array();
$this->load->view('home/property_detail', ['select1'=>$select1], ['select2'=>$select2]);
//$this->load->view('home/property_detail', ['select2'=>$select2]);
}
Model
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list(){
$query1 = $this->db->query("select * from article");
return $query1->result();
}
In Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->find($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
In Model
public function find($id)
{
$query = $this->db->get_where('article', array('id' => $id), 0, 0)->get();
if( $query->num_rows() > 0 )
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
$result = $query1->result_array();
return $result;
}
In View
foreach ($select2 as $item) {
# your foreach lop goes here
}
As well check empty() before passing it to the foreach loop
As an alternative to #Rijin's useful answer newMethod() can make calls to the existing model methods. This might be useful if you don't want to break the interface already created for the model because you are using find($id) and detail_list() in other code.
Model:
public function find($id)
{
$query = $this->db->from('article')->where(['id' => $id])->get();
if($query->num_rows())
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
public function newMethod($id)
{
$result['select1'] = $this->find($id);
if($result['select1'] !== FALSE)
{
$result['select2'] = $this->detail_list();
return $result;
}
return FALSE;
}
Controller:
public function property_detail($id)
{
$this->load->model('insertmodel');
$data = $this->insertmodel->newMethod($id);
$this->load->view('home/property_detail', $data);
}
Model :
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
Controller :
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->newMethod($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
Please Help.. im using CODEIGNITER
I want to print the value of my userlevel and email using echo.. to know if im getting the right data from the database.. then suddenly this error keep bugging me.
beginner in CODEIGNITER need some help here.. thanks a lot!
in my controller:
if ($this->is_validated($rules))
{
$where = array('email' => $this->input->post('txt_email'));
$data['query'] = $this->database_model->select_userlevel('userlevel', $where);
$user = $data['query'];
foreach($user->result() as $row)
{
echo $row->email;
echo $row->userlevel;
}
}
in my model:
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return true;
}
else
{
echo 'no value';
return false;
}
}
Instead of return true you need to return your query in model
Model
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query;// return query here
}
else
{
return false;
}
Controller
$user = $this->database_model->select_userlevel('userlevel', $where);
foreach($user->result() as $row)
{
// your code here
}
UPDATED FOR better solution and proper use of MVC.
Instead of return query you need to return data from your model
MODEL
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query->result(); // return your result
}
else
{
return false;
}
}
CONTROLLER
$user = $this->database_model->select_userlevel('userlevel', $where);
foreach ($user as $row) {
// your code here
}
$user is not an active record object, so it does not have a reference to the result method.
All of this logic belongs in the Model and not the Controller.
Controller
public function doSomething()
{
if( !$this->is_validated($rules) )
return;
$email = $this->input->post('txt_email');
$users = $this->database_model->select_userlevel($email);
// for debugging
var_dump( $users )
die();
}
Model
public function select_userlevel($email)
{
$query = $this->db->select()
->where('email', $email)
->get('userlevel');
return ( $query->num_rows() > 0) ? $query->result() : false;
}
This is my first time doing web programming. I want to make one variable that I can use on some functions, I use public $username; and public $password; and use $this->username and $this->password; but it didn't work. This is my code on controller;
public $can_log ;
public function home(){
$this->load->model("model_get");
$data["results"] = $can_log;
$this->load->view("content_home",$data);
}
public function login(){
$this->load->view("site_header");
$this->load->view("content_login");
$this->load->view("site_footer");
}
public function login_validation(){
$this->load->library('form_validation');
$this->load->view("site_header");
$this->load->view("site_nav");
$this->form_validation->set_rules('username','Username','required|trim|callback_validate_credentials');
$this->form_validation->set_rules('password','Password','required|trim');// use md5 if want to encrpyt this
if($this->form_validation->run()){
redirect('site/home');
} else {
$this->load->view('content_login');
}
}
public function validate_credentials(){
$this->load->model('model_get');
$username = $this->input->post('username');//"user";
$password = $this->input->post('password');//"password";
//I tried both but none of those work
$this->can_log = $this->model_get->can_log_in($username, $password);
if($this->can_log){
return true;
} else {
$this->form_validation->set_message('validate_credentials','Incorrect username/password.');
return false;
}
}
I also tried with public $username and public $password but still can't get it
on my model;
public function can_log_in($username, $password){
$query = $this->db->query("SELECT col1, col2 FROM table1 where id_login = '$username' and id_password = '$password'");
if($query->num_rows() > 0) {
$data = $query->result(); // fetches single row: $query->row();
return $data; //fetches single column: $data->col1;
}
}
so how can I get can_log - that contains col1 and col2 - to other function?
Maybe something like this?
public function with_parameter($parameter)
{
do something with $parameter
}
And then call the function
with_parameter($can_log);
I didn't understood the exact requirements, but try below code if it works for you.
Have followed some CI guidelines which you need to learn.
Controller:
class Controller_name extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("model_get"); // load models in constructor
$this->can_log = "some value"; // is the way to define a variable
}
public function home()
{
$data["results"] = $this->can_log; // is the way to retrieve value
$this->load->view("content_home",$data);
}
public function validate_credentials()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$is_valid = $this->model_get->can_log_in($username, $password);
if($is_valid)
{
return true;
}
else
{
$this->form_validation->set_message('validate_credentials','Incorrect username/password.');
return false;
}
}
}
Model:
class Model_get extends CI_Model
{
public function can_log_in($username, $password)
{
$where_aray = array("id_login" => $username, "id_password" => $password);
$query = $this->db->get_where("table", $where_array);
if($query->num_rows() > 0)
return $query->row();
return false;
}
}
I am trying to create a basic login, no error checking or anything yet, in an effort to get used to codeigniter. Below is my controller class method that I am attempting to pass the result from my model method back in to verify username and password.
public function login()
{
if (isset($_POST['email'])) {
$this->cdata['email'] = $_POST['email'] ;
} else {
$this->cdata['email'] = "";
}
if (isset($_POST['password'])) {
$this->cdata['password'] = $_POST['password'];
} else {
$this->cdata['password'] = "";
}
$this->load->model("dbaccess");
$this->loggedin = $this->dbaccess->check_input($this->cdata['email'], $this->cdata['password']);
if($this->loggedin == TRUE) {
$this->load->view('carerview', $this->cdata);
} else {
$this->cdata['warning'] = "Check failed ! Please try again";
$this->load->view('mainview', $this->cdata);
}
}
The post from my view seems to be working fine. The post is sent back to the main login/index, to the method login (shown above) below shows my model class which is called in my login method in the controller it only has one method so far. check_input()
class Dbaccess extends CI_Model
{
function __construct()
{
parent::__construct();
}
function check_input($email, $password)
{
$this->db->select('email');
$this->db->from('tablename');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return TRUE;
} else {
return FALSE;
}
}
}
When I hit submit on my index page I keep getting the warning no matter what and I can't figure out where the issue is.
Try if it works:
function login()
{
if( $this->input->post( null ) ){ #check if the post array is not blank
$this->load->model("dbaccess");
$this->loggedin = $this->dbaccess->check_input($this->input->post('email'),$this->input->post('password'));
}else{
$this->loggedin = false;
}
if($this->loggedin == TRUE)
{$this->load->view('carerview',$this->cdata);}
else
{$this->cdata['warning']="Check failed ! Please try again";
$this->load->view('mainview',$this->cdata);
}
}
Is your table actually called tablename? Just to try and debug it, try this inside your check_input function and post the results (changing the username/pass if needed):
function check_input($email,$password)
{
var_dump($email);
var_dump($password);
$this->db->select('email');
$this->db->from('tablename');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get();
echo $this->db->last_query();
if (!$query) {
// if query returns null
$msg = $this->db->_error_message();
exit("Error: ".$msg);
}
if($query->num_rows() > 0)
{
return TRUE;
}
else
{
return FALSE;
}
}