mysql - How to retrieve all data from database using select option in php? -
so have table
database
want echo out
using select
option
. i'm having trouble because have option
user can add more options , don't know how set value of it. can me guys? thanks!
here current code:
<?php while(mysqli_stmt_fetch($stmt)) { ?> <select class="form-control input-sm"> <option value="able seaman" <?php if($boiler_size == 'able seaman') { echo "selected"; } ?>>able seaman</option> <option value="ako ito" <?php if($boiler_size == 'ako ito') { echo "selected"; } ?>>ako ito</option> <option value="apprentice" <?php if($boiler_size == 'apprentice') { echo "selected"; } ?>>apprentice</option> </select>
you have count of rows has been selected db since if count 0 throwing error in while loop
or foreach
preferred usage showing dynamic select options.
hence showing of dynamic select options need following process.
<?php $query="select * `tablename` order `id` desc"; $stmt = mysqli_query($conn,$query); $count = $stmt->num_rows; ?> <select class="form-control input-sm"> <?php if($count==0) { echo '<option value="">no datas have been created yet</option>'; } else { while($fetch = $stmt->fetch_assoc()) { ?> <option value="<?php echo $fetch['id']; ?>"><?php echo $fetch['name']; ?></option> <?php } } ?> </select>
bu using above codes can fetch n-numbers of option values db , can display easy without confusions. option value repeating till loop ends , after alone select
end . if db has n-number
while loop repeat n-number of times , end after that.
Comments
Post a Comment