i have two database. tabel_jawaban and tabel_user.
in tabel_user there is field called "id_user"
i want to move/call "id_user from tabel_user" to "id_user in tabel_jawaban" when registration.
but i failed. anyone can help? there are my code....
model_user :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_user extends CI_Model {
function __construct() {
parent::__construct();
$this->tbl = "tabel_user";
}
function cek_user($username="",$password="") {
$query = $this->db->get_where($this->tbl,array('username' => $username, 'password' => $password));
$query = $query->result_array();
return $query;
}
function ambil_user($username)
{
$query = $this->db->get_where($this->tbl, array('username' => $username));
$query = $query->result_array();
if($query){
return $query[0];
}
}
function ambil_iduser($idUser)
{
$query = $this->db->get_where($this->tbl, array('id_user' => $idUser));
$query = $query->result_array();
if($query){
return $query[0];
}
}
function getAllUser() {
$this->db->from('tabel_user');
return $this->db->get();
}
function deleteUser($id)
{
$this->db->where('id_user', $id);
$this->db->delete('tabel_user');
}
}
?>
Jawaban_model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Jawaban_model extends Ci_Model {
public function Ambil() {
$data = $this->db->query('select * from tabel_jawaban, tabel_user where tabel_jawaban.id_user=tabel_user.id_user');
return $data;
}
public function AmbilNilai2($iduser) {
$data = $this->db->query("select * from tabel_jawaban, tabel_user where tabel_jawaban.id_user=tabel_user.id_user AND tabel_jawaban.id_user='$iduser'");
return $data;
}
public function Simpan($tabel, $data){
$res = $this->db->insert($tabel, $data);
return $res;
}
public function UpdateTotalNilai($id_jawaban,$data)
{
$this->db->where('id_jawaban',$id_jawaban);
$this->db->update('tabel_jawaban',$data);
}
}
Controller :
function daftar(){
$ambil_akun = $this->session->userdata('uname');
$idUser = $this->user_model->AmbilIdUser($ambil_akun);
$data = array(
'id_user' => $idUser,
'tgl_tes' => date("Y-m-d"),
);
$this->jawaban_model->Simpan('tabel_jawaban', $data);
$id=mysql_insert_id();
redirect('dashboard/soaluser/'.$id);
}
There is minor typo in your code. As far as I could notice, your user_model-> method is
function ambil_iduser($idUser)
{
$query = $this->db->get_where($this->tbl, array('id_user' => $idUser));
$query = $query->result_array();
if($query){
return $query[0];
}
Whereas, in your controller, you are calling for different method here,
$idUser = $this->user_model->AmbilIdUser($ambil_akun);
Both the method's name differs ambil_iduser and AmbilIdUser.
The correct controller calling state should be
function daftar(){
$ambil_akun = $this->session->userdata('uname');
$idUser = $this->user_model->ambil_iduser($ambil_akun);
$data = array(
'id_user' => $idUser,
'tgl_tes' => date("Y-m-d"),
...
Related
I have a problem with edit and delete data in CodeIgniter.
I've tried some optional but it wasn't work. can somebody help me?
here I attached controller and model.
View
data bahan baku
kode_bahan_baku = kbb
nama_bahan_baku = hps
edit bahan baku = edit_dbb
delete bahan baku = delete_dbb
Model:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
Class Model_data extends CI_Model{
public function __construct(){
parent::__construct();
}
// Data Bahan Baku
public function set_dbb(){
$data = array(
'bb_code' => $this->input->post('kbb'),
'bb_price' => $this->input->post('hps')
);
return $this->db->insert('data_bahan_baku',$data);
}
public function get_dbb(){
$query = "select * from data_bahan_baku";
$sql = $this->db->query($query);
return $result = $sql->result();
}
public function edit_dbb(){
$data = array(
'bb_code' => $this->input->post('kbb'),
'bb_price' => $this->input->post('hps')
);
$this->db->where('kbb',$id);
$edit = $this->db->update('data_bahan_baku',$data);
return $edit;
}
public function delete_dbb($id){
$data = array(
'bb_code' => $this->input->post('kbb'),
'bb_price' => $this->input->post('hps')
);
$this->db->where('kbb',$id);
return $this->db->delete('data_bahan_baku');
}
Controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');
Class Control_data extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("model_data");
}
// Entry Data
/*Input Data Bahan Baku*/
public function data_bahan_baku(){
$data = array();
$data['title'] = "Data Bahan Baku";
$data['result'] = $this->model_data->get_dbb();
$this->form_validation->set_rules('kbb','Kode Bahan Baku','required');
$this->form_validation->set_rules('hps','Harga Per Satuan','required');
if($this->form_validation->run() == FALSE){
$this->load->view('data/data_bahan_baku',$data);
}else{
$this->model_data->set_dbb();
$this->load->view('data/data_bahan_baku',$data);
}
}
/*Edit Data Bahan Baku*/
public function edit_data_bahan_baku($id){
$this->model_data->edit_dbb($id);
}
/*Delete Data Bahan Baku*/
public function delete_data_bahan_baku($id){
$this->model_data->delete_dbb($id);
redirect('control_data/data_bahan_baku');
}
i want to run multiple model in same controller but its show below error
Severity: Notice
Message: Undefined property: Home::$Company
Filename: controllers/Home.php
Line Number: 23
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent::__construct ();
$this->load->model('Product');
$this->load->model('Company');
//$this->load->model(array('Product','Company'));
}
public function index()
{
$Product = $this->Product->getProduct(10);
if($Product)
{
$data['Product'] = $Product;
}
$Company = $this->Company->getCompany(10);
if($Company)
{
$data['Company'] = $Company;
}
$this->load->view('Home',$data);
}
}
//Company Model
class Company extends CI_Controller {
function getCompany($limit=null,$start=0)
{
$data = array();
$query = $this->db->query('SELECT * FROM user as u,category as c,sub_category as sc WHERE c.Category_Id=u._Category_Id and u._Sub_Category_Id=sc.Sub_Category_Id ORDER by rand() limit '.$start.','.$limit);
$res = $query->result();
return $res;
}
function getOneCompany($id)
{
$this->db->where('User_Id',$id);
$query = $this->db->get('user');
$res = $query->result();
return $res;
}
}
// Product model
class Product extends CI_Controller {
function getProduct($limit=null,$start=0)
{
$data = array();
$query = $this->db->query('SELECT * FROM product as p,user as u,category as c,sub_category as sc WHERE p._User_Id = u.User_Id and c.Category_Id=u._Category_Id and u._Sub_Category_Id=sc.Sub_Category_Id ORDER by rand() limit '.$start.','.$limit);
$res = $query->result();
return $res;
}
function getOneProduct($id)
{
$this->db->where('User_Id',$id);
$query = $this->db->get('user');
$res = $query->result();
return $res;
}
}
Your models start with
class Company extends CI_Controller {
They should be
class Company extends CI_Model {
Try changing
$this->load->model('Product');
$this->load->model('Company');
to
$this->load->model('product');
$this->load->model('company');
notice the lower case model names. Just tried it here and got the same error as you.
$models = array(
'menu_model' => 'mmodel',
'user_model' => 'umodel',
'admin_model' => 'amodel',
);
foreach ($models as $file => $object_name)
{
$this->load->model($file, $object_name);
}
Try giving a name to you model :
$this->load->model('Company', 'Company');
how to use insert_batch to add multiple data in database by using for loop please help me as i am very new to codeigniter.
My controller
class Student extends CI_Controller {
public function _construct()
{
parent::_construct();
//call model
$this->load->model("StudentModel","m");
}
function index()
{
$this->load->view("index");
}
function savedata()
{
$data = array(
array(
'studentname' => 'Reddy' ,
'gender' => 'Male' ,
'phone' => '456879'
),
array(
'studentname' => 'Yalla' ,
'gender' => 'Female' ,
'phone' => '12345678'
)
);
//mean that insert into database table name tblstudent
$this->db->insert_batch('tblstudent',$data);
//mean that when insert already it will go to page index
redirect("Student/index");
}
function edit($id)
{
$row=$this->m->getonerow($id);
$data['r']=$row;
$this->load->view('edit',$data);
}
function update($id)
{
$id=$this->input->post('id');
$data=array(
'studentname' => $this->input->post('studentname'),
'gender' => $this->input->post('gender'),
'phone' => $this->input->post('phone')
);
$this->db->where('id',$id);
$this->db->update('tblstudent',$data);
redirect("Student/index");
}
function delete($id)
{
$id=$this->db->where('id',$id);
$this->db->delete('tblstudent');
redirect("Student/index");
}
}
Model
class StudentModel extends CI_Model{
function _construct()
{
parent::_construct();
}
function gettable()
{
$query=$this->db->get('tblstudent');
return $query->result();
}
function getonerow($id)
{
$this->db->where('id',$id);
$query = $this->db->get('tblstudent');
return $query->row();
}
}
First of all you don't have to add direct access to database in controller. For all database access code you have to create model.
in your above code you added database access code in direct controller,which is logically wrong as per MVC architecture.
Below is code as per MVC and all database access code resides in Model.
Your main question for batch insert is in function insert()
of model.Please go through it.
<?php
class Student extends CI_Controller {
public function _construct()
{
parent::_construct();
//call model
$this->load->model("StudentModel","m");
}
function index()
{
$this->load->view("index");
}
function savedata()
{
$stdarray = array();
$stdarray[] = array('studentname' => 'Reddy' ,'gender' => 'Male' ,'phone' => '456879');
$stdarray[] = array('studentname' => 'Yalla' ,'gender' => 'Female' ,'phone' => '12345678');
//mean that insert into database table name tblstudent
$this->db->insert_batch('tblstudent',$data);
$this->m->insert($stdarray);
//mean that when insert already it will go to page index
redirect("Student/index");
}
function edit($id)
{
$row=$this->m->getonerow($id);
$data['r']=$row;
$this->load->view('edit',$data);
}
function update($id)
{
$updateArray = array();
$updateArray['studentname'] = $this->input->post('studentname');
$updateArray['gender'] = $this->input->post('gender');
$updateArray['phone'] = $this->input->post('phone');
$whereArray = array();
$whereArray['id'] = $id;
$this->m->updateRow($updateArray,$whereArray);
redirect("Student/index");
}
function delete($id)
{
$whereArray = array();
$whereArray['id'] = $id;
$this->m->deleteRow($whereArray);
redirect("Student/index");
}
}
?>
Model:
class StudentModel extends CI_Model{
function _construct()
{
parent::_construct();
}
function gettable()
{
$query=$this->db->get('tblstudent');
return $query->result();
}
function getonerow($id)
{
$this->db->where('id',$id);
$query = $this->db->get('tblstudent');
return $query->row();
}
function insert($insrtArray)
{
$success = $this->db->insert_batch('tblstudent', $insrtArray);
}
function updateRow($updateArray,$whereArray)
{
if(!empty($whereArray))
{
foreach ($whereArray as $key => $value) {
$this->db->where($key,$value);
}
}
$this->db->update('tblstudent',$updateArray);
}
function deleteRow($whereArray)
{
if(!empty($whereArray))
{
foreach ($whereArray as $key => $value) {
$this->db->where($key,$value);
}
}
$this->db->delete('tblstudent');
}
}
?>
Important: morever you have to use escape function for all inputa which are direct send to database for maintain security from SQL injection.
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);