Home > Mobile >  Duple select from database in same table but diffent proposes
Duple select from database in same table but diffent proposes

Time:04-28

First option of select must be the name referring to the ID. The remaining select options are the remaining names

<select  name="client_id">
  <?php
  $sel_client_detail="Select * from client WHERE client_id=".$id."";
  $result_detail = mysqli_query($con,$sel_client_detail);

  while($new_record_row = mysqli_fetch_assoc($result_detail)) { ?>
    <option selected><?php echo $row['nome'];?></option>
  <?php };?>

  <?php
  $sel_client="Select * from client";
  $result = mysqli_query($con,$sel_client);
  ?>
  <option>-----------</option>

  <?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
    <option><?php echo $new_record_row['nome'];?></option>
  <?php };?>
</select>

Output:

<select>
<option selected> Izzi (current ID name)</option> 
<option> ____________</option> 
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>
<option> Other existing clients</option>  
</select>

CodePudding user response:

Here's a different, but more conventional, approach to this common scenario:

Why not just make the chosen ID selected when you get to it in the list? Then it will still show to the user first. It's more efficient than having two separate queries.

Like this:

<select  name="client_id">
  <?php
  $sel_client="Select * from client";
  $result = mysqli_query($con,$sel_client);
  ?>
  <option>-----------</option>

  <?php while($new_record_row = mysqli_fetch_assoc($result)) { ?>
    <option <?php ($new_record_row["client_id"] == $id ? "selected": ""); ?> ><?php echo $new_record_row['nome'];?></option>
  <?php };?>
</select>

CodePudding user response:

If you want the user to be first in your option list just run the query once and build the HTML parts in 2 seperate strings. Then once the loop is complete put them together and echo them

<?php

echo '<select  name="client_id">';

$itsme = '';
$others = '<option>-----------</option>';

$sql = "Select * from client";
$result = $con->query($sql);

while($row = $result->fetch_assoc()){
    if ( $id == $row['id'] ) {
        $itsme = "<option selected="selected">$new_record_row[nome]</option>";
    } else {
        $others  = "<option>$new_record_row[nome]</option>";
    }
}
// put the option tags together in the order you specified

echo $itsme . $others . '</select>';
  • Related