Asked  10 Months ago    Answers:  5   Viewed   10 times

sorry to bother but I was hoping someone could help me out with a quite mundane issue that I am having within CI. I am able to send a variable via the URL using CI's examples eg:

http://localhost/project/main/getproduct/24

within the getproduct() method of my main controller I can get the variable sent 24, without an issue.

however I now want to pass two variables via the URL, but I have no idea how to do this or whether CodeIgniter will allow me to do this. could someone please show me how to pass 2 variables in CI and a method that can retrieve them I have tried:

http://localhost/project/main/getproduct/24/45

and then within my getproduct method:

public function getproduct($productID, $factoryID){
  .....
}

but I'm finding my method can get the first variable without an issue but not the second variable. Could anyone point me in the right direction please. Many thanks in advance.

 Answers

3

You can use uri to retrieve values in your url

Here is an example

public function getproduct()
{
  $productID =  $this->uri->segment(3);
  $factoryID =  $this->uri->segment(4);
  // ProductID will be 25
  // Factory ID will be 45
}

Then you can just use the values as you please

Tuesday, August 3, 2021
 
jasu
 
2

Instead of going to /test , try going to /index.php/test

By default CodeIgniter runs everything through index.php.

You can change this behaviour by adding a .htaccess file to the site root folder.

This is taken straight from the CodeIgniter user guide.

https://ellislab.com/codeigniter/user-guide/general/urls.html

The htaccess file

RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

If you do this you also need to change your config/config.php file

find

$config['index_page'] = 'index.php';

change it to

$config['index_page'] = '';

When you want to create links in a page (view) you can use the built in site_url() function, you may need to load the url helper for this.

for example

<?php echo site_url('test'); ?>

This will take into account the $config['index_page'] setting and create the right url for your link.

Thursday, April 1, 2021
 
3

This should work,

  <a href="<?= base_url(). 'promos/details/' . str_replace(' ', '_', $promo_row->name); ?>"> </a>

As far as - vs _ if you are routing to them, for example say that product name becomes a method in your controller, I don't think PHP allows - in method names, but it doesn't allow spaces either.. You can cheat using __call() but that's a other story for a different day.

If its a parameter, then it shouldn't matter as much. Just if it is a method/function name.

-Note- this is not a CI specific error, look at any url, tell me if you see spaces in them.

UPdate: as a suggestion the best way to handle this is by having a field dedicated to the url name ( such as slug or such ) and when a new product is made just do the transform on the name. Then it's an editable piece of data you have control over. For example what if you have a product with a - in it's name. This will probably cause issues. You can avoid that by having a separate piece of data just for that, it could even be like a SKU number etc. It's more work to setup, but in the long run more maintainable.

Thursday, April 1, 2021
 
Trott
 
1

Use the ampersand & to glue variables together:

$url = "http://localhost/main.php?email=$email_address&event_id=$event_id";
//                               ^ start of vars      ^next var
Wednesday, June 2, 2021
4

Request for http://mysite.com/var1/value2.html is rewritten to http://mysite.com/index.php?var1=var1&var2=var2

where index.php and .htaccess file are at the documentroot

.htaccess files will only work if httpd.conf (or some apache conf file) has "AllowOverride All" set for the DocumentRoot you are working in. Check that first.

Next, make sure you have mod_rewrite enabled in Apache conf files (restart webserver after changing conf files) and then enable it in your .htaccess file

contents of .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
# /var1/value2.html to /index.php?var1=value1&var2=value2
RewriteRule ^([^/]+)/([^.]+).html$ /index.php?var1=$1&var2=$2
</IfModule>

put this in /index.php to see it work:

<? print_r($_GET); ?>
Saturday, August 21, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :