0

Helo,

I have a confussion and I need to get it solved in core php we used to create a url parameter through which we would be able to get values from the url so hwo this could be happend in codigniter for example

<a href="index.php?add=1">Add page</a>
//setting up a get request

if(isset($_GET["add"]) == 1) {
    //show ad page form 
} else {
    //show the whole page
}

how do this same condition would be done in codigniter?

4
Contributors
6
Replies
26
Views
1 Year
Discussion Span
Last Post by UK-1991
0

You can append the query string to the link and then access it through $_GET as in pure PHP or by using $this->input->get('keyword').

For example, you have controller Blog with method show($title_slug) and you want to append a query string to show the comments, your link would look like:

http://website.tld/blog/show/hello-world-title?comments=on 

in controller:

class Blog extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function show($title)
    {
        $comments = $this->input->get('comments');

        # other code ...
    }

}
0

$this->input->get(),
This method will return all get parameters,you need to set your variable name and get value.

Edited by pritaeas: Removed fake signature/self-promotion link.

0

I tried like this in the view file as i want to set condition there

<?php 
                if($this->input->get('parameter')) {
                    echo 'Parameter condition is running okay';
                }
                echo $this->input->get('parameter');
            ?>

 <?php echo anchor('admin/pages?parameter', 'Click Here'); ?>

But nothing happend for me though please let me know what I am ding wrong

1

You have to assign a value to parameter:

<?php echo anchor('admin/pages?parameter=value', 'Click Here'); ?>

Otherwise the IF condition fails, try:

var_dump($this->input->get('parameter'));
1
  1. Your view file and Controller function goes like below

        <html>
        <head></head>
        <body>
        <div id="container">
            <a href="index.php?add=1">Add page</a>
        </div>
        </body>
        </html>
    
     class Welcome extends CI_Controller {
            public function index(){
                  $add=(int) $this->input->get('add');
                  if(isset($add) && $add === 1) {
                      echo "show ad page form";              
                  } else {
                     echo "show the whole page";
                  }
            }
        }
    
  2. Now if you type "localhost/CodeIgniter_2.1.4" . it will show you "show the whole page" and if you call "localhost/CI2.1.4/?add=1" it will show you "show ad page form"

  3. i think passing condition directly in view is not good so you can go like below.

    class Welcome extends CI_Controller {
    public function index(){
    //setting up a get request
    $add=(int) $this->input->get('add');
    $data['add']=$add;
    $this->load->view('welcome_message',$data);
    }
    }

Edited by mangel.murti: Formatting Code

0

@mangel.murti && @cereal

Thank you both of your provided queries worked for me though

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.