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?

Recommended Answers

All 6 Replies

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 ...
    }

}

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

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

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. 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);
    }
    }

@mangel.murti && @cereal

Thank you both of your provided queries worked for me though

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.