loading views in editing record in codeigniter - php

Hello I am new to codeigniter. I am learning this framework by developing a testing application. I am showing user listing and in front of each record an anchor tag to edit that record. That anchor tag looks like
echo(anchor('first/edit_user/'.$value->id,'Edit'));
It redirects the browser to first controller and edit_user function. At edit_user function I load the view of edit like this.
$this->load->view('edit_user',$data);
It loads the view with respect to selected record to edit the record and the url looks like
http://localhost/CodeIgniter/index.php/first/edit_user/9
Every thing works fine. But when user clicks the update button it again comes to same function. It updates record and then again tries to load the same view but here a problem occurs. Now after updating record it loads the view and url becomes like this
http://localhost/CodeIgniter/index.php/first/edit_user
It creates error as it is not having the id of the selected record. IF i change the load view code of my function like this
$this->load->view('edit_user/'$this->uri->segment(3)),$data);
It generates an error of edit_user/.php is not defined and some thing like that. Now I want to ask How can I Redirect the user to same edit form telling that his record has been updated? How to load the view of
selected record from function of the controller?

To redirect to the same URL as the current one:
redirect(current_url());
Otherwise, be specific where you want to redirect.
$this->load->view('edit_user/'$this->uri->segment(3)),$data);
It generates an error of edit_user/.php is not defined and some thing like that.
Instead of using specific URL segments, use the parameters passed to your controller and set defaults, as well as handle the possible absence or invalidity of the arguments passed (url segments). Example:
function edit_user($id = NULL)
{
if ( ! $id) // handle error (redirect with message probably)
$user = $this->user_model->get($id);
if ( ! $user) // User not found, handle the error
// If user found, load the view and data
}
Remember that controllers are still just php classes and functions, and accept user input (anything I can type in the address bar) - so never assume anything about what's in the URL, always respond appropriately if the data you need isn't there.

