show html option value codeigniter - php

I'm new in Codeignite. I have a "Test" Controller with "index()" and "view($id)" functions. the index method goes to "test1.php" view. In "test1.php", I have a dropdown options. if I submit on this page, the "view" method of "Test" Controller will be called. My question is how can I pass the option value to "view" function , so that the argument of method will be set to the option value and then the url would be something like "http://localhost/test/view/id" which the id is option value from "test1.php"
Test Controller
class Test extends CI_Controller{
public function indx() {
//some code
$this->load->view('test1.php');
}
public function view($id)
{
//some code, here I use $id which I want to be option value from test1.php
}
test1.php
<?php
$options = array(
'1' => 'One',
'2' => 'Two',
'3' => 'Three',
'4' => 'Four',
);
$js = 'id="shirts" onChange="this.form.submit();"';
echo form_dropdown('shirts', $options, '1', $js);
/* here I want to call echo form_open() as echo form_open("/test/view/[option value]") but I don't know how to do this;*/

You can load view with data.
class Test extends CI_Controller{
public function index($id) {
$this->data['result'] = get_results($id);
$this->load->view('test1.php', $this->data);
}
}
In the view file:
foreach ($results as $result) {
// action
}

If its essential that you pass the ID in the URL then you'll need to use javascript to append the ID to the form action URL using a change event listener on the select field.
However, IMHO, you would be better off just retrieving the value within the view() method from the POST data.
Using $id = $this->input->post('shirts', TRUE), rather than forcing it through the URL.

Related

How to create a function in controller and use it in template/view in cakephp 3.xxx

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

Passing data from controller function to controller function before redirect

I am using codeigniter I have an edit page which shows me all information of a vacancy.
The (Vacancy) controller method to load this view looks like this, it makes sure all data is preloaded.
public function editVacancy($vacancyid)
{
$this->is_logged_in();
$this->load->model('vacancy/vacancies_model');
// Passing Variables
$data['title'] = 'Titletest';
$data['class'] = 'vacancy';
$orgadminuserid = $this->vacancies_model->getOrgAdminUserId($vacancyid);
if ((!is_null($orgadminuserid)) && ($this->auth_user_id == $orgadminuserid[0]->user_id)) {
$data['vacancyid'] = $vacancyid;
$data['vacancy'] = $this->vacancies_model->get($vacancyid);
$data['test'] = $this->session->flashdata('feedbackdata');
$partials = array('head' => '_master/header/head', 'navigation' => '_master/header/navigation_dashboard', 'content' => 'dashboard/vacancy/edit_vacancy', 'footer' => '_master/footer/footer');
$this->template->load('_master/master', $partials, $data);
}
}
In this view i have different forms for updating different sections.
Every form submit goes to a different method in my 'Vacancy' controller.
public function saveGeneralInfo()
{
$this->is_logged_in();
$this->load->model('vacancy/vacancies_model');
$vacancyid = $this->input->post('vacancyid');
$vacancyUpdateData = $this->vacancies_model->get($vacancyid);
$result = $this->vacancies_model->update($vacancyid, $vacancyUpdateData);
if ($result) {
$feedbackdata = array(
'type' => 'alert-success',
'icon' => 'fa-check-circle',
'title' => 'Success!',
'text' => 'De algemene vacature gegevens zijn geupdate.'
);
$this->session->set_flashdata('feedbackdata', $feedbackdata);
redirect("dashboard/vacancy/editVacancy/" . $vacancyid);
}
}
}
Where I indicated in my code "//HERE ..." is where I would want the feedback message parameter to pass on to my 'main controller method' which loads the view with the prefilled data. (editVacancy).
Is there a clean way to do this?
EDIT:
I tried using flashdata, i updated the code to have the flashdata inserted.
However when i do a var_dump($test); in my view, it remains null.
EDIT 2:
I noticed when i put my $_SESSION in a variable in my editVacancy controller method (which is being redirected to) and var_dump it in my view that this does not contain the ci_vars with the flashdata in.
Instead of using set_header you can use simple redirect function for it.
redirect("dashboard/vacancy/editVacancy/".$vacancyid);

Zend Form getValues() doesn't work

