Adding dynamic id in the url using codeigniter - php

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

Related

Reload current URL in Codeigniter After Submit The Form

I have one form in my view.php file. Its URL like https://example.com/members/view/1, https://example.com/members/view/2 etc. When I submit form its calling model through controller like below
public function insert_comments(){
$data=$this->input->post();
$this->load->model('work_model');
$result=$this->work_model->insert_comments($data);
if($result)
{
$this->session->set_flashdata('insert_comments','your comments succesfully');
$this->session->set_flashdata('succesfully','alert-success');
$this->load->view('add_coments');
}
else{
$this->session->set_flashdata('insert_comments','your comments failed');
$this->session->set_flashdata('succesfully','alert-danger');
$this->load->view('add_coments');
}
}
}
and model is like below
public function insert_comments($array)
{
return $this->db->insert('comments',$array);
}
Currently its working fine and on form submit its loding view called add_comments, instead I want reload/refresh current page. I am not able to get idea of how I can do it, let me know if someone can help me for do it.
Thanks!
According to this answer, in the controller you can use:
redirect($this->uri->uri_string());
use this :
redirect($_SERVER['REQUEST_URI'], 'refresh');
Whenever you are reloading or redirecting a page you should always use the redirect() method instead of loading a view.
Redirection basically uses the header() method of core PHP and redirection will never execute the code blocks written beyond the redirect() method. But in the case of loading the view, it can execute until the end of the code block.
In your code, replace the line $this->load->view('add_coments'); with the redirection to the desired controller
redirect('your-controller','refresh');
I hope that helps you.

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

CodeIgniter GET method and SEO friendly URLs

In my web page search input I am using the get method, and the URL is something like this www.example.com/search?city=example&service=example2, but I want a URL like this www.example.com/search/example/example2; how can I convert?
Or maybe there is some other solution to get the data from html inputs and get a friendly URL?
I think its help you and also SEO.
Please write following uri rules in : application/config/routes.php
$route['search-(:any)-(:any).html'] = 'search?city=$1&service=$2';
its rewrite your url from www.example.com/search?city=example&service=example2 to www.example.com/search-example-example2.html
In your routes.php file inside the config folder:
$route['search/(:any)'] = 'search/searchFunction'; //You will now need to have a searchFunction() inside the controller search. Or you can just have 'search' and handle the code below inside the index() function.
Now, inside your searchFunction:
function searchFunction()
{
$city = (isset($this->uri->segment(2))) ? $this->uri->segment(2) : null; //if you have www.example.com/search/example
$service = (isset($this->uri->segment(3))) ? $this->uri->segment(3) : null; //if you have www.example.com/search/example/example2
//General Pattern is: www.example.com/uri-segment-1/uri-segment-2/uri-segment-3.....and so on.
// Now when you are querying, if($city) then 'WHERE city = $city' and so on.
//Also, in the link that was earlier triggering 'www.example.com/search?city=example&service=example2'
//like <a href='www.example.com/search?city=example&service=example2'>Link</a>
//Now have it as:
// <a href='www.example.com/search/example/example2'>Link</a>
}
I see others posting about routing, yes i can do like what, but i think first i need to solve problem with html submit action, because when i press the button its auto redirect to what standart format url search?city=example&service=example2, but like i say i need other format. So how i can solve this problem?
My form action
<form method="get" action="http://example.com/search">

Code Igniter: Load controller with jQuery but forbid direct url loading

I have a website build in Code Igniter with a commenting system that loads a controller function with jQuery for each page.
I'm also passing the article ID as URI segment to the controller while loading.
Lets say the controller name is "comments" and the function is:
function get_comments(){
$article_id = $this->uri->segment(3);
echo 'the uri segment is '.$article_id;
}
I call them to my page with the following jQuery:
$(document).ready(function(){
$('.comments-holder').load('<?php echo base_url();?>comments/get_comments/'<?php echo $article_id;?>);
});
and this is fine.
The thing I want to accomplish is to forbid the user to load the "get_comment" function by typing the http://www.domain.com/comments/get_comments/
I believe that is possible not to have a solution for this problem a workaround will work good as well even if it means more changes to be done.
Code igniter has this by default.
Add this in the ajax action:
if ($this->input->is_ajax_request()) {
// your regular ajax code
} else {
// redirect or show error
}

loading views in editing record in codeigniter

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!!

Categories