Check if a parameter passed by the url exists inside the View

1

I have this following code in my Controller :

public function SetImageAndColor($client_id) {
if(isset($_GET['color']) AND isset($_GET['image'])) {
$dados['click2call'] [$client_id] ['image'] = $this->input->get('image');
$dados['click2call'] [$client_id] ['color'] = $this->input->get('color');
$this->session->set_userdata('click2call', $dados);
  }
}

The goal is to save the parameters passed by the url (color and image) in the session. Correct?

After that, I need to check inside the View , if these parameters exist, in order to call them (because they will enter the site customization). But I've tried it anyway (ex: if(isset($_GET['color'])) ). And always returns that these values do not exist, even with them being passed in the url. This is the code for my View :

<header data-color="<?php echo $client->click2call_color; ?>">
<h1>
<?php if($client->click2call_image != ''): ?>
<img src="<?php echo $client->click2call_image; ?>" alt="<?php echo $client->name; ?>"/>
<?php else: ?>
<?php echo lang('click2call_title'); ?></h1>
<?php endif; ?>
</header>

In case the values passed by parameter would replace $client->click2call_color and $client->click2call_image which are the default.

Is there something wrong with the code?

If not, how could you do this verification?

    
asked by anonymous 24.03.2015 / 19:22

1 answer

1

So I understand, you're trying to save a certain configuration in the session.

But there is an error when data is being saved in the session.

I think the chain would look like this:

public function SetImageAndColor($client_id) {
    if(isset($_GET['color']) AND isset($_GET['image'])) {
        $dados[$client_id]['image'] = $this->input->get('image');
        $dados[$client_id]['color'] = $this->input->get('color');
        $this->session->set_userdata('click2call', $dados);
    }
}

You do not need to create this INDEX = > click2call within $dados .

Add a function to get the data saved in the session.

public function GetImageAndColor($client_id) {
    $dados = $this->session->get_userdata('click2call');

    if(!empty($dados) && isset($dados[$client_id])) {
        return $dados[$client_id];

    } else {
        return false;
    }
}

I think this is how it would solve your problem.

$configuracoes = $classNome->GetImageAndColor(1234);

print_r($configuracoes);

// Array(
//     [image] => img.jpg,
//     [color] => #fff
// )
    
24.03.2015 / 19:54