I'm trying to create simple form with Zend, I need to use this form in most part, so I create the default form then in controller i modify it for the occurrence with private function. But I have two problems:
the form getValues() doesn't take the value of text element.
I put render at the end of the form action, but it doesn't render to the right page.
The form consists of a text field and the sumbit button
Here is the code of my controller:
That is for customize the form
private function getSearchForm($action = '', $name, $type, $placeholder)
{
$urlHelper = $this->_helper->getHelper('url');
$this->_searchForm = new Application_Form_Admin_Search_Search();
$this->_searchForm->setName($name);
$text = $this->_searchForm->getElement('ricerca');
$text->setLabel('Ricerca '.$type);
$text->setName($type);
$text->setAttrib('placeholder', $placeholder);
$this->_searchForm->setAction($urlHelper->url(array(
'controller' => 'admin',
'action' => $action),
'default'
));
return $this->_searchForm;
}
there are the actions:
public function pneumaticoAction()
{
$this->_searchForm = $this->getSearchForm('pneumaticosearch', 'search', 'pneumatico', 'Ricerca per: modello, marchio o codice');
$this->view->searchForm = $this->_searchForm;
}
public function pneumaticosearchAction()
{
if (!$this->getRequest()->isPost()) {
$this->_helper->redirector('index', 'public');
}
$form=$this->_searchForm;
if (!$form->isValid($this->getRequest()->getPost())) {
$this->render('pneumatico');
}
$values = $form->getValues();
$this->view->assign(array(
"pneumatici" => $this->_modelAdmin->searchPneumatici($values['pneumatico'])
));
$this->render('pneumatico');
}
First question, whenever you get routed to pneumaticosearch action, you do not set $this->_searchForm but you have it as:
$form=$this->_searchForm;
Should be something like this:
$form = $this->getSearchForm('pneumaticosearch', 'search', 'pneumatico', 'Ricerca per: modello, marchio o codice');
And the second question. When you run render, it is similar to pass $this->view parameters to .phtml. I don't see your view files, but I guess you need to set view first:
$this->view->searchForm = $form

Add Custom Row action in Prestashop ModuleAdminController

I want to add a download button for each row in moduleadmincontroller helper.
I tried to add it by using the following code on RenderList function. But it is not working.
$this->addRowAction('download');
Kindly let me know if I can add custom action for each row and how to process it.
as you know the actions is the default array that have default value array('view', 'edit', 'delete', 'duplicate'); and you can use this but if you want add new action you should use some function.for example you can go to your_prestashop/controllers/admin/AdminRequestSqlController.php
this class add new action with 'export' name
$this->addRowAction('export');
then for create link for this action it is using the displayExportLink() function as you can see in bellow code
public function displayExportLink($token, $id)
{
$tpl = $this->createTemplate('list_action_export.tpl');
$tpl->assign(array(
'href' => self::$currentIndex.'&token='.$this->token.'&
'.$this->identifier.'='.$id.'&export'.$this->table.'=1',
'action' => $this->l('Export')
));
return $tpl->fetch();
}
and then you can get your new action with the initProcess() function or initcontent() function and do something lik download
public function initProcess()
{
parent::initProcess();
if (Tools::getValue('export'.$this->table))
{
$this->display = 'export';
$this->action = 'export';
}
}

Can I direct two different buttons to the same function in a controller with the same view in CakePHP?

There are two buttons in my cakephp page,one for registering new users and the other one for login. Can both the button's action be directed to the same function in the controller and have the same view.ctp file? If yes, how can I do it?
Yes, just set the correct URL in your buttons. But I don't know why you would do this. If it is just about re-using the view.ctp then you do not need to use a single action just to use the same view. Example:
<?php
class FoobarController extends AppController
{
function view()
{
// This will render views/foobar/view.ctp because the action
// is named "view"
}
function register()
{
// Normally this would render views/foobar/register.ctp but you can
// call the render() function manually and render something else. The
// following call will render views/foobar/view.ctp
$this->render('view');
}
function login()
{
// Same thing here...
$this->render('view');
}
}
?>
I create buttons in my CRUD admin pages that allow either "Confirm (edit/delete/create/etc)" or "Cancel". I do this by creating 2 submit buttons in the form, and giving each a unique name. For example:
View code:
...
$form->submit('Delete', array('name' => 'delete'));
$form->submit('Cancel', array('name' => 'cancel'));
...
Action logic:
function admin_delete( ... ) {
// Bail if cancel button pressed
if (isset($this->params['form']['cancel'])) {
$this->redirect('/');
}
// Delete if delete button pressed
if (isset($this->params['form']['delete'])) {
// delete stuff
...
}
...
}
On the flip side, you're essentially smashing 2 actions into one for the sake of reusing a view. Sander Marechal's solution is better.
Well, yes, why not? Isn't this only a matter of setting the appropriate URL in your form actions? Or am I missing something?
You can use a hidden form value to denote which action it is.
$form->create('User', array('action' => 'process');
$form->hidden('User.signup', array('value' => '1'));
$form->end('Signup');
$form->create('User', array('action' => 'process');
$form->hidden('User.login', array('value' => '1'));
$form->end('Login');
It isn't exactly clear why you don't want to use 2 functions though. You are basically going to have to manually check which action it is, instead of letting cake do it for you.
In your controller
function process()
{
if ($this->data['User']['signup'] == 1)
{
// process signup
}
if ($this->data['User']['login'] == 1)
{
// process login
}
}

Categories