Check if $_GET exists within the view

Asked

Viewed 2,017 times

0

Guys is the following, 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, so the client has the option to pass the image and color by parameter in the 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 the $_GET exist, it puts the image and the color selected by the client, if not, it puts the pattern?

  • Here you use $client->client_color and here you use $dados['client'] [$client_id] ['image'], that is, you’re using some framework and we don’t know what it is, or the right one should be $client->color;. I suppose it’s not a problem with GET. What framework is this? Something else the color is going to the attribute data-color, this attribute does nothing unless it is used by a Javascript library. You have viewed the source code, suddenly the color is in the attribute data-color, but the javascript library is the problem.

  • A simple if and else??

  • Exactly, I want to know how to mount if and Else within the html structure, whether or not to include $_GET data

2 answers

3

Use isset() it returns true if there is something

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
}
  • Good afternoon Stuney, he made it very clear that he wants to check the $_GET in the View. His point is to pass on the data from Controller to the View

  • @Guilhermenascimento, I already managed to make the passage to the view, my doubt is how to make the replacement of the image and color, if $_GET exists.

  • @Guilhermewermann Yes, I get it, but the answer here isn’t talking about the Controller and the View, so it’s not the problem, even if you managed to "pass", yet your question is whether the Controller passes data from the ELSE to the View, so the answer here doesn’t talk about it. It’s not a negative criticism, I’m just trying to help the AP improve the answer.

  • @Guilhermenascimento Okay, thank you!

2


Put a else 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.

  • Thanks bro, you helped a lot!

Browser other questions tagged

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