How to loop with input select PHP?

Asked

Viewed 787 times

1

I have an array named $c that I made through a explode need to count with the for to see if it is Selected. The problem is that it echo option 2 or how many times there are records. Does anyone have any solution to this?

foreach($interesse_compra as $ic)
{
   $c = explode(";",$ic->cidade);
}

for($i = 0; $i < count($c);$i++)
{
   foreach($cidades as $cidade)
   {
      $select = $cidade->f_cidade == $c[$i] ?  "selected" : "";
      echo "<option value='".$cidade->f_cidade."'$select>".$cidade->f_cidade."</option>";
   }

}
  • 1

    pq $c = explode(";",$ic->cidade); is inside the foreach? that way the count() will only be done in the last element of $interesse_compra.

  • Is it a multiple selection select? That is, you can have several options selected?

  • foreach is there because I use codeigniter. But this part is normal. $c is dividing into 3 parts. I do the counting. Only in the count it is doubling my clear and obvious options because it is remaking the same process based on $C[$i]. Got it @rray?

  • Yes it is a select of multiple choices... I am bringing the database data in the editing part and I need to know which ones were selected. Only they’re saved separated by Pono and comma. @Kaduamaral

  • Why the explode is inside a foreach? If it’s a loop, all that code shouldn’t be inside it too?

  • You can ask the question the content of $interesse_compra and $cidades? You can print them with var_dump($interesse_compra) and put output in question.

Show 1 more comment

2 answers

3


I made a simplified version of your code, from the point that $c contains all available cities and $cidades has the items to be selected.

A comparison is made between in_array() if present value($cidade) exists in $c case in point $select receives Selected, otherwise receives an empty stringa.

<html>
<body>
<form>
<select multiple size="8">
<?php
    $c = ['Rio Branco', 'Maceió','Macapá','Manaus','Salvador','Fortaleza','Brasília','Vitória','Goiânia', 'Curitiba'];
    $cidades = ['São Luís','Salvador','Cuiabá','Curitiba','Campo Grande','Belém','Maceió','Belo Horizonte'];

    foreach($cidades as $cidade){
        $select = in_array($cidade, $c)  ? $select = 'selected="selected"' : "";
        printf('<option value="%s" %s>%s</option>'. PHP_EOL , $cidade, $select, $cidade);
    }
?>
</select>
</form>
</body>
</html>

Example - phpfiddle

2

By the description of the problem, you need to check if the selected city belongs to your list of cities.

For this use the functions array_map() and in_array():

$interesse = 'Recife;Campinas;Alagoas';
$cidades = ['Rio de Janeiro', 'São Paulo', 'Salvador', 'Piracicaba', 'Belo Horizonte', 'Recife'];

$interesse_array = explode(';', $interesse);

array_map(function($c) use ($cidades) {
    $selecionado = in_array($c, $cidades) ? "selected" : "false";
    echo "<option value='$c' selected='$selecionado'>$c</option>\n";
}, $interesse_array);

Exit:

<option value='Recife' selected='selected'>Recife</option>
<option value='Campinas' selected='false'>Campinas</option>
<option value='Alagoas' selected='false'>Alagoas</option>

See example working on ideone.

Browser other questions tagged

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