Not able to pass data from view to Controller via url - codeigniter-3

I am new to CodeIgniter and trying to pass data from view to controller, and then access it. I have looked for various solutions but none of them worked for me. Can you please help in letting me know where am I wrong?
the URL helper is loaded automatically, and I don't understand what else to do.
view.php
...
<a href="<?php echo base_url().'product/'.$id; ?>
...
Controller.php
class Product extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function index($id){
$id = $this->uri->segment(2);
echo "hello";
echo $id;
}
}
the expected result is to print Hello but instead it shows 404 Page not found.
Please help. Thanks in advance.

I got the solution to the answer.
<a href="<?php echo base_url().'index/product/'.$id; ?>
prints the correct output. It is so because i did the url_routing in my site. Thanks for the help by the way.

Related

How can i redirect to previous page after a form submit in codeigniter

I have a test function in controller which generates a form page.
public function testing()
{
$this->form_validation->set_rules('test', 'TEST', 'required');
if ($this->form_validation->run()) {
redirect($this->agent->referrer());
} else {
$data['title'] = 'Testing';
$data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->load->view('templates/header', $data);
$this->load->view('pages/nonmember/testing', $data);
$this->load->view('templates/footer');
}
}
The code of testing view as below
<div id="message"><?php echo $message; ?></div>
<?php echo form_open(base_url()."testing");?>
<p>
<?php echo form_input('test');?>
</p>
<p><?php echo form_submit('submit', 'Submit');?></p>
<?php echo form_close();?>
If users come from another page lets say About page to testing page i want after submitting form properly they get redirected back to About page. But it is not being possible as referer agent is staying at the testing page, so after submit it is staying there and not redirecting to About page.
All you need is:
redirect($_SERVER['HTTP_REFERER']);
add this line in your controller's testing function
$this->session->set_flashdata('url',$this->agent->referrer());
change this in the function testing
if ($this->form_validation->run()) {
redirect($this->session->flashdata('url'));
} else {
$this->session->set_flashdata('url',$this->agent->referrer());
//load the view
}
...
or you can also submit an hidden field like,
form_hidden('rURL', $this->agent->referrer());
then simply use its value in the controller.
if ($this->form_validation->run()) {
redirect($this->input->post('rURL'));
} else {
...
Not sure if it is the best solution or not , but i found the solution working perfectly.
We need to write the referrer address in a file and to redirect we need to read from that file.
first we need to load the file helper.
$this->load->helper('file');
if ($this->form_validation->run())
{
redirect(read_file('referrer.php'));
}
else
{
$referrer = $this->agent->referrer();
//need to check wrong input submission and page refresh
if ($referrer != "http://localhost/CIpractice2/testing" && !empty($referrer))
{
write_file('referrer.php', $referrer);
}
//show the form
}
Please refer to codeIgniter's file helper documentation

Assigning View Helpers ZF 1.12

I can't get view helpers to work. This is what I've done so far:
I added this in application.ini
resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/helpers"
In directory application/helpers I put the file findurl.php
Inside findurl.php I put
<?php
class My_View_Helper_findurl extends Zend_View_Helper_Abstract
{
public function findUrl($url){
$url = $url;
return htmlspecialchars($url);
}
}
?>
Then in a view.phtml file I tried $this->findurl("http://google.com"); and got no luck, page goes blank.
I know I'm probably getting naming conventions wrong, could anyone help me out? Thank you.
I also tried this in view.phtml
$helper = $this->_helper->getHelper('findurl');
echo $helper->findUrl("http://google.com");
Try:
$this->findurl('url');
http://framework.zend.com/manual/1.12/en/zend.view.helpers.html

View Helper in Kohana

I am trying to figure out a way to do the following:
I want to make an action which will be loaded through ajax and also its the internal part of the page when page is reloaded.
I know this in ZEND framework by using View Helper, But don't know how to do in Kohana
I am new to Kohana.
EDIT:
Example of what I am trying to do http://www.espncricinfo.com/west-indies-v-india-2011/engine/current/match/489228.html?CMP=chrome
In above webpage when the whole web page is loaded the live score board is loaded with it. But when u click on "Refresh scoreboard" button only the live score board is replaced through ajax.
I want to create an action say action_scoreboard which will be used to bring scoreboard data. And action_index to load the whole page, but while in the view of action_index i need to call action_scoreboard.
Thanks
Not sure if this is the best way to do this, but this is how I like to handle the situation.
public function action_index($raw = 0) {
$records = Jelly::select('scores')->execute();
if ($raw == 0) {
$view = new View('purdy');
$view->records = $records;
$this->template->content = $view;
} else {
$this->auto_render = FALSE;
$this->request->headers['Content-Type'] = 'text/xml';
$view = new View('raw');
$view->records = $records;
$this->response->body($view->render());
}
}
### THE PURDY VIEW ###
<table>
<?
foreach ($records as $record) {
echo '<tr>';
echo '<td>'.$record->name.'</td>';
echo '<td>'.$record->value.'</td>';
echo '</tr>';
}
?>
</table>
### THE RAW VIEW ###
<?xml version="1.0" encoding="utf-8"?>
<scores>
<?
foreach ($records as $record) {
echo '<score>';
echo '<name>'.$record->name.'</name>';
echo '<value>'.$record->value.'</value>';
echo '</score>';
}
?>
</scores>
I used Kopjax - Pjax jQuery ajax module. Its code is available on gitgub

How to render a view with specific text?

I have a class like this:
class myData {
function render(){
$str = 'This is string.';
// have to code here
}
}
and a myview.phtml file:
<div id='someid'></div>
Q: Now I want to do something like this in another phtml file:
<?php
$obj = new myData ();
echo $obj->render(); // it should be <div id='someid'>This is string.</div>
?>
So how can I change my render function in myData class that it should get myview.phtml and place string between DIV tag(<div id='someid'></div>) and print.
Thanks
One possible solution could be to use Partial view helper. This helper can be used to " render a specified template within its own variable scope".
Specifically in myview.phtml you can add the following:
<div id='someid'><?php echo $this->myText; ?></div>
Then, in the another phtml you could have:
<?php
$obj = new myData ();
echo $this->partial('path/to/myview.phtml',array('myText' => $obj->render()));
?>
Maybe you are looking for something like this:
http://webgen.hu/class.html.txt
http://webgen.hu/class.html.php

Embedding persistent login form Zend

I've seen this question asked already - but none of the answers really gelled for me, so I'm asking again: I want to embed a persistent login form (which will change into a nav bar if logged in) in the header bar for a site. Effectively, I want to be able to inject some controller logic into the layout.
After much research, I can see several ways that might achieve this - none of which seem ideally suited.
View Helpers seem suited to adding a suite of methods to the Zend_View object - but I don't want to write conditional code in the layout.phtml to trigger a method. Action helpers would help me remove that functionality and call it from a Controller - but that seems to be in poor favour from several quarters. Then there are plugins, which might be well suited in the dispatch/authentication loop.
So, I was hoping someone might be able to offer me some guidance on which way might best suit my requirements. Any help is greatly appreciated.
For those of you with a similair issue, this is how I ended up solving it (I'm using layout btw)
I registered a view helper in the Bootstrap:
protected function _initHelpers(){
//has to come after view resource has been created
$view = $this->getResource('view');
// prefix refers to the folder name and the prefix for the class
$view->addHelperPath(APPLICATION_PATH.'/views/helpers/PREFIX','PREFIX');
return $view;
}
Here's the view helper code - the actual authentication logic is tucked away in model code. It's a bit clumsy, but it works
class SB_UserLoginPanel extends Zend_View_Helper_Abstract {
public function __construct() {
$this->user = new SB_Entity_Users();
$this->userAccount = new SB_Model_UserAccount();
$this->request = Zend_Controller_Front::getInstance()->getRequest();
$this->form = $this->makeLoginForm();
$this->message='';
}
//check login
public function userLoginPanel() {
if(isset($_POST['loginpanel']['login'])) {
$this->processLogin();
}
if(isset($_POST['loginpanel']['logout'])) {
$this->processLogout();
}
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$this->loginPanel = $this->getUserNav();
} else {
$this->loginPanel = $this->getLoginForm();
$this->loginPanel .= $this->getMessages();
}
return $this->loginPanel;
}
private function processLogin() {
if($this->form->isValid($_POST)){
$logindata = $this->request->getPost('loginpanel');
if($this->user->login($logindata['email'],$logindata['password'])) {
Zend_Session::rememberMe();
$redirect = new Zend_Controller_Action_Helper_Redirector();
$redirect->goToUrl('/account/');
return $this->getUserNav();
}else {
$this->message = '<p id="account_error">Account not authorised</p>';
}
}else {
$this->form->getMessages();
}
}
private function processLogout() {
if(isset($_POST['loginpanel']['logout'])) {
$this->user->logout();
$request_data = Zend_Controller_Front::getInstance()->getRequest()->getParams();
if($request_data['controller']=='notallowed') {
$redirect = new Zend_Controller_Action_Helper_Redirector();
$redirect->goToUrl('/');
}
}
}
private function makeLoginForm() {
}
private function getLoginForm(){
return $this->form;
}
private function getMessages(){
return $this->message;
}
private function getUserNav(){
//return partial/render
}
}
I then call this from the relevant part of the markup in the layout.phtml file.
<?php echo $this->doctype(); ?>
<head>
<?php
echo $this->headLink() ."\n";
echo $this->headScript() ."\n";
echo $this->headMeta() ."\n";
?>
<title><?php echo $this->escape($this->title) ."\n"; ?></title>
</head>
<div id="masthead">
<div id="userLoginPanel">
<?php echo $this->userLoginPanel(); ?>
</div>
</div>
<!--rest of layout-->
In principle, this should be an action helper, but after reading some less than favourable articles regarding Zend Action Helper - I opted for this method which did the trick.
Hope that helps!