0

This will echo dropdown value take from database that

<select name="PACKAGE_ID" id="PACKAGE_ID" ng-model="FormData.Phases" class="form-control" required>

<?php 
$result=mysqli_query($conn, "select * from unifi WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {
?>
    <option name="UNIFI" value="<?php echo $row["PACKAGE_NAME"]; ?>">
        <?php echo $row[ "PACKAGE_NAME"]; ?>
    </option>
<?php } ?>

<?php $result=mysqli_query($conn, "select * from streamyx WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
    <option name="STREAMYX" value="<?php echo $row["PACKAGE_NAME"]; ?>">
        <?php echo $row[ "PACKAGE_NAME"]; ?>
    </option>
<?php } ?>


<?php $result=mysqli_query($conn, "select * from webe WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
    <option name="WEBE" value="<?php echo $row["PACKAGE_NAME"]; ?>">
        <?php echo $row[ "PACKAGE_NAME"]; ?>
    </option>
<?php } ?>

<?php $result=mysqli_query($conn, "select * from dome WHERE STATUS!='DELETE' ORDER BY PACKAGE_NAME ASC");
while ($row=mysqli_fetch_assoc($result)) {?>
    <option name="WEBE" value="<?php echo $row["PACKAGE_NAME"]; ?>">
        <?php echo $row[ "PACKAGE_NAME"]; ?>
    </option>
<?php } ?>

below is how im taking value from database based on ticket id

 $sql = "SELECT * FROM cusinfo WHERE TICKET_ID = '".$strid."' ";
 $query = mysqli_query($conn,$sql);
 $result=mysqli_fetch_array($query,MYSQLI_ASSOC);

I want to echo selected value for eg.<<< $result["PACKAGE_ID"]==$row[ "PACKAGE_NAME"] echo selected >>

  • http://stackoverflow.com/questions/39070359/how-to-set-an-option-from-multiple-options-or-array-with-different-values-to-vie – independent.guru May 04 '17 at 01:05
  • Above link doesn't provide the best approach because it repeatedly redeclares the `$select` variable which is only possibly used once before it is overwritten. The best practice is not to declare a variable that will only be used once. My answer doesn't take the unnecessary step of declaring a variable just to echo it. – mickmackusa May 04 '17 at 01:23
  • @mickmackusa The variable is set with each iteration of a loop, it's the cleanest and most practical way of achieving the result – independent.guru May 04 '17 at 01:36
  • its okay guys im new in coding so maybe my question is not explain briefly im just explain it based on my knowledge sorry guys – Lokman Hakim May 04 '17 at 01:47

1 Answers1

1

Because you are using the $result variable many times with subsequent queries, you will need to preserve $result["PACKAGE_CATEGORY"] as a new variable, like this:

$selected=$result["PACKAGE_CATEGORY"];

You can write inline condition statements like this:

echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"",($row[ "PACKAGE_NAME"]==$selected?" selected":""),">{$row["PACKAGE_NAME"]}</option>";

This means if the condition is true then selected will be echoed, if false an empty string will be echoed.

The same technique on multiple lines would look like this:

echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"";
    echo $row["PACKAGE_NAME"]==$selected?" selected":"";
echo ">{$row["PACKAGE_NAME"]}</option>";

If you don't want to use an inline condition here is the standard syntax:

echo "<option name=\"WEBE\" value=\"{$row["PACKAGE_NAME"]}\"";
    if($row["PACKAGE_NAME"]==$selected){
        echo " selected";
    }
echo ">{$row["PACKAGE_NAME"]}</option>";
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • can i write like this ? `code` echo "";`code` – Lokman Hakim May 04 '17 at 01:21
  • You can set it to whatever you wish. The method is the same. I am only following the content in your question. If you question is not right, please edit your question. – mickmackusa May 04 '17 at 01:21
  • still cant selected the value im so confuse – Lokman Hakim May 04 '17 at 01:25
  • @LokmanHakim Every new query is overwriting the last fetched results, you will need to declare unique variables for each result set if you plan to use them after a new query result is generated. You do not mention `$result["PACKAGE_CATEGORY"]` in your question, I don't know where this value is coming from. – mickmackusa May 04 '17 at 01:26
  • sorry i cant understand can u answered it based on my code – Lokman Hakim May 04 '17 at 01:28
  • @LokmanHakim Because you are re-using the same variable names, your earlier values WILL BE overwritten by new query results. You must preserve your `$result["PACKAGE_CATEGORY"]` values as `$selected=$result["PACKAGE_CATEGORY"];` then you can repeatedly use it in your condition statements. – mickmackusa May 04 '17 at 01:36