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

Asked

Viewed 1,341 times

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, to be able to call them (because they will enter in the customization of the site) . But I have tried in every way (ex: if(isset($_GET['color']))). And it always returns that these values do not exist, even though they are passed in the url. This is the code of mine 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 this case the values passed by parameter would replace $client->click2call_color and $client->click2call_imagethat are the standard.

There’s something wrong with the code?

If not, how could I make this check?

  • are you using any framework? what kind of view is this? You can also put the view trehco in your question?

  • Codeigniter usage. I added the snippet as requested. @Gebender

1 answer

1


From what I understand, you are wanting to save a certain setting in the session.

But, there is an error at the time that data is being saved in session.

I think that would be the weather:

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

No need to create this INDEX => click2call inside $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 you would solve your problem.

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

print_r($configuracoes);

// Array(
//     [image] => img.jpg,
//     [color] => #fff
// )
  • David, where do I add this last piece of code?

  • Probably just below the Setimageandcolor function()

  • Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.