0

I have two table in database. in table their is my all divisions which are shown in dropdown options. In second table, i submit my division name in division field. I want value in division field show as a default value in dropdown.here is my code..

here is my first query by which all division are comes

   $all_customers = mysql_query("select * from `supp_customers`");
    <select name="division" id="c_name" required class="form-control" onchange="my_function(this,<?php echo $users['id']; ?>)">

        <option value="">All Divisions</option>

        <?php while($customer = mysql_fetch_array($all_customers)){ ?>
        <option value="<?php echo $customer['name']; ?>"><?php echo $customer['name']; ?></option>
        <?php } ?>
   </select> 

and my second query

 $pro_qry = mysql_query("SELECT * FROM `supp_average_notes` where `id`='$id'");
 $div_query = mysql_fetch_array($pro_qry);
 $dive_query['division'];
?>  

i Want to set $div_query['division'] as a default in dropdown. How it can be possible

  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Oct 05 '16 at 10:28
  • Can we assume you want to do this when a user has selected something from an HTML form about which we have absolutely no knowledge of at all? – RiggsFolly Oct 05 '16 at 10:38

1 Answers1

0

In your while loop, you should compare the division to the current division. If it's the same, select the option. I would recommend you to

  • do what @RiggsFolly says,
  • look into a PHP framework; it will take a lot of work out of your hands and add security, e.g. Laravel, Yii or Symfony,
  • use id's in your select option values instead of names,
  • use alternative code style in views like below,
  • improve your variable naming so the name states what it holds.

.

<?php foreach ($divisions as $division): ?>
    <?php if ($division['id'] == $currentDivision['id']) $selected = ' selected="selected"'; else $selected = ''; ?>
    <option <?= $selected ?> value="<?= $division['id'] ?>">
        <?= $division['name'] ?>
    </option>
<?php endforeach ?>
John
  • 177
  • 2
  • 10