First load the URL helper "config/autoload.php"
/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url');
Or load the URL helper in your function itself
public function edit_user($user_id = '')
{
if(!is_numeric($user_id) {
// user id is not here redirect somewhere //
redirect('home/index');
}
if(isset($_POST['submit'])) {
// proceed to model // #param is optional // you can pass hidden field from form also //
$this->user_model->edit_user($user_id)
}
$this->load->helper('url');
$this->load->view('first/edit_user', $data);
}
Now go to your form :
<form method="post" action="<?php echo current_url(); ?>">
</form>
This will return to same edit page, after submit also.
Hope this helps you, let us know if anything there... Thanks!!

Related

How can i put page loader in laravel controller's function?

I am routing my user's to third party URL.
Now whenever they are routing there is a blank page or default tab which is open is displaying, but they are actually redirecting on that URL.
What i want to do like as soon as that routing first function called i need to put page loader there so user's come to know that they are redirecting.
How can i do that in controller ? I can load view file i know but after that remaining code will not work of that function.
Guide me over here.
Thanks in advance.
I tried loading view file but it was not run remaining code.
Simply direct your users to a holding page, and then run some simple javascript redirects to get them to their actual destination.
app/routes/web.php
Route::get('redirect', 'YourController#redirect');
app/Http/Controllers/YourController.php
public function redirect(Request $request) {
$url = $request['url'];
return view('holding-page', compact('url'));
}
resources/views/holding-page.blade.php
<script type="text/javascript">
// Redirect the user to where they want to go after 3 seconds.
setTimeout(function() {
window.location.replace("{{ $url }}");
}, 3000);
</script>
Make sure you url encode any urls that you pass as a query string parameter.
https://yoursite.com/redirect?url=https%3A%2F%2Fstackoverflow.com

How to use the URL ignoring controller's default behavior in codeigniter?

I have a CI codebase in my localhost whose URL is http://localhost/mydomain/site/
here "site" is a folder NOT controller
I have DB table data field where locations of certain areas are stored.
Ideally when the above URL is hit, the default "home" controller is called.
But I Want if user hit the URL as http://localhost/mydomain/site/southtown/ where "southtown" is the locatily name, user can see all the southtown associated data pn the view page.
Note: the data comes on the basis of location(here southtown) from database.
Also there will be URLS as
localhost/mydomain/site/southtown/aboutus
localhost/mydomain/site/southtown/inspiration
localhost/mydomain/site/southtown/services
These will be the respective pages as per locations specified in URL all data coming from DB
try using routes, like
$route["home"] = "home";
$route["home/(:any)"] = "home/index/$1";
and in your home controller, do:
public function index($location = "") {
//get $location and check against db
}
So, when use hits url http://localhost/mydomain/site/southtown/, your $location will have value "southtown" for it.

Load view depending user type logged

I´m actually using Codeigniter with php in my project and when a user is logged at the page if its a normal user load the same view if his a admin but changing some things in it.
I think that passing a variable to the controller and depending in it load the view with the changes depending on the user.. but the url its seems not ver cool let se this:
public function dc($q="")
{
if($q=="o")
{
// Here i have to change some parts of the template for normal users
$this->load->view('Main/template_main', $data, FALSE);
}elseif ($q=="a") {
// Here i have to change some parts of the template for admins
$this->load->view('Main/template_main', $data, FALSE);
The url is like www.xxx.com/controller/o or /a I want to see it like www.xxx.com/controller because its the index page..
Thanks for your time..
You should approach this in the way that Damien suggests. Your controller should set session data something like:
$this->session->set_userinfo('is_admin', FALSE);
Then, when you have checked the login creds, you can set the session data when the user is an admin:
$this->session->set_userinfo('is_admin', TRUE);
On your page, you can then set who sees what based on this value.

Adding dynamic id in the url using codeigniter

I have an edit page with url
example.com/product/edit/11
Now if the validation is false I intend to reload the edit page but can't figure how to add that id '11' in the url
Things I have done so far
In routes.php
$route['product/edit/(:num)'] = "product/edit/$1";
Now how do I pass that id in the url from the controller
I tried this
$this->load->view('product/edit/'.$edit_id,$data);
Edit: I am not trying to pass edit_id to the view file
I am trying to simulate this:
Edit
So that the function in my controller
function edit($edit_id)
{
//some code
}
Will have have that $edit_id to work with
It didn't work as I suspected.
I know I can pass that id to the view file and make it work, but I wanted to if I can pass that id in the url.
Thanks
Edit: I figured that even if I managed to pass it in the url. I will end up with more problem like setting the form attributes and other stuff. What I did was
redirect('product/edit/'.$edit_id);
and gave an error message by setting the flashdata.
Your Product.php controller
function edit($edit_id) {
//some codes
//passing edit_id to view
$data['edit_id'] = $edit_id;
$this->load->view('product/edit', $data);
}
And in view/product folder, edit.php view file
//some codes
echo $edit_id
Or directly from url in view file
echo $this->uri->segment(3);

Zend, How to access current route from within Action Helper

In Zend Framework I have an Action Helper that loads a login form on most pages. This happens in the preDispatch() method of the Helper and I want to setAction() on the form so that it posts back to the current URL.
What's the best way to access the current URL / route from within the Action Helper? Access the Request (via the Action Controller), then pull then getActionName() and getControllerName(), and concatenate them with baseURL()?
Is there a simpler way? (Set action requires the URI string as a parameter).
Thanks!
You can do as #Elie suggested. However, if you want to use ZF methods for this, you can have a look at this:
$request = Zend_Controller_Front::getInstance()->getRequest();
echo $request->getHeader('referer'); // referer's address
echo $request->getRequestUri(); // current address
I found that I didn't need to access the current URL / route from within the Action Helper. By leaving the form action blank, it automatically posts to the current URL. Perfect.
If I understand correctly, when the user logs in, you want to send them back to the page where they came from. The code I use to do this is:
// the user has come from a particular page - send them back
if($_SERVER['HTTP_REFERER']) {
$this->_redirect($_SERVER['HTTP_REFERER']);
} else {
// the user has come from the home page, or this page
$this->_redirect('/');
}
which is located in the login action (i.e. LoginController->loginAction()).

Categories