link_to and passing variables - php

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()); ?>

Related

Pagination custom url in codeigniter-4

I created a method in a model of CI4 to query a search results. I need to paginate search results but the URL not provide results. I need to standard way for pagination in CI4.
Here is model method -
<?php
public function search($key)
{
$builder = $this->table('apps_common_all');
$builder->Like('name',$key,'after');
$query = $builder->paginate(51);
return $query;
}
?>
controller method is -
<?php
public function result()
{
$key = $this->request->getVar('s');
$apps = new AppsModel();
$data = [
'items' => $apps->search($key),
'pager' => $apps->pager,
];
return view('search/index',$data);
}
?>
Pagination code in view page
<?php if ($pager) :?>
<?php $pagi_path=getenv('pagi_path').'search_result'; ?>
<?php $pager->setPath($pagi_path); ?>
<?= $pager->links() ?>
<?php endif ?>
when pagination link clicked the url not provide results
http://myshahadat.com/demo/apkdownload/search_result?s=a&page=2
and my route is
<?php $routes->get('/search_result', 'Search::result'); ?>

How to pass data controller to view in codeigniter

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

fetch value from url and pass it to another page in codeigniter

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.

Codeigniter -> Edit Page

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

how to get the value of form input box in codeigniter

value of FORM INPUT Help!!
//this is just a refrence of $nm and $fid from test_model//
$data['fid']['value'] = 0;
$data['nm'] = array('name'=>'fname',
'id'=>'id');
say i have one form_view with
<?=form_label('Insert Your Name :')?>
<?=form_input($nm)?>
and a function to get single row
function get($id){
$query = $this->db->getwhere('test',array('id'=>$id));
return $query->row_array();
}
then in controller.. index($id = 0)
and somewhere in index
if((int)$id > 0)
{
$q = $this->test_model->get($id);
$data['fid']['value'] = $q['id'];
$data['nm']['value'] = $q['name'];
}
and mysql table has something like 1. victor, 2. visible etc. as a name value
but here its not taking the value of name and id from form_input and not showing it again in form_view in same input box as victor etc so to update and post it back to database...
anyone please help!!
and please be easy as I am new to CI!!
Based on your comment to my first answer, here is a sample of a Controller, Model and View to update a user entry pulled from a table in a database.
Controller
class Users extends Controller
{
function Users()
{
parent::Controller();
}
function browse()
{
}
function edit($id)
{
// Fetch user by id
$user = $this->user_model->get_user($id);
// Form validation
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
if ($this->form_validation->run())
{
// Update user
$user['name'] = $this->input->post('name', true);
$this->user_model->update_user($user);
// Redirect to some other page
redirect('users/browse');
}
else
{
// Load edit view
$this->load->view('users/edit', array('user' => $user));
}
}
}
Model
class User_model extends Model
{
function User_model()
{
parent::Model();
}
function get_user($user_id)
{
$sql = 'select * from users where user_id=?';
$query = $this->db->query($sql, array($user_id));
return $query->row();
}
function update_user($user)
{
$this->db->where(array('user_id' => $user['user_id']));
$this->db->update('users', $user);
}
}
View
<?php echo form_open('users/edit/' . $user['user_id']); ?>
<div>
<label for="name">Name:</label>
<input type="text" name="name" value="<?php echo set_value('name', $user['name']); ?>" />
</div>
<div>
<input type="submit" value="Update" />
</div>
<?php echo form_close(); ?>
It's hard to see the problem from your snippets of code, please try and give a little more information as to the structure of your app and where these code samples are placed.
Presume in the last code listing ('somewhere in index') you are getting $id from the form, but you define the ID of the form input box as 'id' array('name'=>'fname','id'=>'id') rather than an integer value so maybe this is where the problem lies.
Where does the $data array get passed to in the third code listing?
From your question I think you want to display a form to edit a person record in the database.
Controller code
// Normally data object is retrieved from the database
// This is just to simplify the code
$person = array('id' => 1, 'name' => 'stephenc');
// Pass to the view
$this->load->view('my_view_name', array('person' => $person));
View code
<?php echo form_label('Your name: ', 'name'); ?>
<?php echo form_input(array('name' => 'name', 'value' => $person['name'])); ?>
Don't forget to echo what is returned from form_label and form_input. This could be where you are going wrong.

Categories