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);
}
Related
i have problem to show my query result in my view, i user codeigniter with mvc structure, in model my code its look like this,
<?php
class Model_Kabalitbang extends CI_Model{
public function getPaguAnggaran(){
$query = "SELECT pagu_anggaran_program_modalutama FROM program_modal_utama WHERE id_program_modalutama = '3'
";
return $this->db->query($query);
}
}
and in my controller i call my model like this
<?php
class Kabalitbang extends CI_Controller{
function __construct(){
parent::__construct();
if($this->session->userdata('logged_in') !== TRUE){
redirect('login');
}
$this->load->model('Model_Kabalitbang');
}
function index(){
//Allowing akses to kabalitbang only
if($this->session->userdata('level')==='2'){
// Jumlah PAGU
$pagu = $this->Model_Kabalitbang->getPaguAnggaran();
$paguanggaran = $pagu->num_rows();
$data = array(
'jml_pagu' => $paguanggaran,
);
$this->load->view('kabalitbang/dashboard_view', $data);
}else{
echo "Access Denied";
}
}
}
but when i call in view <?=$jml_pagu ?> this show just 1, but the value form field in my query is 24392
how to make my code run?
Your query is already return a row containing a number, so you just need to display the row, not counting the row again :
$pagu = $this->Model_Kabalitbang->getPaguAnggaran();
$paguanggaran = $pagu->row_array();
$data = array(
'jml_pagu' => $paguanggaran['pagu_anggaran_program_modalutama'],
);
use this...
$this->db->where('id_program_modalutama ',3);
$result['data']=$this->db->get('Table Name')->result();
$this->load->view('kabalitbang/dashboard_view', $result);
now use loop on $data in your view...
Example :-
foreach($data as $allData)
{
print_r($allData);
}
I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>
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);
Im trying to allow a user to edit their own details. Here is my code:
Controller:
public function myaccount() {
if($this->session->userdata('logged_in')) {
$session_data = $this->session->userdata('logged_in');
$data['id'] = $session_data['id'];
$this->load->model('myaccount_model');
$this->myaccount_model->get_details($data);
$this->load->view('head');
$this->load->view('myaccount', $data);
$this->load->view('footer');
} else {
redirect('login', 'refresh');
}
}
Model:
<?php
class Myaccount_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_details($data) {
$query = $this->db->get_where('users', array('id' => $id));
return $query->row_array();
}
}
It comes up with the error :Undefined variable $id on my account_model. The session is created at the controller as:
$data['id'] = $session_data['id'];
So how do i pass the data from the controller, to the model so i can query the database and select the user that matches the session id? Thanks
Use $data['id'] because you are setting the value in your controller
public function get_details($data)
{
$query = $this->db->get_where('users', array('id' => $data['id']));
return $query->row_array();
}
In order to echo out user data in your view I think you can do something like this:
$userData = $this->myaccount_model->get_details($data);
$this->load->view('myaccount', $userData);
there is no variable at your model $id inside get details function.
try to change your getdetails function like this
public function get_details($data)
{
$query = $this->db->get_where('users', array('id' => $data['id']));
return $query->row_array();
}
Or you can pass only id instead of $data like this from your controller
$this->myaccount_model->get_details($session_data['id']);
Now you can change your model like this
public function get_details($id)
{
$query = $this->db->get_where('users', array('id' => $id));
return $query->row_array();
}
I am building my comment module and trying to get the active records to display on view. Seems simple however I have a lot going on and using a separate model and controller then what the view is being generated from.
So I have a profile page ( where I'm trying to get the results to display )
Controller:
public function profile()
{
$this->load->helper('url'); //Include this line
$this->load->helper('date');
$this->load->library('session');
$this->load->library('form_validation');
$session_id = $this->session->userdata('id'); //Ensure that this session is valid
//TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
$user_get = $this->uri->segment(3); // Modify this line
// LOAD THE CORRECT USER
$this->load->model('account_model');
$user = $this->account_model->user($user_get); //(suggestion) you want to pass the id here to filter your record
$data['user'] = $user;
$data['session_id'] = $session_id;
if($user_get != $user['id'] || !$user_get)
{
$data['main_content'] = 'account/notfound';
$this->load->view('includes/templates/profile_template', $data);
}
else
{
if($user_get == $session_id) //Modify this line also
{
$data['profile_icon'] = 'edit';
}
else
{
$data['profile_icon'] = 'profile';
}
$sharpie = $this->sharpie($user);
$data['sharpie'] = $sharpie;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/profile_template', $data);
}
}
Now I have a new controller for my comments I'm trying to display:
public function airwave() {
$this->load->helper('date');
$this->load->library('session');
$airwave_get = $this->uri->segment(3);
$this->load->model('community_model');
$airwave = $this->coummunity_model->airwave($airwave_get);
$data['airwave'] = $airwave;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/main_page_template', $data);
}
with this model:
public function airwave($id=null)
{
if(is_null($id)) {
$session = $this->session->userdata('is_logged_in');
$commenter_id = $this->session->userdata('id');
}else{
$commenter_id = intval($id);
}
$query = $this->db->query("SELECT * FROM airwaves_comments WHERE from_id=$commenter_id");
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
//above returns the single row you need to the controller as an array.
//to the $data['user'] variable.
}
}
and trying to display it on this view ( which is generated by the profile function )
<div class="profile_airwave_comment_text">
<?php echo $airwave['comment'];?>
</div>
I just can't seem to get it to pass the array variable I've created properly to that view?
thanks in advance.
The first glaring thing I see wrong is in you model's airwave function:
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
You are assuming that there will only be one result in your query, so if there are more or less than one, your $data array will bever be set.
Also, even then you are only returning the first element in the array. I am not seeing anywhere in your question that you want to return only one.
Finally, in your view, if you now return the full array, and not only one element, you will have to do a foreach statement.
EDIT: please see suggested solution.
In your model, don't worry about the check for the amount of data in the result array. This will come later. Simply do:
return $query->result_array();
Now, keep your controller as is, but adapt your view:
<div class="profile_airwave_comment_text">
<?php
if (isset($airwave) && is_array($airwave) && !empty($airwave))
{
foreach ($airwave as $wave)
{
echo $wave['comment'];
}
}
else
{
echo 'No comments found...';
}
?>
</div>
This should work, and if not, at least let you know that no comments were fond, and thus check your database.
HTH!
If your using HMVC, simply call the module from inside the profile view, ie:
echo Modules::run('comments/profileComments', $user->id);
Else:
You would need to create a new method in the loader class, to load in the controller seperate from routing.
If your really after a quick fix until you come up with a better alternative, you could(depending if both controllers live in the same directory) just include the 'comments' controller in your 'profile' controller and try something like this:
{Cons: CI Pagination wont work!}
application/controllers/profile.php
include 'Comments' . EXT;
class Profile extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function index( $user_id ){
$commentsPartialView = call_user_func_array(array(new Comments(), "getProfileComments"), array($user_id));
return $this->load->view($this->template, array(
'view' => 'profiles/_index',
'comments' => $commentsPartialView
));
}
}
-
application/controllers/Comments.php
class Comments extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function getProfileComments( $user_id ){
$user_comments = ''; //pull from DB
//set third param to TRUE for partial view, without main template
return $this->load->view('comments/show', array(
'user_comments' => $user_comments
), TRUE);
}
}