Welcome Guest, Not a member yet? Register   Sign In
URI Get query issues - Solve it once for all
#1

[eluser]damienwjking[/eluser]
Hi all,

I have been going round and round trying different theorys trying to solve get queries. Some involve config changes, rewrites, modifing CI URI and extending it with a custom class. Can someone please show the newbies the correct way to do this. Please


I want to do this

www.test.com/controller/function/?title=Damiens %Test&url=http://www.test2.com/help/codeigniter.html

In theory it should be simple but I have spent more time tying to solve this than I have on the entire build of the site. I've tried lots of differnt code from different forum posts but I always get nothing returned. Is the a simple recommended way of doing this?

Rick if you are listening, can you please get this documented as it has been a real headache.

Please help...
#2

[eluser]pistolPete[/eluser]
Why do you need query strings at all?

Did you have a look at this post?
http://ellislab.com/forums/viewreply/389313/
#3

[eluser]damienwjking[/eluser]
I did try the PATH_INFO and it didn't work, also tryed the MY_URI but because I need a url as one of the querys the / breaks the other code. Well thats what I think is happening.

I have to use it this way as its posting from 3rd party.

www.test.com/controller/function/?title=Damiens %Test&url=http://www.test2.com/help/codeigniter.html

Any other ideas?
#4

[eluser]TheFuzzy0ne[/eluser]
You need to encode the URI, as the forward slashes are seen as the start of another segment.

Check out my library which takes care of this for you - http://ellislab.com/forums/viewthread/106502/
#5

[eluser]damienwjking[/eluser]
Thanks Lollerskates, but I am very new at this and having some issues integrating. Haved added your MY_URI to library, how do I call it in and use it.

My controller has a method called add i.e. below, what method in MY_URI do I call to get the get field back when the URL is

www.test.com/controller/add/?title=Damien &url=http://www.test.com/test2/codeigniter/index.php

function add($title = NULL, $url = NULL)
{

$this->load->library('MY_URI');

$this->MY_URI->assoc_to_alt_uri();

// output $title and $url
echo $title . $url;
}

This errors with fatal undefinded property
#6

[eluser]TheFuzzy0ne[/eluser]
It extends the core URI class, and loads automatically. You call it like this:

Code:
$arr = array(
        'title' => 'Damien',
        'url' => 'http://www.test.com/test2/codeigniter/index.php'
    );
$uri = $this->uri->assoc_to_alt_uri($array, 'controller/add');

This will give you a string that looks like this: controller/add/titleBig Grinamien/url:http://www.test.com/test2/codeigniter/index.php

You can then use that string in your URL.

To get the segments back from the next controller method, you can do:
Code:
$title = $this->uri->asegment('title');
$url = $this->uri->asegment('url');

Hope this helps.

EDIT: The forum has decoded the encoded string, so it doesn't look like it actually will in your script.
#7

[eluser]damienwjking[/eluser]
Ok so I have updated method to

function add()
{

$arr = array(
'title' => 'Damien',
'url' => 'http://www.test.com/test2/codeigniter/index.php'
);

$uri = $this->uri->assoc_to_alt_uri($arr, 'whaddado/add');

$title = $this->uri->asegment('title');
$url = $this->uri->asegment('url');

echo $title.$url;

}

Still returns nothing for this url. Why do I need to set the assoc_to_alt_uri as I will never know what the query for url and title will be, they are dynamic.

Sorry to be confused, I don't understand why its so simple.
#8

[eluser]TheFuzzy0ne[/eluser]
The first function encodes the URI so you can use it in a view or redirect.

The second function is to be used in the receiving controller method (not the same method that you encoded it in), which decodes it. The class allows you to pass parameters via the URI.
#9

[eluser]damienwjking[/eluser]
Still don't get anything back for $title or $url with the following url. Doesn't error just prints nothing. Sorry for bothering you but I really am struggling with this.

http://www.test.com/controller/add?title.../index.php

CONTROLLER...

function add()
{

$title = $this->uri->asegment('title');
$url = $this->uri->asegment('url');
// load query
//$title = $_GET['title'];
//$url = $_GET['url'];
echo 'title and url is '.$title.$url;
}
#10

[eluser]TheFuzzy0ne[/eluser]
I'm not sure if I'm explaining myself correctly, so here's a demonstration that will show how we pass arguments in the URI from func1 to func2.
./system/application/controllers/test.php
Code:
<?php

class Test extends Controller {
    
    function Test()
    {
        parent::Controller();
    }
    
    function index()
    {
        // Nothing interesting here. Just displaying a view..
        $this->load->view('test');
    }
    
    function func1()
    {
        // This is where we encode the URI
        // We should have some validation in here, but for
        // the purpose of this exercise, we have omitted it.
        if ($this->input->post('submit'))
        {
            $arr['title'] = $this->input->post('title');
            $arr['url'] = $this->input->post('url');
            
            $uri = $this->uri->assoc_to_alt_uri($arr, '/test/func2');
            // ...and redirect
            redirect($uri);
        }
        redirect('/test');
    }
    
    function func2()
    {
        // This method will docode the URI and display it.
        echo "Func2<br />";
        echo "title: " . $this->uri->asegment('title') . "<br />";
        echo "url: " . $this->uri->asegment('url');
        
        // So there we have it. The parameters have been passed
        // from func1 to func2 via the URI. :)
        
    }
}

// End of file: test.php
// Location: ./system/application/controllers/test.php

And the view:

./system/application/views/test.php
Code:
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Test&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form action="test/func1" method="POST"&gt;
            &lt;input type="text" name="title" value="" /&gt;&lt;br />
            &lt;input type="text" name="url" value="" /&gt;&lt;br />
            &lt;input type="submit" name="submit" value="Submit" /&gt;
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;

The index() method just displays the page, when it's submitted, it's sent to the func1() method, which processes it, and then redirects to func2 with a new URI. The func2() method converts the URI arguments back into their original form again, and displays them.

Does that make sense now?

That exact code doesn't work for me, as my server doesn't allow encoded slashes. I have created a workaround, but I've found a bug in it which I will fix tomorrow. Let me know if it doesn't work for you.




Theme © iAndrew 2016 - Forum software by © MyBB