Passing get variable in CodeIgniter - codeigniter

How to pass get variable from my view to the controller and to the model also and load to my view again
without the use of form like
$jcid= $row['id']; // this id from other table which is parent.
$s = "SELECT * FROM `jobs` WHERE job_cat=$jcid";
$re = mysql_query($s);
$no = mysql_num_rows($re);
echo $no;
Thanks in advance
Amit

Instead of using get param use like this
This can be your form url
http://example.com/index.php/news/local/metro/crime_is_up
$this->uri->segment(3); // Will give you news
Like wise in place of 3 replace with 4 will give you metro and so on. You can access this in controller.
Uri class in the codeigniter
http://ellislab.com/codeigniter/user-guide/libraries/uri.html

First use
$this->load->helper('url');
And then
$data['$jcid'] = $this->uri->segment(3);

Related

Laravel 8 how to split url into seo friendly?

I am using laravel 8 and currently my url is looking as
http://127.0.0.1:8000/?country_id_to=1
and I want to set URL as
http://127.0.0.1:8000/uk/en/pakistan
In each route. Ihave no idea to do it.
To add on #Douwe de Haan comment - and be more precise have a look at this page: https://laravel.com/docs/8.x/routing#route-parameters
For your example you could do
Route::get('/{localization}/{language}/{country}', function ($localization, $language, $country) {
// your code
});
Where
http://127.0.0.1:8000/uk/en/pakistan
Would now result in
$localization = "uk";
$language = "en";
$country = "pakistan";
This will obviously also work with a controller.
If you want to reference an entity not by its ID but by a custom field you can do that as well
https://laravel.com/docs/8.x/routing#customizing-the-default-key-name

How can I use "prep_url" from codeigniter inside my View in Laravel?

I add the function "prep_url" from codeigniter to my laravel project like here:
Laravel add http to urls
If I do it in my Controller like this:
$website = example.com;
$url = prep_url($website);
and then in my view I can do it like this:
{{$url}}
it shows me this: http://example.com
so everything works :)
but I have a problem
I have the table users and there all users have a website
in my controller I do it like this:
$user = DB::table('users')->get();
return view('user/einzelansicht' ['user' => $user] );
and in my view I can show my user with
{{$user[1]->name}}
{{$user[1]->website}}
how can I use prep_url here?
because I already did this in my view:
{{prep_url($user[1]->website);}}
but it tells me semicolons are not allowed and without the semicolon it does not work
then I tried in my controller this:
$website = $user->website;
$url = prep_url($website);
but this doesn't work too
so how can I do this?
Maybe your "website" attribute is not a string.
Try
prep_url(json_encode($u[1]->website));

Laravel pass data between views

