Fetching "id" from a view and pass to another view in CodeIgniter - php

I'm new in CodeIgniter. I'm having a problem fetching "id" from "project view" then i will pass it to another view which a call "project details view".
I've tried using ampersand (&) in URL and i guess that does not work in CodeIgniter.
Here are my codes:
In my controller:
public function project(){
$data["title"] = "CodeIgniter Projects";
$this->load->model("projects_model");
$data["result"] = $this->projects_model->getProjects();
$this->load->view("project", $data);
}
public function project_details($project_id){
$this->load->model("projects_model");
$data["result"] = $this->projects_model->getProjectDetails($project_id);
$this->load->view("project_details", $data);
}
for my model
function getProjects(){
$query = $this->db->query("SELECT * FROM projects");
return $query->result();
}
function getProjectDetails($project_id){
$this->db->where('project_id', $project_id);
$query = $this->db->get('projects');
}
in my view i only use < a href="controller/project_details">< / a> to call the project details view.

You can pass anything to controller from a view as a uri_segment. The general syntax is,
Controller/function/parameter
Here, in your case
controller/project_details/<?php echo $project_id; ?>

Related

I could not retrive a value from controller page

I am using codeigniter framework. I have a table named client. I need to get the first name from the table and send it to my view page. I have a function in model file to retrieve the client data.
This is my model- function:
public function get_cli($id)
{
$this->db->select('*');
$this->db->from('client');
$this->db->where('id', $id);
$res = $query->result();
$row = $res[0];
return $row->first_name;
}
Here I get my client first name.
This is my controller file. Here I have a function to send the data from the client table to my view page.
public function ticket($id)
{
$id=$this->uri->segment(4);
$data['id']=$this->uri->segment(4);
$data['client']=$this->billing_model->get_cli($id);
$data['employee']=$this->employee_model->emp_name();
$data['main_content'] = 'admin/billing/ticket_page';
$this->load->view('includes/template', $data);
}
My view page.
<label>Client Id:<?=$id?></label>
<input type="text" value="<?=$client['first_name'];?>"/>
I am getting a white screen. Why is this happenening? Can someone help me with the code?
Change your model function to this:
public function get_cli($id)
{
$this->db->select('*');
$this->db->from('client');
$this->db->where('id', $id);
$query = $this->db->get();
$result = $query->result_array();
return $result[0]['first_name']; //returning only the first name key value of first row(0th row) of the result;
}
Then On the view page:
<input type="text" value="<?=$client;?>"/>
Which will print only the first name;
Another approach:
Change your model function to this:
public function get_cli($id)
{
$this->db->select('*');
$this->db->from('client');
$this->db->where('id', $id);
$query = $this->db->get();
$result = $query->result_array();
return $result; //returning the whole array;
}
Then On the view page:
<input type="text" value="<?=$client[0]['first_name'];?>"/>
Which will print only the first name value from the 0th(first row) of the array;
You should change your view page to this
<input type="text" value="<?=$client;?>" />
This will work as you are returning the firstname in your model not whole array.
So, your $client variable will contain the firstname. You dont need to refrence it.
Simply echoing $client will work.
When you have only single row in result i suggest you to use row_object() instead of result()
public function get_cli($id)
{
$this->db->select('*');
$this->db->from('client');
$this->db->where('id', $id);
$res = $query->row_object();
//$row = $res[0]; no need to write this line
return $res->first_name;
}
for your problem on your view
You will get first_name in $client
<input type="text" value="<?=$client?>"/>
if you are not getting anything please check your result from database might be null.
USE
$row['first_name'] instead of $row->first_name

Codeigniter with Ignited Datatables

