Using data saved in a Session

Asked

Viewed 196 times

0

I modified my question because I was able to solve the problem of the session. But now I have another question. I have this code in Controller to capture the data passed by parameter and save them in Session:

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

The url with the passing of the parameters I’m using for testing is this: http://localhost/crm/trunk/click2call?color=4444&image=img.jpg.

So far, everything ok. Now going to the View. I made the following code to check if the data were saved:

<?php $result = $this->session->userdata('click2call'); 
 foreach ($result as $row) {
 print_r($row);
 } ?>

And I got as a result: Array ( [image] => img.jpg [color] => 4444 ). Right?

Now I want to know how I can use these values within the View. For example, I have this default html:

<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; ?>

Where, if there are these values saved in the session: Color will replace header color -> <header data-color="<?php echo $client->click2call_color; ?>">

and image will replace the image -> img src="<?php echo $client->click2call_image; ?>" alt="<?php echo $client->name; ?>"/>

How can I do this check and assign these values?

2 answers

2


Your session returns an array when it has multiple results: https://stackoverflow.com/questions/25518056/codeigniter-echo-session-multidimensional-array-variable

//view.php
<?php 
$sessao = $this->session-> userdata ('click2call'); 
if(!empty ($sessao){  
    echo "<header data-color=\"$sessao['client_id']['color']\">\n";
    echo "<img src=\"$sessão['client_id']['image']\" alt=\"$client->name\"/>\n";
}
else {
    echo "<header data-color=\"$client->click2call_color\">\n";
    echo "<img src=\"$client->click2call_image\" alt=\"$client->name\"/>\n";

}
  • Solved it, thanks man!

  • Or I could subscribe to the variable : <?php &#xA;$sessao = $this->session-> userdata ('click2call'); &#xA;if(!empty ($sessao){ $client_click2call_color = $sessao['color'];would be correct?

  • 1

    You can do that, yes!! I’m used to using the values right from the session, but it’s up to you (as long as it works rsrsrs)

  • Dude, I’m having a little problem here. The first few times it worked perfectly. But now you’re no longer subscribing to the view values. What can it be?

  • Got it, just needed to lock the color with # because the other was already coming from the database processed! Can you give me an idea? Instead of if(!empty ($sessao)){ that checks if it is empty, would have to check if it is different from fake. How do I do? Thanks!

  • Also checking a php variable: if(!$session), if($session != TRUE) or if($session == FALSE)

  • Thanks! And another, I can’t use the value directly. For example $session = $this->session->userdata('click2call'); &#xA;echo $session['color']; I can only do a foreach, but I can’t use it...

  • And so: echo $session->color; ??

  • Neither. And giving a print_r($Session) the result is simply Array.

Show 4 more comments

0

You can subscribe to the values like this:

$session = $this->session->userdata('click2call'); 
if($session != FALSE){
    if(isset($session['color'])) {
        $client->click2call_color = $session['color'];
    }
    if(isset($session['image'])) {
        $client->click2call_image = $session['image'];
    }
}

Browser other questions tagged

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