I am trying to get to data from database details from model to controller i was not get
it's saying Undefined variable: result can any one know how to get data from model to controller?
controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('login_model');
$this->load->helper(array('form', 'url'));
$this->load->library('session');
}
public function setting()
{
$this->load->database();
session_start();
$userss="";
if($_SESSION["registerid"])
{
$userss = $_SESSION["registerid"];
}
$this->load->model('setting_model');
$this->setting_model->getsetting($userss);
$data12345 = array(
'registerid' => $result[0]->registerid,
'username' => $result[1]->username,
'email' => $result[2]->email,
'password' => $result[3]->password,
'companyname' => $result[0]->companyname,
'phonenumber' => $result[0]->phonenumber ,
'address' => $result[0]->address,
'postalcode' => $result[0]->postalcode,
'descriptionofcompany' =>$result[0]->descriptionofcompany,
'creditcardinformation' =>$result[0]->creditcardinformation,
'title'=>'Setting Page'
);
//passing page title
$this->load->helper('url'); // passing base url
$this->load->view('setting',$data12345); // passing setting.php view
}
}
setting_model.php
<?php
class setting_model extends CI_Model{
function __construct() {
parent::__construct();
}
public function getsetting($userss) {
$this->load->database();
$condition = "username =" . "'" . $userss . "'";
$this->db->select('*');
$this->db->from('register');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
// Read data from database to show data in admin page
}//end class
?>
Looks like you forgot to assign the query result to the $result variable. Try this $result = $this->setting_model->getsetting($userss); in the controller.
Related
i'm using controller to call update function from model, but when i try to update for column name, that's no working.
---- Controller ----
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class editprofile extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user');
}
public function index()
{
$this->form_validation->set_rules('email', 'email', 'required|valid_email');
$this->form_validation->set_rules('namalengkap', 'namalengkap', 'required');
$this->form_validation->set_rules('password', 'PASSWORD', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('v_profile');
} else {
$row = $this->session->userdata('id');
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'phone' => $this->input->post('phone'),
'address' => $this->input->post('address'),
'kecamatan' => $this->input->post('kecamatan'),
'kelurahan' => $this->input->post('kelurahan'),
'rt' => $this->input->post('rt'),
'rw' => $this->input->post('rw'),
'zip code' => $this->input->post('zip code'),
'city' => $this->input->post('city'),
'province' => $this->input->post('province'),
);
$result = $this->user->update($data, $row);
if ($result > 0) {
$this->updateProfil();
$this->session->set_flashdata('msg', show_succ_msg('Data Profile Berhasil diubah, silakan lakukan login ulang!'));
redirect('c_login/profile');
} else {
$this->session->set_flashdata('msg', show_err_msg('Data Profile Gagal diubah'));
redirect('c_login/profile');
}
}
}
}
----- model -----
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model
{
private $uid = 'id';
private $tabel = 'users';
public function __construct()
{
parent::__construct();
}
public function login($email, $password)
{
$this->db->where('email', $email);
$this->db->where('password', $password);
return $this->db->get($this->tabel);
}
function update($data, $id)
{
$this->db->where($this->uid, $id);
$this->db->update($this->tabel, $data);
}
public function get_by_cookie($kue)
{
$this->db->where('cookie', $kue);
return $this->db->get($this->tabel);
}
}
----- Core / MY_Controller -----
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user');
}
public function updateProfil()
{
if ($this->userdata != '') {
$data = $this->user->select($this->userdata->email);
$this->session->set_userdata('userdata', $data);
$this->userdata = $this->session->userdata('userdata');
}
}
}
In your update function you have not set any values. As set method required array values you should give if. Good practice is using set before update and finally update of table. Follow the following codes for updating your users table.
function update($data, $id)
{
$this->db->set($data);
$this->db->where($this->uid, $id);
$this->db->update($this->tabel);
}
use this code. I hope it will work well.
in your controller
$result = $this->user->update($data, $row);
variable data is array
so in your model:
function update($data=array(), $id){
$this->db->where($this->uid, $id);
$this->db->update($this->tabel, $data);
}
I get the error :
Fatal error: Call to undefined method select::form() in public_html/opunletter/application/controllers/Welcome.php on line 44.
when i run this. what might be the reason.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
//load the database
$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['h']=$this->select->select();
$data['a']=$this->select->archieve();
$data['n']=$this->select->latest();
$id=$this->input->get("id");
$data['f']=$this->select->load($id);
//return the data in view
$this->load->view('welcome_message', $data);
}
public function login()
{
$this->load->view('userlogin');
}
public function submit()
{
$data = array(
'from' => $this->input->post('from'),
'to' => $this->input->post('to'),
'title' => $this->input->post('title'),
'openletter' => $this->input->post('openletter')
);
$this->load->model('select');
//Transfering data to Model
$this->select->form($data);
}
} ?>
My model
<?php
class Select extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//we will use the select function
public function Select()
{
//data is retrive from this query
$query = $this->db->get('country');
return $query;
}
public function archieve()
{
//data is retrive from this query
$query = $this->db->query("SELECT * FROM country WHERE archieve=1;");
return $query;
}
public function latest()
{
//data is retrive from this query
$query = $this->db->query("SELECT * FROM country WHERE latest=1;");
return $query;
}
public function load($id)
{
//data is retrive from this query
$query = $this->db->select('*')
->from('country')
->where('open_id', $id)
->get();
return $query;
}
public function form($data){
// Inserting in Table(students) of Database(college)
$query = $this->db->insert('user_openletter', $data);
return $query;
}
}
?>
Hi i am not sure why are my codes not working.
This is the Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
public function __construct(){
parent::__construct();
if (! $this->session->userdata('is_logged_in')){
redirect('main/restricted');
} else {
$privilege = $this->session->userdata('privilege');
if ($privilege == 'member'){
redirect('main/restricted');
}
}
}
public function index() {
$this->load->model("model_books");
$data2 = $this->model_books->select_book();
$data = array(
'title' => 'Admin Page'
);
$this->load->view("header", $data);
$this->load->view("admin", $data2);
$this->load->view("footer");
}
This is the Model:
<?php
class Model_books extends CI_Model {
public function select_book(){
$query = $this->db->get('books');
if ($query){
return $query->result();
} else {
return false;
}
}
}
This is the View:
<?php
echo $data2->content;
?>
I got around 10 books inside the database however the books in the database are not showing up.
Try this
public function index() {
$this->load->model("model_books");
$data = array(
'title' => 'Admin Page',
'books' => $this->model_books->select_book()
);
$this->load->view("header", $data);
$this->load->view("admin", $data);
$this->load->view("footer");
}
In view
<?php
print_r($books);
?>
try the following in constructor function in model
$db = $this->load->database('default', TRUE);
The Session library is autoloaded
My model:
class Login_model extends CI_Model{
function __construct(){
parent::__construct();
}
public function validate(){
$username = $this->security->xss_clean($this->input->post('username'));
$password = $this->security->xss_clean($this->input->post('password'));
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows == 1)
{
$row = $query->row();
$data = array(
'id' => $row->id,
'first_name' => $row->first_name,
'last_name' => $row->last_name,
'username' => $row->username,
'validated' => true
);
$this->session->set_userdata($data); // ### line 28 ###
return true;
}
return false;
}
}
Gives this error:
Fatal error: Call to undefined method Session::set_userdata() in
/var/www/codeIgniterTest/_application/models/login_model.php on line
28
Have you loaded the Session library?
class Login_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->library('session');
}
Try debugging and check all the included files by.
$included_files = get_included_files();
foreach ($included_files as $filename) {
echo "$filename\n";
}
I found the problem. I created my own Session class, but forgot to extend the CI_Session.
Thanks for the help!
Im trying add data to the database useing CodeIgniter framework. DB method return true, but I have no new data in database.
There is my controller:
class Facebook extends CI_Controller
{
public function index()
{
if($this->input->is_ajax_request())
{
$this->load->model('user_model','user');
$data = array();
$data['u_ip'] = $this->input->ip_address();
$this->user->add_user($data);
}
}
}
And there is my model:
class User_model extends CI_Model
{
public function add_user($data)
{
$this->db->insert('users',$data);
//$res = $this->db->insert('users',$data); (true)
}
}
Update:
There is my database:
id_users (int 255) AI, key
u_ip (varchar 100)
Also, make sure your $data looks like this:
$data = array(
"u_ip" => $this->input->ip_address(),
);
$this->insert("data", $data);
Or if coming from another file, do this:
$this->insert("users", $data[0];)
Remember to load the dataabse:
$this->load->database();
This will go within add_user($data) eg.
class User_model extends CI_Model
{
public function add_user($data)
{
$this->load->database();
$this->db->insert('users',$data);
//$res = $this->db->insert('users',$data); (true)
}
}
However, you can have these automatically, if you go to autoload.php, you can do this:
$autoload['libraries'] = array('database');
And for your model:
$autoload['model'] = array('logic');
You are not mentioning any column names in your query. Try the below code -
class User_model extends CI_Model{
public $db;
public function __construct(){
parent::__construct();
$this->db = $this->load->database('default',true);
}
public function add($data)
{
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
}
$this->input->is_ajax_request()
is this posted via a form ? if so it should be $this->input->post('is_ajax_request');
is_ajax_request being the name of the input your posting,
If its another function within the controller you need to do $this->is_ajax_request();