I have finally got Codeigniter to work with ignited datatables. Now i have run into a different problem. Can anyone help or tell me if i could run the below query with the datatables plugin for codeigniter.
At present i'm doing it within the controller which is lame i know (this was only for testing)
Controller
$data['query'] = $this->test_queries->list_partners();
foreach($data['query'] as $k => $company){
$data['query'][$k]->partner_contacts = $this->test_queries->get_partner_contacts($company->id);
}
Queries in the Model
function list_partners(){
$this->db->select("company.id,name,general_email,general_phone,market");
$this->db->from("company");
$this->db->join('markets','markets.id = company.market_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
function get_partner_contacts($id){
$this->db->select('partner_contacts.id,contact_type');
$this->db->from('partner_contacts');
$this->db->where('company_id',$id);
$this->db->join('department','department.id = partner_contacts.contact_type_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
You can change the queries in the model as below:
function list_partners(){
$this->datatables->select("company.id,name,general_email,general_phone,market");
$this->datatables->from("company");
$this->datatables->join('markets','markets.id = company.market_id');
return $this->datatables->generate();
}
function get_partner_contacts($id){
$this->datatables->select('partner_contacts.id,contact_type');
$this->datatables->from('partner_contacts');
$this->datatables->where('company_id',$id);
$this->datatables->join('department','department.id = partner_contacts.contact_type_id');
return $this->datatables->generate();
}
You can also use method chaining if you are using PHP 5 or above.

passing variables in form_open() in codeigniter

Good day all. I'm new to codeigniter and I'm trying to pass a variable from one view page to another. But I get error and after a lot of tries still couldn't figure it out.
This is the Model code:
function get_post($postID){
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->result_array();
}
function update_post($postID, $data)
{
$this->where('post_id', $postID);
$this->db->update('khanposts', $data);
}
This is the Controller code:
function editpost($postID)
{
$data['success']=0;
if($_POST){
$data_post=array(
'fullname'=>$_POST['fullname'],
'dob'=>$_POST['dob'],
'blood'=>$_POST['blood'],
'village'=>$_POST['village'],
'occupation'=>$_POST['occupation'],
'company'=>$_POST['company'],
'email'=>$_POST['email'],
'contact'=>$_POST['contact'],
'password'=>$_POST['pass'],
'marry'=>$_POST['marry']);
$this->khanpost->update_post($postID, $data);
$data['success']=1;
}
$data['post']=$this->khanpost->get_post($postID);
$this->load->view('edit_post', $data);
}
This is the code of View page which passes the value to edit_post view page:
foreach ($posts as $row){
<tr><td><i>Edit</i></td></tr>';
}
This is code of edit_post view page where it must get the value of $row['post_id']:
echo form_open(base_url().'khanposts/editpost/'.$row['post_id']);
echo '<b>Full Name: </b>';
$data_form=array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value'=>$row['fullname'] );
echo form_input($data_form);
How do I assign the passsed variable($row['post_id']) in form_open()? Any solution will be really helpful. Tnx.
As far as I understand the problem;
Pass PostID to view in editpost function:
function editpost($postID)
{
...
$data['postID'] = $postID;
}
Get it in view page like:
echo form_open(base_url('khanposts/editpost/'.$postID));
You should load form helper:
$this->load->helper('form');
And your data_form input:
$data_form = array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value' => $post['fullname']);
You should use row_array instead of result_array, because of you get single post.
function get_post($postID)
{
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->row_array();
}

Issue in accessing table contents using status parameter

I am trying to display jobs won by a certain provider. What I did was to create function get_approved_job_proposals in my model. Then, I created function manage_job_contracts in my controller, but I never got to successfully run it.
Here's my code in model:
public function get_approved_job_proposals($status)
{
$this->db->select('*')->from('job_proposal')->where('status', $status);
$this->db->where("status" == "Awarded");
$query = $this->db->get();
return $query->result_array();
}
and this is what I have in my controller:
public function manage_job_contracts()
{
$this->validateRole('client');
$this->load->model('job_model');
$data['my_preference'] = $this->job_model->get_approved_job_proposals($status);
$data['job'] = $this->job_model->get_job($id);
$this->load->view('client/manage_job_contracts', $data);
}
Kindly help me fix this issue.
This is wrong:
$this->db->where("status" == "Awarded");
Correct:
$this->db->where("status", "Awarded");
Check documentation: http://ellislab.com/codeigniter/user-guide/database/active_record.html

codeigniter use part of query result in another query

It was hard to come up with a title. I am using CodeIgniter with models/views/controller. I have the following tables in my MySQL database that are relevant:
In my model I have the following function:
function get_shoptable() {
$this->db->from('productshop')->where('productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
In my controller I use the above function like
$data['bookshop'] = $this->Product_model->get_shoptable();
In my view I am foreaching $bookshop. My problem is, what is the best wayto show shopName, instead of showing shopId. Taking in regards that $bookshop should be as it is (except of shopid), because I am creating a HTML table with product data.
Try some like this:
function get_shoptable() {
$this->db->from('productshop')
->join('shop', 'productshop.shopId = shop.shopId')
->where('productshop.productId', $this->productId);
$query = $this->db->get();
return $query->result();
}
Model:
function get_products() {
$this->db->select('productshop.productUrl, productshop.price, productshop.deliveryTime, productshop.shippingCast, productshop.inventory, productshop.productId, productshop.shopId, shop.shopName');
$this->db->from('productshop');
$this->db->join('shop', 'productshop.shopId = shop.shopId');
$this->db->where('productshop.productId', $this->productId);
return $this->db->get()->result_array();
}
Controller:
function products() {
$data['products'] = $this->model_name->get_product();
$this->load->view('products', $data);
}
VIEW:
<?php foreach($products as $p): ?>
<h1><?php echo $p['productUrl']; ?></h1>
<h1><?php echo $p['shopName']; ?></h1>
<?php endforeach(); ?>
get an overlook to active class of codeigniter for details of functions
function get_shoptable()
{
$this->db->from('productshop')
$this->db->join('shop', 'productshop.shopId = shop.shopId')
$this->db->where('productshop.productId', $this->productId);
$query = $this->db->get();
return $query->result();
}

Categories