I have a view with a few variables that I want to pass to a new page and prepopulate a form with them.
$data->title
$data->catid
$data->category
$data->groupName
The "vanila way I could make a url like something.com?var1=something&var2=something etc.
But how can I do this in laravel?
Considering you can redirect to a page with GET parameter. For example, "something.com?var1=something&var2=something", You can actually pass this data to another view in Laravel. Let's assume you want to pass var1 and var2 variables to another view.
You will redirect user to the URL containing two variables as GET parameters, You will also execute Controller function that will handle that route. Let's assume that following is your web.php or routes.php file.
Route::get('firstpage","SomeController#view1");
Route::get("secondpage","SomeController#view2");
Now, in view2 method of the SomeController, You can check if those variables exist or not. Here is an example of var1 and var2.
public function view2()
{
if(isset($_GET['var1'])){
$var1 = $_GET['var1'];
}
// Similar for var2
}
This is how you can pass variables between views.
The best way IMHO is using sessions.
session()->put('data', $data);
And to retrieve it and to check if the key exists:
if (session()->has('data')) {
$data = session('data'); //array
}
If you use the $data in session() only once you can pull it session()->pull('data'); //array
A word of caution if you use database sessions. You may have to change the field 'payload' from TEXT to MEDIUMTEXT if $data holds larger amounts of data.
The use of sessions is not search engine (SEO) friendly. If that matters, the easiest way to make the url is $str = http_build_query($data); and to build the url \URL()->route('your.route').'?'.$str;
To access the data from the URL: $title = \Request::query('title');

Codeigniter URI routing with for multiple methods

I am working on codeigniter. Controller name is "project" and 4 functions are there.."get_city_data", "get_store_data", "get_currency", "get_description".
Parameters I am passing in above 2 functions are - get_city_data($state, $city), get_place_data($state, $city, $store).
So on browser to render these functions I am using the urls respectively as-
http://localhost/project/get_city_data/state1/city1
I want to change the urls like
http://localhost/state1/city1
In routes.php if I define a route like this
$route['(:any)/(:any)'] = 'project/get_city_data/$1/$2' then it reflects for all other functions as well but I want to change urls for these 2 functions only.
Also I do not want to use _remap function as it doesn't go well with my needs.
Can anybody help me in this?
ThankYou
I suggest you to check in the database at first, then determine the routes required. Here is my answer for your requirement.
In your routes.php, try this code:
require_once( BASEPATH . 'database/DB' . EXT );
$url_source = $_SERVER['REDIRECT_URL'];
$arr_url = explode("/", $url_source);
$state_name = $arr_url[3];
$city_name = $arr_url[4];
$store_name = $arr_url[4];
$db = & DB();
//Let say grab 3 columns to be check
$db->select('state_name, city_name, store_name');
if(!empty($state_name)):
$db->where('state_name', $state_name);
endif;
if(!empty($city_name)):
$db->where('city_name', $city_name);
endif;
if(!empty($store_name)):
$db->where('store_name', $store_name);
endif;
$query = $db->get('table', 1);
$obj = $query->row();
if ($obj):
$route["$obj->state_name/$obj->city_name/$obj->store_name"] = "project/get_city_data/$state_name/$city_name/$store_name";
endif;
You can try by this url to test:
http://localhost/state_name/city_name/store_name
I got it solved using regex.
In routes.php i wrote
$url_source = $_SERVER['REQUEST_URI'];
if(!(preg_match('%^/project/get_description\?state=[a-zA-Z_-]*%', $url_source) || preg_match('%^/project/get_currency\?state=[a-zA-Z_-]*%', $url_source))){
if(!preg_match('%^\/[a-zA-Z_-]+\/[a-zA-Z_-]+\/[a-zA-Z_-]+$%', $url_source)){
$route['(:any)/(:any)'] = 'project/get_city_data/$1/$2';
}else{
$route['(:any)/(:any)/(:any)'] = 'project/get_store_data/$1/$2/$3';
}
}
This way I was able to route for particular functions.

How to route all module of HMVC to single controller CI

i use HMVC with CI last version andi want to route all module of HMVC to a controller CI
If i use
$route[‘admin/(:any)’] = “admin/”;
$route[’(:any)’] = “index/index/$1”;
$route[’(:any)/(:any)’] = “index/index/$1/$2”;
$route[’(:any)/(:any)/(:any)’] = “index/index/$1/$2/$3”;
is not a solution, because a url can have many segments
i want to route the controller, method, and all parameters, like this:
$route[’(:any)/(:any)/ *all parameters *’] = “index/index/$1/$2/ *array($parameters)*”;
or how can stop the route of HMVC, i don`t need the route of HMVC.
Pls help.
Thanks, Jhon.
You don't need routing, codeigniter does this already.
From a fresh CI install, playing with the welcome controller:
public function index()
{
$args = $this->uri->uri_to_assoc();
echo "<pre>";
print_r($args);
echo "</pre>";
// $this->load->view('welcome_message');
}
Visiting [http://localhost/codeigniter2.1.2/index.php/welcome/index/param1/param1_value/param2/param2_value/param3/etc]
yields this:
Array
(
[param1] => param1_value
[param2] => param2_value
[param3] => etc
)
So you have your controller [welcome], your function [index] and everything after are parameters; no need to mess with routing.
You don't need to account for every possible parameter that might be routed.
You can use a route like this:
$route[‘admin/(:any)’] = “admin/”;
$route[’(:any)/(:any)/(:any)’] = “index/index/$1/$2/$3”;
Then every URL except admin/* will be routed to index/index. From that point, you can grab the parameters like I posted above or via $this->uri->segment(n); where n is the segment you want (so, $this->uri->segment(3) would return $1 from the route above).

Resources