Welcome Guest, Not a member yet? Register   Sign In
how to retrive complex $_GET variables as a multi-dimensional array? NOT using QUERYSTRING enabled
#1

[eluser]faluci[/eluser]
** I've gooled for hours, and searched within stackoverflow. Found a few similar ones, but couldn't find a good answer yet.**

I'm thinking of rewriting my project using codeigniter.

I have a search.php with possible querystring:
Code:
search.php?o=1&kk=1&k=sales&kx;=&w=4&l=New+York,+NY,+USA&i=222&i=229&i=225&i=238&i=237&i=203&el=3&eu=10&ei=on&d=5&d=4&d=9&d=6&at;=&a=Any
Please note that $_GET['i'] & $_GET['d'] could be arrays.

I found some mentioned about
Code:
$this->url->uri_to_assoc();
hoping I maybe able to retrieve $_GET values as
Code:
/i/222/i/229/i/225/i/238/i/237/i/203
or
Code:
/i/222/229/225/238/237/203
So I tested with
controllers/show.php
Code:
class show extends CI_Controller
{
    function get()
    {
        echo "<pre>";
        print_r ($this->uri->uri_to_assoc());
        echo "</pre>";
    }
}
when I input in url as the following,
Code:
http://localhost/index.php/show/get/l/0123/l/52/l/l2

the above code only returns the last input.
Code:
Array
(
    [l] => l2
)
My actual question is, in Codeigniter
from url above, is there a way to retrive as an array as
Code:
Array
(
[0] => 0123,
[1] => 52,
[2] => 12
)

or similar NOT using Querystring? ('cos if I enable Querystring I cant use other Codeigniter helping features etc)

so I want my old project's querystring as an array something like

Code:
Array
(
[d] => array ([0] => 123, [1] => 456),
[i] => array ([0] => 11, [1] => 99),
[dx] => 1,
[dy] => 'New+York'
)

etc etc
#2

[eluser]M52 Studios[/eluser]
Have you tried doing uri_to_string, and then perhaps using list() and explode() functions to get what you want into an array

EDIT: I'm sorry, $this->uri->uri_string()
#3

[eluser]M52 Studios[/eluser]
By the way, the reason why you are only getting this:

Code:
Array
(
    [l] => l2
)

is because each time, the array key 'l' get rewritten, and that's the reason why you are only getting the very last parameter as the final array. Does that kind of make sense?
#4

[eluser]M52 Studios[/eluser]
Enjoy!

Code:
class Welcome extends CI_Controller {

function get_array()
{
  // Let's get all of the parameters of the url into an array
  $arr = explode("/", $this->uri->uri_string());
  
  // Initiate a new array, as we're going to parse the information to get it into the final form
  $new_arr = array();
  
  // Unless you know the "keys" in advance, you'll have a hard time grabbing parameters of non-numeric value.  See why below...
  $last_key = "";
  
  foreach($arr as $row)
  {
   // Let's ignore the class name, along with the method
   // You can also use: $this->uri->segment(n); // n=1 for controller, n=2 for method ...
   // ... http://stackoverflow.com/questions/2062086/how-to-get-controller-action-url-informations-with-codeigniter
   if($row != $this->router->fetch_class() && $row != $this->router->fetch_method())
   {
    // Ok, so in order to assign numeric values to an array with a non-numeric key, we must first get the key
    if(!is_numeric($row))
    {
     $last_key = $row;
     // We don't want to create a new array within $new_arr each time, that will override our previous data.
     // So, only create a new "sub" array if the key doesn't already exist
     if(!array_key_exists($last_key, $new_arr))
     {
      $new_arr[$last_key] = array();
     }
    }
    else {
     // Push numeric information into the array
     array_push($new_arr[$last_key], $row);
    }
   }
  }
}
}

Again, unless you know in advance what your 'keys' are going to be for each array, you'll have a hard time distinguishing what is the actual data vs its' identifier. So, the above function will work for every case, except for your last one, where the [dy] = New+York.
#5

[eluser]M52 Studios[/eluser]
Here is URL and final array sample:

URL: welcome/get_array/i/222/229/225/i/238/i/237/i/203/d/3234/i/65434/d/2344
Code:
Initial Array from the URI string:
Array
(
    [0] => welcome
    [1] => get_array
    [2] => i
    [3] => 222
    [4] => 229
    [5] => 225
    [6] => i
    [7] => 238
    [8] => i
    [9] => 237
    [10] => i
    [11] => 203
    [12] => d
    [13] => 3234
    [14] => i
    [15] => 65434
    [16] => d
    [17] => 2344
)
Final Array:
Array
(
    [i] => Array
        (
            [0] => 222
            [1] => 229
            [2] => 225
            [3] => 238
            [4] => 237
            [5] => 203
            [6] => 65434
        )

    [d] => Array
        (
            [0] => 3234
            [1] => 2344
        )

)
#6

[eluser]faluci[/eluser]
My question is solved. Thanks M52 Studio.
#7

[eluser]M52 Studios[/eluser]
You're welcome!




Theme © iAndrew 2016 - Forum software by © MyBB