Search something in a list and say whether it exists or not - Display Value, and amount

Asked

Viewed 66 times

2

Good afternoon, what is the easiest way to make a list in which it contains several items, for example meat, fish, bread etc, and from the variable $_GET know if the product exists or not ? I tried to create an array and do it with array_key_exist but I’m not able to. thanks

  • Are these products in a database? Mysql for example

  • It’s a simple exercise I’m trying to do, I’m putting the products in an array, and I want it when I do...php? Milk/ It says whether milk exists in the array or not

2 answers

1

If you want to do $_GET, consider adding multiple items in the same parameter’s input GET.

Behold:

url?itens[]=pão&itens[]=leite&itens[]=ovo

In PHP this would return:

 print_r($_GET['itens']); // ["pão", "leite", "ovo"]

In order for the url to look like the comment, you need to declare the form like this:

 <input type="text" name="itens[]" value="ovo" />
 <input type="text" name="itens[]" value="pão" />

To check, just save a list of expected values and compare like this:

 $valores = $_GET['itens'];

 ksort($valores);

 $obrigatorios = ['leite', 'ovo', 'pão']

 var_dump($valores === $obrigatorios);

Another way to do the check would be by using the function array_intersect:

count(array_intersect($obrigatorios, $valores)) > 0
  • Thanks for the help !

1


Do so then:

$produtos = array(
    'Leite' => array(
        'preco' => 1.5,
    ),
    'Chocolate' => array(
        'preco' => 10,
    ),
    'Pão' => array(
        'preco' => 3.5,
    ),
    'Sal' => array(
        'preco' => 0.5,
    ),
);
// EX: aceda a www.seusite.com?produto=Leite&quantidade=5 e depois:
if(isset($_GET['produto'])) {
    echo 'Preço por unidade: ' .$produtos[$_GET['produto']]['preco']. '<br>';
    if(isset($_GET['quantidade'])) {
        echo 'Preço total: ' .$produtos[$_GET['produto']]['preco']*$_GET['quantidade'];
    }
    else {
        echo 'Preço total: ' .$produtos[$_GET['produto']]['preco'];
    }
}
else {
    echo 'Produto indefinido';
}
  • worked out! That’s just what I needed! Thank you :)

  • Not at all @Mariocaeiro, if you really decided you can/should accept the answer. I’m glad you solved

  • Miguel, if I wanted to do otherwise, if the product were found, would I present the price, for example php? /product=Milk, output: Milk 1.90 ?

  • Ha I’ll edit the answer to that, give me 2 mins

  • Okay I’ll wait for the issue! Thanks !

  • Done @Mariocaeiro

  • Not wanting to abuse, You could add the sum in case you put something of the genre in the url . php? /product=Milk&quantity=5 and present in this case 5x the price'?

  • 2 mins @Mariocaeiro

  • Feito @Mariocaeiro , aceda por ex a aceda a www.seusite.com?produto=Leite&quantidade=5

  • Miguel, thank you so much for your help, I am taking my first steps in php and your help was precious and very direct, thank you for sharing your knowledge with me and your time! a big hug

  • No problem @Mariocaeiro, I’m glad I helped. I would ask you to edit the question now according to the developments we have had. Add this from multiplication and sff quantity, so that those who have the same doubt can see some coherence of questions/ answers

  • are you saying to add tags with multiplication amount etc? sorry I’m also in stackoverflow just a while ago

  • No no, add to the question you want also price, quantity and multiplication... Everything that has been developed that is not there... Tags are good @Mariocaeiro

Show 8 more comments

Browser other questions tagged

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