Member Avatar for iamthwee

As title states, I need help using both query strings and segment urls. Any ideas I'm at a loss?

Recommended Answers

All 7 Replies

I dont think its possible. I've tried before but not luck. I had to use $_GET param.

Member Avatar for iamthwee

Anyone else, I have an activation code which is emailed to the user as a link. Then I need to read it as a $_GET request.

There is no way around it. I need to utilise GET, but at the same time use the segement url structure.

For example /view/method in other parts of my code. This is kinda the standard.

I'm not sure I've understood but you can use both at the same time. For example you have a method like this one:

public function activation($id)
{
    $data = array(
        'id'   => $id,
        'code' => $this->input->get('code', true)
    );

    $this->load->view('signup/activation', $data);
}

And call the url in this format:

http://localhost/signup/activation/1?code=random_string_123

The important is to set to TRUE the allow_get_query in the config file, so that the appended query string will be regularly processed:

$config['allow_get_array'] = TRUE;
commented: spot on +14

by default uri_protocol is set to auto, so you can use /controller/method?key=238ojhqf8e23oijrqf098u23&user=123443&software=paid

Then just catch the with the global $_GET method in your controller

commented: ty +14
Member Avatar for iamthwee

Thanks guys, setting the config['allow_get_array'] to true and default uri to auto worked with cereal's example.

So one more question...

let's say I have a view and inside I want to build a urlquery string:

<a href='localhost/product/id/1?version=12&name=mydoc'>click me </a>

How would I do this and then process the GET variables (version and name)

I'm looking at the 'anchor function' in the url helper but it fails to work.

The first argument of the anchor() function can accept an array or a string, so:

$segments = array(
    'product',
    'id',
    '1',
    '?version=12&name=mydoc'
);

# with array
echo anchor($segments, 'click me');

# with string
echo anchor('/product/id/1?version=12&name=mydoc', 'click me');

the difference between the two is that the former will output a trailing slash after the $id (with value 1) segment:

# version 1
http://localhost/product/id/1/?version=12&name=mydoc

While the latter no:

# version 2
http://daniweb.ci/product/id/1?version=12&name=mydoc

but, in both cases it should work fine, the point is to include the ? in the segment.

If you want to generate only the second version of the links through an array, then this could be done by extending the url helper, for example by simply exploding the string at the ? character and removing the right slash. In application/helpers create the file MY_url_helper.php and paste this:

<?php

if ( ! function_exists('my_anchor'))
{
    function my_anchor($uri = '', $title = '', $attributes = '')
    {
        $title = (string) $title;

        if ( ! is_array($uri))
        {
            $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
        }
        else
        {
            $site_url = site_url($uri);
        }

        if ($title == '')
        {
            $title = $site_url;
        }

        if ($attributes != '')
        {
            $attributes = _parse_attributes($attributes);
        }

        # new edit
        $querystring = explode('?', $site_url);
        $querystring[0] = rtrim($querystring[0], '/');
        $site_url = implode('?', $querystring);
        # stop edit

        return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
    }
}

Then from the view call the new function my_anchor() as submit the same arguments of the anchor() helper:

$segments = array(
    'product',
    'id',
    1,
    '?version=12&name=mydoc'
);
echo my_anchor($segments, 'click me');

It will output:

http://daniweb.ci/product/id/1?version=12&name=mydoc
commented: comprehensive answer +0
Member Avatar for iamthwee

Perfect cereal ^^. Seeing as the first example works for me I won't need to extend the class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.