How to select an option in select in a less laborious way?

Asked

Viewed 482 times

5

I have a page called editarpublicacao.php who receives the id through GET.

One of the two <select> for example contained on the page, is similar to that of companies:

<select name="company" data-placeholder="Selecione" class="form-control chosen-select" id="cmbCompany" required data-parsley-errors-container="#company-errors">
  <option value="" selected disabled hidden>Selecione</option>
  <?php 
  $companies = $searchSQL->fetchAll(PDO::FETCH_ASSOC);
  foreach ($companies as $empresas):
  ?>
    <option value="<?php echo $empresas['id_Company'];?>"><?php echo $empresas['razaoSocial']; ?></option>
  <?php endforeach; ?>
</select>

And I show all the companies through PHP using the while in that .

But for example, if I receive $_GET['id'], one id value 12,

How to show all companies and select one of them? In an example, id == 12 has to be automatically selected.

If the id == 12 was the so-and-so, I had to show all the companies and leave the so-and-so selected, like in that snippet I did:

<select name="select">
  <option value="valor1">Valor</option> 
  <option value="valor2" selected>Fulano</option>
  <option value="valor3">Valor 3</option>
</select>

I have the query that selects all companies:

$searchSQL = $pdo->prepare("SELECT id_Company, razaoSocial FROM tbl_company ORDER by razaoSocial ASC");
$searchSQL->execute(); 

How to do this, how to show all companies and select one of them, which has the same id as the one received by $_GET?


I saw in this [link][1] that I can do so:

<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>

There are problems to do so, what if I had 1000 records? Isn’t there an easier way to do it? And how can I do it?

  • You want to return from the bank only 1 company, for example, the id value 12?

  • No @Dvdsamm I want all companies to return, but one of them == 12 has to be selected.

  • Got it. The form you quoted in the question works, but your concern is if there are too many records, getting a huge select.

  • So that way I don’t want to, isn’t there a simpler way to do it? See the edition I made, improved?

  • The simplest way is the same as you asked the question: <option value="<?php echo $empresas['id_Company'];?>"<?=$empresas['id_Company'] == '$_GET['id']' ? ' selected="selected"' : '';?>><?php echo $empresas['razaoSocial']; ?></option>. I understand your concern is because this way the <select> will have <option> too. In this case, I think a common text field that looks for a company dynamically when typing its name (or part of it) is feasible.

  • It cannot be text field, because it is an edit page and has to be a select that shows all companies and automatically select the company with id == $_GET.

  • So I see no other way than the one I showed in the last comment.

  • Also, I believe you don’t have @Dvdsamm

  • 1

    Even if I did it differently, I would have to pull ALL 1000 companies from the bank. It would change 6 by half dozen.

  • What you could do, to charge the database less, was just pull the record with the ID and put in the <select>. If the company has to be changed, when the person clicks the <select> to see the options, then he would pull all the other records via Ajax. This can be interesting because, in addition to saving the database by pulling a large number of records every time you open the page, the company will not always be changed when editing the data.

Show 5 more comments

1 answer

1

I believe this can solve your problem, will leave as Selected the one who has the ID equal to the GET id

<select name="company" data-placeholder="Selecione" class="form-control chosen-select" id="cmbCompany" required data-parsley-errors-container="#company-errors">
  <option value="" selected disabled hidden>Selecione</option>
  <?php 
  $companies = $searchSQL->fetchAll(PDO::FETCH_ASSOC);
  foreach ($companies as $empresas):
  if($empresas['id_Company'] == $_GET['id']){
  ?>
    <option value="<?php echo $empresas['id_Company'];?>" selected><?php echo $empresas['razaoSocial']; ?></option>
  <?}else{ ?>
    <option value="<?php echo $empresas['id_Company'];?>"><?php echo $empresas['razaoSocial']; ?></option>
  <?}?>
  <?php endforeach; ?>
</select>

Browser other questions tagged

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