Add parameter to Codeigniter URL - codeigniter

I have a problem when i try to add other parameter to URL.
before i use Codeigniter i add those parameters using JavaScript like this
test
but when i tried to do it with Codeigniter i don't know how.
<?php echo anchor("home/index/param1","test"); ?>
as i said i want to add this parameter for example my URL looks like this
home/index/param2
so when i click on test i want the URL to be like this
home/index/param2/param1

Take a look at CodeIgniter's URL Helper Documentation
The first parameter can contain any segments you wish appended to the URL. As with the site_url() function above, segments can be a string or an array.
For your example, you could try:
<?php
$base_url = 'home/index/';
$param1 = 'param1';
$param2 = 'param2';
$segments = array($base_url, $param1, $param2);
echo anchor($segments,"test");
?>

You can't do that with the form helper, you have to use your js function again :
echo anchor("home/index/param2", "test", array("onClick" => "javascript:addParam(window.location.href, 'display', 'param1');"));
It will produce :
test
But I don't see the point of dynamically change the href on the click event. Why don't you set it directly at the beginning ?
echo anchor("home/index/param2/param1", "test");

Related

How to get translate message in controller Laravel?

I have file excel.php by the path /resources/lang/en/excel.php
Then in controller I tried to fetch word by key:
use Lang;
echo Lang::get('excel.idEvent');
Also I tried:
dd(echo __('excel.idEvent'));
Whats is right way to do that?
First, your excel.php file must be in the right format:
<?php
return [
'welcome' => 'Welcome to our application'
];
The right way to get it on your blade template in fact it is:
echo __('excel.welcome');
or
echo __('Welcome to our application');
The way to do it on your controller is:
use Lang;
Lang::get('excel.welcome');
If you are not using Facades: use \Illuminate\Support\Facades\Lang;
You can also use the trans() function, ex:
Route::get('/', function () {
echo trans('messages.welcome');
});
If you use JSON translation files, you might have to use __().
Here are all the ways to use:
#lang('...') // only in blade files
__('...')
Lang::get('...')
trans('...')
app('translator')->get('...')
Lang::trans('...')
They all defer to \Illuminate\Translation\Translator::get() eventually.

CodeIgniter adjusting url when passing parameters to controler

Ok i have records from database listed in view file, so u can see i wanna pass values to controler via href by update/grab function controler
echo $this->pagination->create_links();
br().br();
foreach ($query->result() as $q): ?>
<?php echo $q->info . br()?>
<?php endforeach; ?>
it works for first page in my pagination, when i am on some other page when i clicked on on record, instead passing parametars to controler when i clicked in keep adding url for example http://localhost/z/records/users/update/grab/3/update/grab/1/update/grab/1/update/grab/1/trtr
So error is when i have in url, when i am on second page in pagination
http://localhost/z/records/users/2
works only when i am on first page
http://localhost/z/records
is there a way to solve this proble. Will it works if i some how adjust routes??? Need help, please help me its very important
Try changing your link to an absolute URL:
<a href="/z/update/grab/<?php echo $q->id;?>/<?php echo $q->info; ?>">
Or adding a correct relative URL base to the header of your pages:
<base href="/z/" />
Codeigniter routes allow you to do this:
$route['post/(:any)/comment/(:any)'] = "posts/comments/$1/$2";
Then in the controller, the function inside my posts controller would work like this:
public function comments($one, $two) {
echo $one."-".$two;
}
so if you hit the url "/post/111/comment/222" the output would be
111-222

$_REQUEST not working in codeigniter

