I'm having issues passing data from my controller to my view. I have a contenteditable div that I am saving the contents of to my database; I want to then display that in the view.
My controller
{
function __construct(){
parent::__construct();
$this->load->database();
$this->load->model('view_data');
$data = array();
}
public function index(){
$data['contactMe'] = $this->input->post('data');
$this->db->update('content', $data);
$results = $this->view_data->loadData();
$data['contactMeData'] = $results;
$this->load->view('pages/home', $data);
}
}
My model
<?php
class View_data extends CI_Model{
function __construct(){
parent::__construct();
}
public function loadData(){
$this->db->where('contactMe');
$query = $this->db->get('content');
return $query->result_array();
}
}
My view
<div class="w-100" ondblclick="makeEditable(this)" onblur="makeReadOnly(this)" contenteditable="true" id="contactMe">
<?php
foreach($contactMeData as $contact){
echo $contact;
}
?>
</div>
<button id="editBtn" type="submit" value="">Save changes
</button>
Every time I reload the page it replaces the data already in the database and I just get a blank div. I'm unable to view the data that was in the database to begin with.
try to add parameter on where
$this->db->where('contactMe',$parameter);
try to add field name on your view
echo $contact['field_name'];
This might be because every time you open the URL it updates the table, so you need a condition to check if POST request was made or not and then update the column
public function index(){
if($this->input->post()){ // check for post request
$data['contactMe'] = $this->input->post('data');
$this->db->update('content', $data); // try writing db queries in model
}
$results = $this->view_data->loadData();
$data['contactMeData'] = $results;
$this->load->view('pages/home', $data);
}
See, if it helps you.
Related
I'm trying to fetch de data from my database and show it in my view.
But I don't seem to get it right. I worked a bit with codeIgniter and I know you can display your data in the view in a way that goes something like this:
{data}{dataname}{/data}
And this would show you every record of the dataname.
This is my model:
public function __construct()
{
$this->load->database();
}
public function Get_Pedras()
{
$query=$this->db->query("SELECT * FROM pedras;");
$row = $query->row_array();
return $row;
}
And this is my controller
public function __construct()
{
parent::__construct();
$this->load->model('Pedras_Model');
$this->load->helper('url_helper');
}
public function index (){
$this->load->view('Main/Formulario_Email');
}
public function GetStone(){
$this->load->library('parser');
$name = $this->Pedras_Model->Get_Pedras();
$data = array(
'title' => 'IdPedra',
'heading' => 'NomePedra'
);
$this->load->view('Main/Formulario_Email', $data);
}
And at least my view:
<div class="col-sm-6 col-sm-offset-1">
{data}
{title}
{/data}
</div>
I think I might be missing something realy important, but I only worked a little bit with CI, and some time ago, could you be helping me out pls? Thanks in advance!
In your Model don't use row_array because you are querying all the available data from your table. use result_array instead.
Like this...
public function Get_Pedras(){
$query=$this->db->query("SELECT * FROM pedras;");
return $query->result_array();
}
Try this one in your Controller...
public function GetStone(){
$this->load->library('parser');
$data['pedras'] = $this->Pedras_Model->Get_Pedras();
$this->load->view('Main/Formulario_Email', $data); //pass data here!
}
then in your view, in order to display the data do this...
foreach($pedras as $p){
echo $p['data']; //rename data with you query_name.
}
I am new to codeigniter. I am stuck at update my form data. i am using one source code from internet but its not working for me. I am done with fetching data from multiple tables on one form.Now i want to update that form.I am sharing my code step by step so you will understand it.
Controller for fetching data in form:
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('user_model');
$this->load->library('session');
}
function login_user(){
$user_login=array(
'USER_NAME'=>$this->input->post('user_email'),
'PASSWORD'=>($this->input->post('user_password')),
'PIN'=>($this->input->post('user_pin'))
);
$data=$this->user_model->login_user($user_login['USER_NAME'],$user_login['PASSWORD'],$user_login['PIN']);
if($data)
{
$this->session->set_userdata('user_id',$data['USER_ID']);
$this->session->set_userdata('user_name',$data['USER_NAME']);
$this->session->set_userdata('employee_id',$data['EMPLOYEE_ID']);
$this->session->set_userdata('nationality',$data['NATIONALITY']);
$this->session->set_userdata('EMPLOYEE_NAME',$data['EMPLOYEE_NAME']);
$this->session->set_userdata('EMPLOYEE_NUMBER',$data['EMPLOYEE_NUMBER']);
$this->session->set_userdata('DATE_OF_BIRTH',$data['DATE_OF_BIRTH']);
$this->session->set_userdata('JOINING_DATE',$data['JOINING_DATE']);
$this->session->set_userdata('CURRENT_LOCATION',$data['CURRENT_LOCATION']);
$this->session->set_userdata('SEX',$data['SEX']);
$this->session->set_userdata('ADDRESS',$data['ADDRESS']);
$this->session->set_userdata('COMPANY_EMPLOYEE_NUMBER',$data['COMPANY_EMPLOYEE_NUMBER']);
$this->session->set_userdata('PHONE',$data['PHONE']);
$this->session->set_userdata('MOBILE',$data['MOBILE']);
$this->session->set_userdata('EMAIL_ADDRESS',$data['EMAIL_ADDRESS']);
$this->session->set_userdata('ALTERNATE_EMAIL',$data['ALTERNATE_EMAIL']);
$this->session->set_userdata('WORKING_HOURS',$data['WORKING_HOURS']);
$this->session->set_userdata('NOK',$data['NOK']);
$this->session->set_userdata('NOK_CONTACT_NUMBER',$data['NOK_CONTACT_NUMBER']);
$this->session->set_userdata('EMPLOYMENT_STATUS_ID',$data['EMPLOYMENT_STATUS_ID']);
$this->session->set_userdata('DESIGNATION_NAME',$data['DESIGNATION_NAME']);
$this->session->set_userdata('CURRENT_ORG_ID',$data['CURRENT_ORG_ID']);
$this->session->set_userdata('COS_NUMBER',$data['COS_NUMBER']);
$this->session->set_userdata('VISA_START_DATE',$data['VISA_START_DATE']);
$this->session->set_userdata('VISA_END_DATE',$data['VISA_END_DATE']);
$this->session->set_userdata('DESCRIPTION',$data['DESCRIPTION']);
$this->session->set_userdata('SKILLS',$data['SKILLS']);
$this->session->set_userdata('YR_OF_EXPERIENCE',$data['YR_OF_EXPERIENCE']);
$this->load->view('layouts/index.php');
}
else{
$this->session->set_flashdata('error_msg', 'Error occured,Try again.');
$this->load->view('layouts/login.php');
}
}
model for fetching data:
class User_model extends CI_model{
public function login_user($email,$pass,$pin){
$this->db->select('*');
$this->db->from('app_users');
$this->db->where('USER_NAME',$email);
$this->db->where('PASSWORD',$pass);
$this->db->where('PIN',$pin);
$this->db->join('employee_details', 'app_users.EMPLOYEE_ID = employee_details.EMPLOYEE_ID', 'LEFT');
$this->db->join('employee_master', 'app_users.EMPLOYEE_ID = employee_master.EMPLOYEE_ID', 'LEFT');
$this->db->join('designation_master', 'employee_details.DESIGNATION_ID = designation_master.DESIGNATION_ID', 'LEFT');
$this->db->join('employee_visa_details', 'employee_details.EMPLOYEE_ID = employee_visa_details.EMPLOYEE_ID', 'LEFT');
$this->db->join('visa_status_master', 'employee_visa_details.VISA_STATUS_ID = visa_status_master.VISA_STATUS_ID', 'LEFT');
$this->db->join('employment_status_master', 'employee_details.EMPLOYMENT_STATUS_ID = employment_status_master.EMPLOYMENT_STATUS_ID', 'LEFT');
$this->db->join('employee_skills', 'employee_details.EMPLOYEE_ID = employee_skills.EMPLOYEE_SKILLS_ID', 'LEFT');
if($query=$this->db->get())
{
return $query->row_array();
}
else{
return false;
}
}
}
Above files are working fine. i can see data of particular user in form view.
Now i want to update that data so i write below code:
Controller:
class Personal_details extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('user_model');
$this->load->model('personal_details_model');
$this->load->library('session');
}
public function update_personal_details(){
$products = new personal_details_model;
$products->update_personal_details($id);
redirect('/responsibilities/personal_details/');
}
}
model for the same:
class Personal_details_model extends CI_model{
public function update_personal_details($id)
{
$data=array(
'EMPLOYEE_NAME' => $this->input->post('emp_name'),
'DATE_OF_BIRTH'=> $this->input->post('dob'),
'JOINING_DATE' => $this->input->post('joining_date'),
);
if($id==0){
return $this->db->insert('employee_master',$data);
}else{
$this->db->where('EMPLOYEE_ID',$id);
return $this->db->update('employee_master',$data);
}
}
}
I dont understand how to write code to update above all tables.
My form view is like this:
<form action="<?php echo site_url('personal_details/update_personal_details');?>" method="post" class="form-horizontal" >
<button type="submit" class="btn btn-info">Edit</button>
<button type="submit" class="btn btn-danger">Cancel</button>
I am getting this error on click edit button:
error on update
First you have to get form data in Personal_details controller then you will pass that data to the Personal_details_model. Second no need to create new object for your model in update_personal_details() function you already loaded personal_details_model in your constructor.
I am a beginner in the CodeIgniter. Working on one small basic project, while I am working on the List view I get an error " Trying to get property of non-object.
Please help me!
Here the screen shots.
Error
My code
Here is my view :
<ul id="actions">
<h4>List Actions</h4>
<li> Add Task</li>
<li> Edit List</li>
<li><a onclick="return confirm('Are you sure?')" href="<?php echo base_url();?>lists/delete/<?php echo $list_detail->id;?>"> Delete List</a></li></ul>
<h1><?php echo $list_detail->list_name; ?></h1>
Created on : <strong><?php echo date("n-j-Y",strtotime($list_detail->create_date)); ?></strong>
<br/><br/>
<div style="max-width:500px;"><?php echo $list_detail->list_body; ?></div>
<!-- <?php //echo print_r($query); ?>
-->
<br/><br/>
Here is Controller
public function show($id){
$data['list_detail'] = $this->List_model->get_lists($id);
$data['main_content']='lists/show';
$this->load->view('layout/main',$data);
}
Here is model
public function get_list($id){
$query=$this->db->get('lists');
$this->db->where('id', $id);
return $query->row();
}
This error is generating because you are trying to access an array in class object style.
Ex:
$data['list_details'] = 'some value';
and accessing it like:
$data->list_details; // it will generate the error "Trying to get property of non object"
After viewing your code. I think you have written wrong in your model function
Your model function must be like below
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Also in your view before you print the value please check it with the condition of !empty($list_details). so if value is not there still it will not throw any error.
I hope this will help you.
From your controller you are calling the model function as get_lists().
But there is no function as get_lists in model. U need to change the function name in controller to get_list()
When your using $query->row() on your model function then on controller
Model Function
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Controller
public function show($id){
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data->id;
$data['create_date'] = $list_data->create_date;
$data['list_body'] = $list_data->list_body;
$this->load->view('someview', $data);
}
When your using $query->row_array(); on your model function then on controller
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row_array();
}
Controller
public function show($id) {
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data['id'];
$data['create_date'] = $list_data['create_date'];
$data['list_body'] = $list_data['list_body'];
$this->load->view('someview', $data);
}
On view then you can access
<?php echo $id;?>
<?php echo $create_date;?>
<?php echo $list_body;?>
Update for multiple data
public function get_list() {
$query = $this->db->get('lists');
return $query->result();
}
Controller
$results = $this->model_name->get_list();
// Or
//$results[] = $this->model_name->get_list();
$data['list_detail'] = $results;
$this->load->view('someview', $data);
View
<?php foreach ($list_detail as $list) {?>
<?php echo $list->id;?>
<?php echo $list->create_date;?>
<?php echo $list->list_body;?>
<?php }?>
I am a new codeigniter developer and have encountered an error. I have stored some value in database and want to display it in view page. Help me please.
Controller
function get_user_value() {
$data['query'] = $this->user_m->user_data_set($query);
$this->load->view('user_settings', $data); }
Model
public function user_details()
{
// $query = $this->db->select('*')->from('user')->get();
$query = $this->db->get('user');
return $query->$result();
}
Try this:
Note call first the model to your controller
add first this function _construct()
function __construct() {
parent::__construct();
$this->load->model("model_file_name", "model_name_alias");
//Example $this->load->model("user_model","user");
//the "user_model" is the file name in your model folder
}
function get_user_value() {
$data['query'] = $this->model_name_alias->user_details();
$this->load->view('user_settings', $data);
}
Basically all I want to do is from my view page which is displaying a specific article then having a delete link to a page with a field and a button for deleting that article with the id displayed in that field.
I have had a go at this for the last couple of days trying to put the id in the URL link i.e "delete?id=20" and trying to access it with a $_GET and then I tried "delete/20" and URI segments.
Then I attempted at using sessions etc but i am not sure which is best as I haven't got any of them working.
I have decided to show my untouched code and start from scratch here is my code:
view.php
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo '<p>'.$news_item['text'].'</p>';
?><br><br>
<a href="http://website.com/CodeIgniter/index.php/news">
Go to latest news</a>
Delete<br>
delete.php
<h2>Delete a news item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/delete') ?>
<form>
<label for="delete">Article Number</label><br>
<input name="id" class="resizedTitlebox" value="id" /><br>
<br>
<input type="submit" name="submit" value="Delete news item" /></form>
news.php (controller)
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function delete() {
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Delete news item';
$this->form_validation->set_rules('id', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/delete');
$this->load->view('templates/footer');
}
else
{
$data['id'] = $this->news_model->delete('id');
$this->load->view('news/success');
}
}
}
news_model.php (model)
<?php
class News_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_news($slug = FALSE){
$this->load->helper('text');
if ($slug === FALSE){
$this->db->order_by('id', 'desc');
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news(){
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'id' => $this->input->post('id'),
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text'));
return $this->db->insert('news', $data);
}
public function delete ($id) {
$this->db->where('id',$this->input->post('id'));
$this->db->delete('news');
}
}
routes.php (config)
$route['news/(:any)'] = 'news/view/$1';
$route['news/delete'] = 'news/delete';
$route['news'] = 'news';
$route['default_controller'] = 'news';
$route['404_override'] = '';
Thanks in advance for any help given!
#jeroen answer
"You are not passing any value in the delete link; you should add an ID either to the path or the query string" - I assume you mean like
Delete
So using article_id. Then I can define article_id in the delete controller? I am not sure how this can be done.
answer:
$this->input->get(article_id)
Your delete procedure seems strange and has some errors:
You are no specifying a method in your form, so that will default to GET but in your delete function you use $this->input->post('id'). You should change your form to <form action="" method="POST">;
You are sending a variable to your delete function that you don't use: $id. This is a string in your controller although the name in the model seems to suggest an ID. But you are not using it anyway...
You are expecting a return value from the delete function when you call it in your controller but you don't return anything from your function.
You are not passing any value in the delete link; you should add an ID either to the path or the query string, but be careful not to use the same name $id if you use the same controller as the form will validate without the confirmation;
You are not getting any value for the ID in the delete controller and passing it to the view to fill the value of your ID field.
By the way, there is no input type id but that should not cause any problems as it defaults to text.
I think their is no need of delete view page.You can directly delete news item from your news list page
Delete<br>
instead of this line use this
<a href = "<?php echo site_url("news/delete/".$news_item['id']); ?>Delete</a>
And in your controller
public function delete($id){
$this->news_model->delete($id);
$this->load->view('news/success');
}
And in your model use this
function Delete($id){
$this->db->where('id', $id);
if($this->db->delete("news"))
return true;
else
return false;
}