itsme
itsme

Reputation: 400

get the parameter from the url which has slace in codeigniter

My URL is like below

http://localhost/demo/company/warehouse_subportal/printEway/CN/00003

my invoice no is CN/00003 and sometimes CN-00003. what I want is to get the invoice from URL. Now I am getting only CN.I know that is because of /. How can I get the invoice no like CN/00003? Please help me thanks in Advance.

Upvotes: 0

Views: 87

Answers (4)

louk
louk

Reputation: 97

Encode & Decode the parameter will resolve your issue.

Exemple:

When preparing the url:

$invoice_number = urlencode("CN/00003") // Or "CN-00003"

$url = "http://localhost/demo/company/warehouse_subportal/printEway/" . $invoice_number

To get the invoice number from url:

$invoice_number = urldecode($this->uri->segment(4));

Upvotes: 0

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

You can use segments.

if(strpos($this->uri->segment(4), '-') !== FALSE){
   $invoice_num = $this->uri->segment(4);
}
else {
   $invoice_num = $this->uri->segment(4).'/'.$this->uri->segment(5);
}

https://www.codeigniter.com/user_guide/libraries/uri.html#CI_URI::segment

Upvotes: 0

pr1nc3
pr1nc3

Reputation: 8338

<?php

$data='http://localhost/demo/company/warehouse_subportal/printEway/CN-00003';

$data=explode('/',$data);

if(is_numeric(array_values(array_slice($data, -1))[0])){
    $invoice=implode(array_slice($data, -2, 2, true),'/');

}
else{
    $invoice=array_values(array_slice($data, -1))[0];
}

echo $invoice;

So basically i check the last field of the url to see if it contains only numbers or has also letters and based on the response i echo the right value.

Works for both / and -

Upvotes: 0

Lajos Arpad
Lajos Arpad

Reputation: 76508

You can use urlencode, when you generate your links or urls example:

<?php
echo '<a href="mycgi?foo=', urlencode($userinput), '">';
?>

and then use urldecode to decode the invoice value, example:

<?php
$query = "my=apples&are=green+and+red";

foreach (explode('&', $query) as $chunk) {
    $param = explode("=", $chunk);

    if ($param) {
        printf("Value for parameter \"%s\" is \"%s\"<br/>\n", urldecode($param[0]), urldecode($param[1]));
    }
}
?>

Note, that only the invoice part needs to be encoded or decoded, the part of

CN/00003

another possible solution is to encode into base64, using base64_encode, example:

<?php
$str = 'This is an encoded string';
echo base64_encode($str);
?>

and decode it using base64_decode, example:

<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?>

Upvotes: 1

Related Questions