简体   繁体   中英

How to pass value in URL using codeigniter and $_GET it in a public function?

How do you add value in URL using codeigniter and $_GET it in a public function? So this is the button directed to the controller public function

 <a href="<?php echo base_url() ?>asset_management/editpc?$comp_id=<?php echo $comp_id?>" class="btn btn-warning btn-xs" ><i class="fa fa-edit"></i></a>

and this is what the public function in the controller looks like:

public function edit_pc(){
           if(isset($_GET['comp_id'])){
           $comp_id=$_GET['comp_id'];
           }
           $this->load->view('getcomp',$comp_id);

       }

is this the correct way of doing it?

you can do the it in codeigniter way

<a href="<?php echo base_url(); ?>asset_management/editpc/<?php echo $name; ?>/<?php echo $value; ?>" class="btn btn-warning btn-xs" ><i class="fa fa-edit"></i></a >

$name is the label you want to pass (company_id) and $value is the value of label ( company_id value ) once outputted the above like will look like the following

<a href="http://yoursite.com/asset_management/editpc/company_id/5" class="btn btn-warning btn-xs" ><i class="fa fa-edit"></i></a >

now change your function

public function edit_pc($name='',$value='')
{
   $data[$name]= $value;
   $this->load->view('getcomp',$data);
}

Use your URI segments as parameters to the base_url() call and - emphasized text as suggested in another answer on this page - use the Codeigniter way for URLs: controller/method/parameter . If $comp_id = 4; then this line

<a href='<?php echo base_url("asset_management/editpc/$comp_id"); ?>'
    class="btn btn-warning btn-xs" ><i class="fa fa-edit"></i></a>

would produce a link to http://example.com/asset_management/editpc/4

The controller's function then becomes

//function takes an argument which comes from the last segment of the URL
public function edit_pc($comp_id)
{
  //create an array to pass variables to the the view 
  $data['comp_id'] = $comp_id;

   $this->load->view('getcomp',$data);
   }

The view will use the var $comp_id . If we had used $data['some_other_name'] then the view should look for and use $some_other_name

There are a few issues with your code here, what specifically are you asking about? Are you receiving any errors?

You should end your lines with ; in php:

<?php echo base_url(); ?>

&

<?php echo $comp_id; ?>

Also your URL structure doesn't appear to be correct, unless "editpc" is a file directory and the index.php there is set to call the edit_pc() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM