I am getting user profile fields from the database and want to display in my view but don't know to do this with the master layout.
this is my model
function fetchProfile($id,$page){
$query = $this->db->query("SELECT * FROM staff_master WHERE Id='$id'");
return $query->result();
}
this is my controller:
public function edit($id,$page){
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
$this->load->view('template_master',$data);
}
I am also trying to find a solution. I am passing user Id and another by URL (get method).
You are overwriting $data['query'] when you assign the array next:
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
Either do:
$data= array('content'=>'view_staff_edit');
$data['query'] = $this->StaffModel->fetchProfile($id,$page); // note position
Or:
$data = array(
'content' = 'view_staff_edit',
'query' => $this->StaffModel->fetchProfile($id,$page),
);
Access in view via $query and $content.
Unrelated:
You are also missing $page in your query, and its generally a good idea to declare gets as null if not set or you will get a notice: public function edit($id=null,$page=null){
Your overriding your first declaration of variable $data what you can do is to initialize them both at the same time.
Controller
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id,$page),
'content' => 'view_staff_edit'
);
$this->load->view('template_master',$data);
}
Then access it on your View file
<h1><?php echo $content ?></h1>
<?php foreach($query as $result): ?>
<p><?php echo $result->id ?></p>
<?php endforeach; ?>
Try doing something like this:
public function edit($id,$page) {
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data['content']= 'view_staff_edit';
$this->load->view('template_master',$data);
}
YOUR MODEL
function fetchProfile($id){
return $this->db->get_where('Id' => $id)->result_array();
}
YOUR CONTROLLER
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id),
'content' => 'view_staff_edit',
);
$this->load->view('template_master',$data);
}
YOUR "template_master" VIEW
<?php foreach ($query as $res){?>
<span><?php echo $res['Id'];?></span> //User html content according to your requirements ALSO you can add all columns retrieved from database
<?php } ?>
Related
I need to use the function in my view/template page.
My code:
public function getAppNumber($id = null){
$aPPLICATIONINFO = $this->AppEstLto->APPLICATIONINFO->find('all', [
'fields'=>['application_number'],
'conditions'=>['application_id'=>$id],
'limit' => 200
]);
foreach($aPPLICATIONINFO as $aPPLICATIONINFO){
$aPPLICATIONINFOx = $aPPLICATIONINFO;
}
return $aPPLICATIONINFOx;
}
You can use set() to use the function variables in your view as given in cookbook:https://book.cakephp.org/3/en/views.html#setting-view-variables
public function get_app_number($id = null){
$applicationInfo = $this->AppEstLto->APPLICATIONINFO->find('all',
[
'fields'=>['application_number'],
'conditions'=>['application_id'=>$id],
'limit' => 200
]);
//Create an array
$applicationArray = new array();
//Store all results in array
foreach($applicationInfo as $application){
$applicationArray = $application;
}
// Pass the array to view
$this->set(compact('applicationArray'));
}
Now, you can use it in your view:
get_app_number.ctp:
<?php
foreach($applicationArray as $application)
{
echo $application['application_number'];
}
?>
You should be doing something like this in your routes.php file inside your Config folder:
Router::connect('/get-app-number', array('controller' => 'yourController', 'action' => 'get_app_number'));
That way you will be able to connect the url that will be used for your view.
Your action corresponds and sends data to your view.
The action is the function that is inside your controller in which you developed for setting the data and variables.
The example of the slug which would be generated:
http://localhost/get-app-number
I have a url of a page as
http://localhost/projectname/admin/client/1
In the above url i have a form in which i need to fill details and save it in database. The form on this page is:
<?php
$data = array(
'type'=>'text',
'name'=>'job_title',
'value'=>'Job Title',
'class'=>'form-control');
?>
<?php echo form_input($data); ?>
<?php
$data = array(
'type'=>'submit',
'class'=>'btn',
'name'=>'submit',
'content'=>'Submit!'
);
echo form_button($data);
?>
<?php
echo form_close();
?>
On the submission of the page i wish to save it in a table but along with it i also wish to carry the id/value which is in url (in this case it is 1), and would like to carry it forward till model so that i can perform actions in database based on this id/value. Can anyone please tell how it can be done
Present code for controller
public function form()
{
$this->form_validation->set_rules('job_title','Full Name','trim|required|min_length[3]');
if($this->form_validation->run() == FALSE)
{
$regdata = array
(
'regerrors' => validation_errors()
);
$this->session->set_flashdata($regdata);
redirect('admin/clients');
}
else
{
if($this->user_model->job())
{
redirect ('admin/client');
}
}
}
Present code for model // In the users table i wish to add job title to that row where the id matches the value in url i.e 1
public function job()
{
$data = array(
'job_title' => $this->input->post('job_title')
);
$insert_data = $this->db->insert('users', $data);
return $insert_data;
}
Try some thing like this:
$clientId = $this->uri->segment('3'); // will return the third parameter of url
Now put this $clientId in form in a hidden input. Now you can get the $clientId on controller function when form submits. Pass it to model or use accordingly.
I am new to codeigniter and trying to write an update function to update information in my database. I've gone through a few tutorials but for some reason my code is not working! Any tips or assistance would be greatly appreciated. I'm just testing with two fields right now, name and email, and I get a blank page when I go to my update view.
Here is my model method:
function get_by_id($id){
return $this->db->get_where('table',array('id'=>$id));
}
function update($id){
$attributes=array(
'name'=> $this->input->post('Name'),
'email'=> $this->input->post('Email')
);
return $this->db->where('id',$id)
->update('table',$attributes);
}
And here is my relevant controller code:
function edit(){
$data['row']=$this->Mymodel->get_by_id($id)->result();
$data['name']= 'Name';
$data['email']='Email';
$this->load->view('updateview', $data);
}
function update($id){
$this->load->model('Mymodel');
$this->Mymodel->update($id);
if($this->input->post()){
redirect('kittens/view/'.$id, 'refresh');
}
And here is my update view:
<?php echo form_open('kittens/update/')
echo form_hidden('id', $row[0]->id);
foreach($attributes as $field_name){
echo '<p>' . $field_name;
echo form_input($field_name, $row[0]->$field_name) . '</p>';
}
echo form_submit('', 'Update');
echo form_close();
?>
Any help would be appreciated! Not sure what specific part is giving me the trouble!
Use the following template:
Controller:
public function updateData(){
....
$data = array(
"columnName" => $columnValue
);
$whereValue = $someNumber;
$this->model_name->updateTable($data, $wherevalue);
...
}
Model:
public function updateTable($data, $whereValue){
$this->db->where('columnName', $whereValue);
$this->db->update('tableName', $data);
}
the first thing is that the model "MyModel" isn't loaded on the "edit" method in the controller.
If you're planning to use that specific model in every function of your controller you should load it in the constructor (so you don't have to load it every time).
Using$this->input->post('Name') in a model is a bad idea. You should receive that data in the controller process it (if you have to) and then send it to the method in the model.
I don't know if you can "chain" the db methods like this in codeigniter:
$this->db->where('id',$id)->update('table',$attributes);
try this:
$this->db->where('id',$id);
$this->db->update('table',$attributes);
Hope that helps
////////////model for update/////////////
public function update_customer_contacts($where, $data){
return $this->db->update('customer_contacts', $data, $where);
}
///////////////////update customer///////////////////
public function update_customer()
{
$data = array(
'company_name'=>$this->input->post('company_name'),
'address' =>$this->input->post('address'),
'telephone' =>$this->input->post('telephone'),
'fax' =>$this->input->post('fax'),
'email' =>$this->input->post('email'),
'website' =>$this->input->post('website'),
);
$res= $this->customer_model->update_customer(array('customer_id' => $this->input->post('customer_id')), $data);
;
echo json_encode($this->input->post());
}
I have a page: my-account/my-templates/view/1 - which simply uses the id field of 1 to display the item
What I'm wanting to do is to create a copy action, that simply takes all the values in this item and creates a new item
I have a link_to() which holds the following:
<?php echo link_to('Copy', '#copy_template', array('id' => $template->getId())); ?>
I'm passing in the id of the template.
Can I access this id in the copy action?
EDIT:
Actions:
public function executeViewTemplate(sfWebRequest $request)
{
$this->template = Doctrine_Core::getTable('UserTemplate')->getUserTemplate($request->getParameter('id'));
}
public function executeTemplateCopy(sfWebRequest $request)
{
$this->id = $request->getParameter('id');
// get the passed template id
}
Templates:
viewTemplateSuccess.php
<?php echo link_to('Copy', '#copy_template', array('id' => $sf_request->getParameter('id'), 'class'=>'button green')); ?>
templateCopySuccess.php
<?php echo "ID". $id;?> --> doesn't return the passed id
In an action, you can retrieve a parameter using:
$id = $request->getParameter('id');
For example, if you have this action:
public function executeCopyTemplate(sfWebRequest $request)
{
$id = $request->getParameter('id');
}
And from a template:
<?php $id = $sf_request->getParameter('id') ?>
In your case:
<?php echo link_to('Copy', '#copy_template?id='.$sf_request->getParameter('id')); ?>
The array at the end of the link_to function alters HTML attributes like class and id. It doesnt pass url parameters normally. THis will work:
<?php echo link_to('Copy', '#copy_template?id='. $template->getId(), array()); ?>
I am getting the following error when I try and access:
domain.co.nz/admin/editpage/home/
I get the following error:
PHP Fatal error: Call to a member function getCMSPage() on a non-object in controllers/home.php on line 22
The issue with this is that I cannot understand why it is being passed back into the main “home” controller - which is the main controller.
All of my models are loaded by default - http://cl.ly/2U1F3a2B0s2K0i3k3g13
Ideal Situation
What I am trying to do with this is load the content into a text area on for editing and when submit is clicked I would like it to go back to the same page with a message saying content updated.
Admin Template
<li><?php echo anchor('#','Edit Pages');?>
<?php if(is_array($cms_pages)): ?>
<ul>
<?php foreach($cms_pages as $page): ?>
<li><a >permalink?>"><?=$page->name?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
Page Model
function updatePage($data){
$data = array('content' => $content);
$this ->db->where('id',$id);
$this->db->update('pages',$data);
}
View:
<?php
//Setting form attributes
$formpageEdit = array('id' => 'pageEdit', 'name' => 'pageEdit');
$formInputTitle = array('id' => 'title', 'name' => 'title');
$formTextareaContent = array('id' => 'content', 'name' => 'content');
?>
<section id = "validation"><?php echo validation_errors();?></section>
<h4><?= $title ?> </h4>
<?php
echo form_open('admin/editpage/'.$page->permalink, $formpageEdit);
echo form_fieldset();
echo form_label ('Content', 'content');
echo form_textarea("content", $page['content']);
echo form_submit('submit','Submit');
echo form_fieldset_close();
echo form_close();
?>
Controller:
function index(){
if($this->session->userdata('logged_in')){
}else{
redirect('admin/home');
}
$page = $this->navigation_model->getCMSPage($this->uri->segment(3));
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = $page->name;
$data['content'] = $this->load->view('admin/editpage', array('page' => $page, TRUE));
$this->load->view('admintemplate', $data);
}
I haven't really tested this yet but it should give you a good start. What you're asking is quite a bit of work to fully code out quickly.
This is the page_model model it's essentially what you posted with a few minor tweaks. Its best to use id's rather than strings. You may also want to do some htmlspecialchars or mysql hijack validation prior to passing these to your DB.
<?php
/**
* This is the Page_model model, this model handles the retrieval and modification of all pages.
*
**/
class Page_model extends CI_Model {
/**
* the getCMSPage function retrieves the data from the db given the ID that was passed via $id.
**/
function getCMSPage($id = NULL) {
$this->db->where('permalink', $permalink);
$query = $this->db->get('pages', 1);
#check to make sure row's were returned, if so continue, otherwise return false.
if ($query->num_rows() > 0){
#set the results into an array and return $row, should return $row['content'], and $row['id'];
#if you were editing more than one page this is where you would use a foreach($query)
$row = $query->result_array();
return $row;
}else{
return false;
}// END if ($query->num_rows() > 0)
}// END function getCMSPage()
}// END Page_model class
?>
This is the site controller and for sake of time i just echo'd your form_textarea straight from the edit function.. you shouldn't do this as it goes against MVC standards. You should create a view for that portion of the section.
<?php
/**
* This is the Site controller, primary controller for your site.
*
**/
class Site extends CI_Controller {
/**
* construct function, in our case we are going to load the Posts_model , you may not want to do this.
*
**/
function __construct()
{
parent::__construct();
#load Page_model, should be located in app/models/page_model.php
$this->load->model('Page_model');
}//END function __construct();
/**
* edit function, this function handles retrieval of the page from the URI, and the page's content for editing.
*
* This function uses $id which auto retrieves the page's ID from the uri. Your URL should look similiar to:
* http://yourdomain.com/site/edit/3/yourunecessaryinformationhere
* everything after the id is not really required but could help with SEO.
*
**/
function edit($id){
#retrieve the page's content in array form.
$page = $this->Page_model->getCMSPage($id);
echo form_textarea("content", $page['content']);
}
}//END Site Class
Keep in mind i wrote this up in 5-10 minutes so its rushed and not very well commented or even tested but itll give you a great head start. This only gives you an example of how to retrieve information from the DB and echo it into a textarea. You would still need to create another function within your page_model to insert/update the information and another snippet of code in your controller to pass the edited content to the model.
You can hit me up on AIM or twitter if you have more questions ThirdGenSup, twitter is #gorelative
// this line
$data['content'] = $this->load->view('admin/editpage', $data);
// needs to be
$data['content'] = $this->load->view('admin/editpage', array('page' => $page, TRUE);