call method in controller with parameters - php

I need to pass parameters to the controller, In the below I have mention a brief explanation with the code,
Controller
class Site2 extends CI_Controller
{
function index()
{
$this->load->helper('url');
$this->home();
}
public function home()
{
$this->load->model('get_company_model');
$this->load->model('bank_account_model');
$data['results'] = $this->get_company_model->get_All();
$this->load->view('view_header');
$this->load->view('view_nav',$data);
$this->load->view('view_content');
$this->load->view('view_footer');
}
in the results array there are id and name,I can get these values in my view, then I need to pass id as parameters and call the function in controller.
view
<?php
foreach($results as $row){
echo $row->id;
//I need to pass this particular id to the function
}?>

You should do it like this:
1) If in same controller:
function home(){
$this->load->model('get_company_model');
$this->load->model('bank_account_model');
$data['results'] = $this->get_company_model->get_All();
$this->some_other_fn_in_same_controller( $data['results'] );
$this->load->view('view_header');
$this->load->view('view_nav',$data);
$this->load->view('view_content');
$this->load->view('view_footer');
}
function some_other_fn_in_same_controller( $results ){
//to do
}
2) If from view:
2.1) Link:
Some Link
2.2) Form: Just put the id in a hidden field and post the form to the controller and get the id in the controller as usual:
<input type="hidden" name="id" value="<?php echo $result['id'] ?>">
$id = $this->input->post('id')

Related

fetch database records in codeigniter

I'm learning CodeIgniter From this link. I've a little confusion in fetching the data from database.
<h2><?php echo $title; ?></h2>
<?php foreach ($news as $news_item): ?>
<h3><?php echo $news_item['title']; ?></h3>
<div class="main">
<?php echo $news_item['text']; ?>
</div>
<p>View article</p>
While displaying the data in view they have used a variable $news in foreach loop. However this variable is never defined anywhere
Copied from here
$query = $this->db->get('mytable');
// Produces: SELECT * FROM mytable
The second and third parameters enable you to set a limit and offset clause:
$query = $this->db->get('mytable', 10, 20);
// Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
You'll notice that the above function is assigned to a variable named $query, which can be used to show the results:
$query = $this->db->get('mytable');
foreach ($query->result_array() as $row)
{
echo $row['title'];
}
Try like this hope this will give you what you want simply make a controller and call the associated model in the constructor and perform the function what your want it will return the array so you will be eassilly fetch and use it in your view.
Controller code
class TestController extends Controller{
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('User');
}
public function index(){
$data['all_user']=$this->User->getAllUser();
$this->load->view('index',$data);
}
}
this is my model code
class User extends CI_Model{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAllUser()
{
$this->db->select('*');
$this->db->from('users');
return $this->db->get()->result();
}
}
just follow the steps
make a controller
make a function inside the controller
call the associated model function into it and you will definately get the result.
Have you seen this Controller code ? (above the code that you pick, in the tutorial)
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
$news is passed through as $data element from the Controller to the View

Getting Error : Trying to get property of non object

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 }?>

How to get the value from model to controller

model
This is my model.using last query I get value in the model,but I don't get the value in the controller
class Login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function email()
{
$this->db->select('email');
$this->db->from('change_password');
$result=$this->db->get();
return $result;
}
}
controller
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$dbemail=$this->login_model->email();
echo $dbemail;
}
}
CI is a MVC Framework. So you need to Command from Controller and get data from the models and finely you need to pass them to view. This is a best Practice
Controller
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();// assign your value to CI variable
$this->load->view('home', $data); //passing your value to view
}
Model
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
$result = $query->result_array();
return $result;
}
View
Extra Knowledge
View will be create under View folder/ Ex im using view name
as home.php
you can use your own stylings(its also created as normal html page.)
foreach ( $dbemail as $new_dbemail )
{
echo $new_dbemail['database_field'];//in here you can get your table header.
//Ex if your table has name field and you need to sho it you can use $new_dbemail['name']
}

How to pass a value from view to model in Code Igniter

I want to edit retrieved users that am stuck here is my code
I have a function in the model that selects all the users
public function retrieve() {
$this->db->select('*');
$query = $this->db->get('UserDetails');
return $query->result();
}
In the controller the queried data is passed to view
public function edit_user() {
$data['query']=$this->user_model->retrieve();
$this->layout->view('creation/update_user',$data);
}
Like this
<?php foreach($query as $row): ?>
<?php echo $row->username;?>
<?php echo $row->firstname;?></a>
<?php endforeach;?>
I want when one clicks on the username he should be able to edit the user.
How do i get the value of the clicked username that will be passed to the
$username
in the model (am aware CI is MVC, how can pass to controller then to model)
public function select_user_details() {
$this->db->select('*');
$this->db->where('username' $username);
$query=$this->db->get('users');
return $query->result();
}
In the controller for editing users
public function edit_user() {
$data['query']=$this->user_model->select_user_details();
$this->layout->view('creation/update_user',$data);
}
In the display
<? foreach($query as $row)
<input type="text" name="uname" value="<?php echo $row->username; ?>"/>
<input type="text" name="name" value="<?php echo $row->names; ?>"/>
<?php endforeach; ?>
You can add a parameter to the edit user method, and make a redirection in your controller. So, if there's no id specified, you get the list of all users, but if there is an id, you run a new model function (query with WHERE user_id = $id rather than $username) and load another view. Something like this:
public function edit_user($id = false) {
if ( !$id ) {
$data['query']=$this->user_model->retrieve();
$this->layout->view('creation/update_user',$data);
}
else {
$data['query']=$this->user_model->retrieve_one_user($id); // new model method for a single user
$this->layout->view('creation/edit_user',$data); // new view
}
}
And in your view, you need to make your username link go to .../edit_user/USER_ID_HERE.
You can also use different controller methods for that: like list_users() and edit_user($id). Also, you can do it in a single view. It's really up to you and what will work best for your app. There are million ways to do it.
To do it with the least amount of changes, just add a parameter to the select_user_details method (as well as the edit_user method in the controller):
public function select_user_details($username) {
...
public function edit_user($username) {
...
But again, make sure that the link from the user list would go to .../select_user_details/my_username_here. Like this:
<?php echo $row->username;?>
In View -
<?php foreach($query as $row): ?>
<?php echo $row->username;?>
<?php echo $row->firstname;?></a>
<?php endforeach;?>>
In Controller -
function edit_user($userName)
{
$data['query']=$this->user_model->retrieve($userName);
$this->layout->view('creation/update_user',$data);
}
In Model -
public function retrieve($userName) {
$this->db->select('*');
$query = $this->db->get('UserDetails');
return $query->result();
}

Getting ID from previous page to field on next page (CodeIgniter)

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;
}

Categories