Check if $ _GET exists within the view

0

Galera is as follows, I have a standard html file, which contains:

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

...

But I did a function in the controller, for the client to have the option of passing the image and the color per parameter in url:

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

How do I check if $_GET exists, it puts the image and the color selected by the client, if not, does it set the default?

    
asked by anonymous 19.03.2015 / 20:41

2 answers

2

Put a else into the function and mount the default array:

private function SetImageAndColor($client_id) {
    if (isset($_GET['color']) AND isset($_GET['image'])) {
        $dados['client'] [$client_id] ['image'] = $this->input->get('image');
        $dados['client'] [$client_id] ['color'] = $this->input->get('color');
    } else {
        $dados['client'][$client_id] = array(
            'image' => 'aqui vai a imagem padrao',
            'color' => 'aqui vai a cor padrao'
        );
    }
    $this->session->set_userdata('client', $dados);
}

And in the view you simply display, without checking.

    
19.03.2015 / 21:08
2

Use isset () it returns true if there is something Home Example:

$var ="";
if(isset($_GET['suavar'])){

//Se existir o GET você atribui o valor, caso contrário, a variável fica valendo ""
$var =$_GET['suavar']
}

Remembering that $ _GET [] is an array variable, you can also use

if(!empty($_GET)){
//faça algo
}
    
19.03.2015 / 20:52