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);
Related
I create a model for fetching data then write code
model..
function get_courses(){
$this->db->from(TABLE_COURSE);
$this->db->where('name !=', '');
$query = $this->db->get();
$result = '';
if($query){
if($query->num_rows() > 0)
$result = $query->result();
}
return $result;
}
Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Quiz extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('Mastermodel','',TRUE);
}
public function index()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $data);
}
public function quiz_of_day()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_of_day";
$this->load->view('layout/dashboard/layout', $data);
}
public function quiz_edit()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_edit";
$this->load->view('layout/dashboard/layout', $data);
}
}
How to call model in controller on constructor because I need write only one time?
To achieve what you want you can do the following...
You need to make the $data array a property of the class
Reference the new $this->data throughout the class
You need to move your call to the model in the constructor.
So what you get is this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Quiz extends CI_Controller {
protected $data = array(); // Old school definition of an array (instead of [])for safety
public function __construct() {
parent::__construct();
$this->load->model('Mastermodel', '', TRUE);
$this->data['courses'] = $this->Mastermodel->get_courses();
}
public function index() {
$this->data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $this->data);
}
public function quiz_of_day() {
$this->data['view_file'] = "content/quiz/quiz_of_day";
$this->load->view('layout/dashboard/layout', $this->data);
}
public function quiz_edit() {
$this->data['view_file'] = "content/quiz/quiz_edit";
$this->load->view('layout/dashboard/layout', $this->data);
}
}
Tim's method would work just find, but I tend to do it more this way.
class Quiz extends CI_Controller {
public $courses; // can also be set to private
public function __construct(){
parent::__construct();
$this->load->model('Mastermodel','',TRUE);
$this->courses = $this->Mastermodel->get_courses();
}
public function index()
{
$data['courses'] = $this->courses;
$data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $data);
}
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 am trying to pass an array to the model from the controller in codeIgnitor I know this question have some answers but I tried all of them and no solution works with me. So this my code:
Controller:
<?php
if(! defined('BASEPATH')) exit('No direct script access allowed');
class SearchController extends CI_Controller{
var $controller = "user";
public function index(){
$this->home();
}
public function home(){
$this->load->view("search_view");
}
public function search(){
if(isset($_POST['submit'])){
$data['type']=$this->input->post('type');
$data['value']=$this->input->post('value');
$this->load->model("SearchModel");
$this->load->SearchModel->getCadre($data);
}
}
}
?>
Model:
<?php
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
$this->getCadre();
}
function getCadre(){
$query = $this->db->get('cadres');
$query = $this->db->where($type,$value);//the argument witch i want to take from array
if($query->num_rows()>0){
$values = result();
return $query->result();
}
else{
return NULL;
}
}
}
?>
Try below code in your model::
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
}
function getCadre($data = array()){
$this->db->where($data);//the argument witch i want to take from array
$query = $this->db->get('cadres');
if($query->num_rows()>0){
// $values = result();
return $query->result();
}
else{
return NULL;
}
}
}
OR you can pass individual data as below and return result...Also try as below
Controller:
if(! defined('BASEPATH')) exit('No direct script access allowed');
class SearchController extends CI_Controller{
var $controller = "user";
public function index(){
$this->home();
}
public function home(){
$this->load->view("search_view");
}
public function search(){
if(isset($_POST['submit'])){
$type=$this->input->post('type');
$value=$this->input->post('value');
$this->load->model("searchmodel");
$data = $this->searchmodel->getCadre($type,$value);
if($data==NULL){
echo 'No records found!!';
}
else
{
//send data to view
$this->load->view('view_name',$data);
}
}
}
}
Model
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
$this->load->database();
}
function getCadre($type,$value){
$this->db->where('type',$type);//the argument witch i want to take from array
$this->db->where('value',$value);
$query = $this->db->get('cadres');
if($query->num_rows()>0){
// $values = result();
return $query->result();
}
else{
return NULL;
}
}
}
?>
When on of the admin clicks an approve button its supposed to add the user_id of the person approving to a field in approval table
This is the button
<?php echo anchor('admin/messages/approve/'.$message->id.'', 'Approve', 'class="btn btn-success"'); ?>
This is the approve function in Controller
public function approve($id) {
$data = array(
'first_approval' => $this->session->userdata('user_id')
);
$this->Message_model->approve($id, $data);
//isset Message
$this->session->set_flashdata('success', 'Your approval was send');
//Redirect
redirect('admin/messages');
}
This is the approve method inside the Model
public function approve($data) {
$this->db->select('*');
$this->db->from('approval');
$this->db->join('messages', 'messages.id = approval.sms_id');
$this->db->where('id', 'sms_id');
$this->db->set('first_approval', $data);
$this->db->update('approval');
}
I have sms_id, first_approval, second_approval and third_approval columns in my approval table but for now I'm just testing if I can put session->user_id in the column 'first_approval'
Change your approve method as following in MODEL
public function approve($id, $data) {
$this->db->where('sms_id', $id);
$this->db->update('approval',$data);
}
Please check below view model and controller
View
<?php echo anchor('admin/messages/approve/'.$message->id.'', 'Approve', 'class="btn btn-success"'); ?>
Controller
class Messages extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('message_model');
}
function approve(){
$id = $this->uri->segment(3); //Get message id
$data = array(
'first_approval' => $this->session->userdata('user_id')
);
$res = $this->Message_model->approve($id, $data);
if($res >0){
//Redirect
redirect('admin/messages');
}else{
//some error accured
}
}
}
Model
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Message_model extends CI_Model {
public function __construct() {
$this->load->database(); //If database not set in autoload
}
public function approve($id, $data) {
$this->db->where('sms_id', $id);
return $this->db->update('approval',$data); //If table update return no of rows update
}
}
Trying to insert a row into my database with CodeIgniter.
My database table is Customer_Orders and the fields are CustomerName and OrderLines. The variables are being submitted correctly.
My Controller is( sales.php ):
function new_blank_order_summary()
{
$data = array(
'OrderLines'=>$this->input->post('orderlines'),
'CustomerName'=>$this->input->post('customer')
);
$this->sales_model->order_summary_insert($data);
$this->load->view('sales/new_blank_order_summary');
}
My Model is( sales_model.php ):
function order_summary_insert($data){
$this->db->insert('Customer_Orders',$data);
}
Whilst the view loads correctly, no data is inserted into the database.
Any ideas as to why not?
Try this in your model:
function order_summary_insert()
$OrderLines=$this->input->post('orderlines');
$CustomerName=$this->input->post('customer');
$data = array(
'OrderLines'=>$OrderLines,
'CustomerName'=>$CustomerName
);
$this->db->insert('Customer_Orders',$data);
}
Try to use controller just to control the view and models always post your values in model. it makes easy to understand.
Your controller will be:
function new_blank_order_summary() {
$this->sales_model->order_summary_insert($data);
$this->load->view('sales/new_blank_order_summary');
}
It will be better for you to write your code like this.
In your Controller Write this code.
function new_blank_order_summary() {
$query = $this->sales_model->order_summary_insert();
if($query) {
$this->load->view('sales/new_blank_order_summary');
} else {
$this->load->view('sales/data_insertion_failed');
}
}
and in your Model
function order_summary_insert() {
$orderLines = trim(xss_clean($this->input->post('orderlines')));
$customerName = trim(xss_clean($this->input->post('customer')));
$data = array(
'OrderLines'=>$orderLines,
'CustomerName'=>$customerName
);
$this->db->insert('Customer_Orders',$data);
return ($this->db->affected_rows() != 1) ? false : true;
}
function saveProfile(){
$firstname = $this->input->post('firstname');
$lastname = $this->input->post('lastname');
$post_data = array('firstname'=> $firstname,'lastname'=>$lastname);
$this->db->insert('posts',$post_data);
return $this->db->insert_id();
}
View
<input type="text" name="name"/>
<input type="text" name="class"/>
Controller
function __construct()
{
parent:: __construct();
$this->load->Model('Model');
}
function index()
{
$this->load->view('view');
}
function user(){
if (isset($_POST['submit'])){
$data = array('name'=>$_POST['name'],
'class'=>$_POST['class']);
$this->Model->insert($data);
}
}
Model
function insert($data)
{
$this->db->insert('table_name',$data);
return true;
}
Based on what I see here, you have used lowercase fieldnames in your $data array, and uppercase fieldnames in your database table.
function order_summary_insert()
$OrderLines=$this->input->post('orderlines');
$CustomerName=$this->input->post('customer');
$data = array(
'OrderLines'=>$OrderLines,
'CustomerName'=>$CustomerName
);
$this->db->insert('Customer_Orders',$data);
}
Check your controller:
function order()
$OrderLines = $this->input->post('orderlines');
$CustomerName = $this->input->post('customer');
$data = array(
'OrderLines' => $OrderLines,
'CustomerName' =>$CustomerName
);
$this->db->insert('Customer_Orders', $data);
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Cnt extends CI_Controller {
public function insert_view()
{
$this->load->view('insert');
}
public function insert_data(){
$name=$this->input->post('emp_name');
$salary=$this->input->post('emp_salary');
$arr=array(
'emp_name'=>$name,
'emp_salary'=>$salary
);
$resp=$this->Model->insert_data('emp1',$arr);
echo "<script>alert('$resp')</script>";
$this->insert_view();
}
}
for more detail visit:
http://wheretodownloadcodeigniter.blogspot.com/2018/04/insert-using-codeigniter.html
Just insert $this->load->database(); in your model:
function order_summary_insert($data){
$this->load->database();
$this->db->insert('Customer_Orders',$data);
}