views:

1339

answers:

2

I'm new to CodeIgniter and have just discovered the difficulties using the GET method of passing variables via the URL (e.g. domain.com/page.php?var1=1&var2=2).

I gather that one approach is to pass the variables in the URI segments but haven't quite figured out how to do that yet as it seems to create the expectation of having a function in the controller named as the specific URI segment????

Anyway Instead of using GET I've decided to use POST by adapting a submit button (disguised as a link) with the variables in hidden input fields. I've created the following solution which seems to work fine, but am wondering whether I'm on the right track here or whether there is an easier way of passing variables via a link within CodeIgniter?

I've created the following class in application/libraries/

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_variables {

function variables_via_link($action, $link_text, $style, $link_data) {
    $attributes = array('style' => 'margin:0; padding:0; display: inline;');
    echo form_open($action, $attributes);
    $attributes = array('class' => $style, 'name' => 'link');
    echo form_submit($attributes, $link_text);
    foreach ($link_data as $key => $value){
        echo form_hidden($key, $value);
    }
    echo form_close();
 }
}
?>

With the following CSS:

/* 
SUBMIT BUTTON AS LINK
adapted from thread: http://forums.digitalpoint.com/showthread.php?t=403667
Cross browser support (apparently).
*/
.submit_as_link {
background: transparent;
border-top: 0;
border-right: 0;
border-bottom: 1px solid #00F;
border-left: 0;
color: #00F;
display: inline;
margin: 0;
padding: 0;
    cursor: hand /* Added to show hand when hovering */
}

*:first-child+html .submit_as_link {  /* hack needed for IE 7 */
border-bottom: 0;
text-decoration: underline;
}

* html .submit_as_link {    /* hack needed for IE 5/6 */
border-bottom: 0;
text-decoration: underline;
}

Link then created using the following code in the VIEW:

<?php
$link = new C_variables;
$link_data=array('var1' => 1, 'var2' => 2);
$link ->variables_via_link('destination_page', 'here is a link!', 
'submit_as_link', $link_data);
?>

Thanks for your help...

+1  A: 

To be honest, creating a form to perform the job of hyperlinks is a bit of a semantic no no.

Codeigniter by default completely strips any $_GET parameters. So without enabling query strings in the config, you can't do the following:

http://my-domain.com/script/?param=1&amp;param2=foo

For a beginner, segment based URLs are a bit of a learning curve, but soon make sense. A good production example of how segment based URLs work in practice is Stack Overflow!

So if you wanted to copy Stack Overflow's question view page with the following URL in codeigniter:

http://stackoverflow.com/questions/2728978/codeigniter-passing-variables-via-url-alternatives-to-using-get

In your default controller create the following method:

public function questions()
{
  $question_id = $this->uri->segment(2);

  // now do something with our question_id
}

The third segment (question title slug) is effectively ignored. But you could grab it with the following:

$question_title = $this->uri->segment(3);

More information here: http://codeigniter.com/user_guide/libraries/uri.html

If you don't like the idea of having to name a method in your controller with the first URI segment. You could create a custom route in your routes configuration.

So, imagine you create a controller called questions_controller.php, and have a method called show_question_by_id(). To keep the /questions/1234/some-text-here style URI but handle it with the controller/method above, you could create the following route:

$route['question/(:num)'] = "questions_controller/show_question_by_id/$1";

More information here: http://codeigniter.com/user_guide/general/routing.html

If you wish to have an infinite number of parameters in your URL, or don't know what parameters to expect e.g. http://mysite.com/my_page/param1/12/param2/foo/param3/bar/param4/baz/another-param/xyz-123

You can split these into an associative array using the $this->uri->uri_to_assoc(1) URI method to get the following:

  [array]
(
    'param1' => '12'
    'param2' => 'foo'
    'param3' => 'bar'
    'param3' => 'baz'
    'another-param' => 'xyz-123'
)

You could then handle this exactly as if you were using the $_GET array. You can then combine this approach, with custom routes to give you practically any URI and application structure you like. You also get the benefit that each parameter and segment has been automatically cleaned up. It's a bit of a learning curve, and can seem like extra work to begin with, but is actually pretty flexible and helps you to build a well structured applicaition.

rbaker86
Thanks, this is the kind of help I was looking for, although I think it may take a little while to get my head round using the URI segments properly.
John Durrant
+1  A: 

Why on earth are you posting your CSS if you are struggling with Query Strings?

Madness.

Anyway, you can re-create access to the Query String in several ways in CodeIgniter.

uri_to_assoc()

Firstly there are the wonderful associative URI segments.

Example URL: http://example.com/controller/method/name/value/name2/value2

You can use the following code to access these two values:

$get = $this->uri->uri_to_assoc();
echo $get['name']; // value
echo $get['name2']; // value2

This of course is a replacement for query strings, but if you can use this do it.

parse_str()

If you have global code like a hook or MY_Controller you can re-populate $_GET with the following one-liner:

parse_str($_SERVER['QUERY_STRING', $_GET');

REQUEST_URI

Set your $config['uri_protocol'] to "REQUEST_URI" and enable quey strings further down. REQUEST_URI wont work on all servers, so this is not the most perfect method even though it is the easiest.

Phil Sturgeon
Thanks for your help, although I don't see where you're coming from with the 'Madness' comment?!? My post is about styling a submit box to appear as a link, seemed fairly sensible to include the CSS... Some of us might be less experienced in coding terms, but not necessarily mad...
John Durrant
styling the submit box? Are you sure you're on the right Q and A site?
Thorpe Obazee
Calm down John, just having a little fun. Was the post any help?
Phil Sturgeon
Not tried it yet Phil, but will do - thanks again.
John Durrant