i dont know what am i doing wrong but i am just messed a bit
i have a a href thats calling a controller
<img src="<?php echo base_url();?>assets/front_assets/images/button_joinclub.png" width="180" height="44" border="0">
now the function is this
class mailing extends CI_Controller{
private $pagesize = 20;
function __construct() {
parent::__construct();
#session_start();
}
function index()
{
echo $_REQUEST['club'];
}
}
but it gives me error
A PHP Error was encountered
Severity: Notice
Message: Undefined index: club
Filename: controllers/mailing.php
Line Number:12
EDIT
i need to call the mailing/index from different pages, and sometimes i need to pass parameter and sometimes not
if i use
function index($club)
{
//function body;
}
then i always need to send some parameter
sometimes the calling href can be like this also
<img src="<?php echo base_url();?>assets/front_assets/images/button_joinclub.png" width="180" height="44" border="0">
so it will call for an error since in function definition i have issued the presense of a parameter, and i am not passing any parameter through this link
so thats why i need
a href="<?php echo base_url();?>mailing/index/?club="<?php echo $club_info[0]['club_title'];?>"
so that i can use isset($_REQUEST['club'] to check if present or not.
First of all, there is no need to echo base_url(); Only /mailing/index is enough. To pass parameters you do as McGarnagle told you as third segment.
Then in your controller in index function:
$club_title = $this->uri->segment(3);
You have just set a new variable called club_title that holds its value. That's how you pass params and if you dont want to pass it from other pages, you don't need to. It only means that the variable will be null in that case.
The way URI helper works so you understand what happened:
Controller - segment 1
Method - segment 2
Paramater - segment 3
You can add as many parameters as you want and then call access them with URI. Make sure you load it in your autoload.php in config folder or in construct function of your each controller like this:
$this->load->helper('url');
PS: We never use $_REQUEST in codeigniter.
CodeIgniter disables all GLOBALS except $_GET, $_COOKIE and $_POST to ensure security.
Reference:
Register_globals
During system initialization all global variables are unset, except those found in the $_GET, $_POST, and $_COOKIE arrays. The unsetting routine is effectively the same as register_globals = off.
See Documentation here
CodeIgniter comes with helper methods that let you fetch POST, GET, COOKIE or SERVER items , but CodeIgniter disables all GLOBALS except $_GET, $_COOKIE and $_POST to ensure security.
You can use input methods are as follow :
$this->input->post()
$this->input->get()
$this->input->cookie()
$this->input->server()
CodeIgniter purges the $_REQUEST variable for security reasons. I assume it's related to the automatic input filtering described in the Codeigniter Manual here, but it's not specifically mentioned there either though. I am unsure whether setting
$config['global_xss_filtering'] = TRUE;
in config.php affects it or not.

Change Codeigniter url

I'm using Codeigniter.I want to set href attr to something like :
<a href="/contact.html" >Contact</a>
But i get 404 error because i should write
Contact.
Where is some thing to fix this problem.
Any help please.
Assuming that you have a controller by the name Contact and you successfully extend the CI_Controller class, go to application/config folder and in config.php find:
$config['base_url'] = 'http://www.youdomain.com/';
Then in your internal links you should do:
Contact
If you are using javascript to make the redirect, put on top of the js file:
var host = 'http://www.yourdomain.com/';
Again:
window.location.href = host + 'contact';
If you're using codeigniter, you do not want to point to an .html file.
If you're using codeigniter correctly, you should use the helper methods that exist in codeigniter.
Instead of writing the anchor tag yourself, try this:
<?php echo anchor('contact', 'Contact'); ?>
to add the suffix to your controller in calling go to config/config.php and search for
$config['url_suffix'] = '';
and assign html to it to become
$config['url_suffix'] = 'html';

CodeIgniter -> Get current URL relative to base url

Tried URI::uri_string() but can't get it to work with the base_url.
URL: http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi
Returns: dropbox/derrek/shopredux/ahahaha/hihihi
but http://localhost/dropbox/derrek/shopredux/ just returns an empty string.
I want the first call to return "ahahaha/hihihi" and the second to return "". Is there such a function?
// For current url
echo base_url(uri_string());
If url helper is loaded, use
current_url();
will be better
Try to use "uri" segments like:
$this->uri->segment(5); //To get 'ahahaha'
$this->uri->segment(6); //To get 'hihihi
form your first URL...You get '' from second URl also for segment(5),segment(6) also because they are empty.
Every segment function counts starts form localhost as '1' and symultaneous segments
For the parameter or without parameter URLs Use this :
Method 1:
$currentURL = current_url(); //for simple URL
$params = $_SERVER['QUERY_STRING']; //for parameters
$fullURL = $currentURL . '?' . $params; //full URL with parameter
Method 2:
$full_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Method 3:
base_url(uri_string());
I see that this post is old. But in version CI v3 here is the answer:
echo $this->uri->uri_string();
Thanks
//if you want to get parameter from url use:
parse_str($_SERVER['QUERY_STRING'], $_GET);
//then you can use:
if(isset($_GET["par"])){
echo $_GET["par"];
}
//if you want to get current page url use:
$current_url = current_url();
Running Latest Code Igniter 3.10
$this->load->helper('uri'); // or you can autoload it in config
print base_url($this->uri->uri_string());
I don't know if there is such a function, but with $this->uri->uri_to_assoc() you get an associative array from the $_GET parameters.
With this, and the controller you are in, you know how the URL looks like.
In you above URL this would mean you would be in the controller dropbox and the array would be something like this:
array("derrek" => "shopredux", "ahahaha" => "hihihi");
With this you should be able to make such a function on your own.
In CI v3, you can try:
function partial_uri($start = 0) {
return join('/',array_slice(get_instance()->uri->segment_array(), $start));
}
This will drop the number of URL segments specified by the $start argument. If your URL is http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi, then:
partial_uri(3); # returns "ahahaha/hihihi"
you can use the some Codeigniter functions and some core functions and make combination to achieve your URL with query string.
I found solution of this problem.
base_url($this->uri->uri_string()).strrchr($_SERVER['REQUEST_URI'], "?");
and if you loaded URL helper so you can also do this current_url().strrchr($_SERVER['REQUEST_URI'], "?");
<?php $currentMenu = $this->uri->segment(2); ?>
<ul>
<li class="nav-item <?= ($currentMenu == 'dashboard') ? 'active' : '' ?>">
<a href="<?= site_url('/admin/dashboard'); ?>" class="nav-link"><i data-
feather="pie-chart"></i> Dashboard</a>
</li>
</ul
this is work for me

Resources