Populate Dropdown From Database And Set Default Value May 08, 2024 Post a Comment Right now I have a working solution for populating an HTML /-dropdown with the content through PHP/MYSQLI from my database: listoption. The database: DASolution 1: You can do that like given below:<?php $result = $mysqli->query("select * from listoption"); $id = ($_GET['id'])? $_GET['id'] : ''; echo "<selectid='list'name='list'>"; while ($row = $result->fetch_assoc()) { $listoption_item = $row['listoption_item']; $sel = ($id == $row['id'])? 'selected="selected"':''; echo '<optionvalue="'.$listoption_item.'" '.$sel.'>'.$listoption_item.'</option>'; // $sel will deside when to set `selected` } echo "</select>"; ?>CopySolution 2: You can rewrite the code as follows:<?php$id = $_GET['id']; $select = ""; $result = $mysqli->query("select * from listoption"); echo"<select id='list' name='list'>"; while ($row = $result->fetch_assoc()) { $row_id = $row['ID']; if($row_id == $id){ $select = "selected"; } $listoption_item = $row['listoption_item']; echo'<option value="'.$listoption_item.'" selected="'.$select.'">'.$listoption_item.'</option>'; } echo"</select>"; ?>CopySolution 3: Use the following code:-<?php$selectedId = isset($_GET['id'])?$_GET['id']:0; $result = $mysqli->query("select * from listoption"); echo"<select id='list' name='list'>"; while ($row = $result->fetch_assoc()) { $listoption_item = $row['listoption_item']; echo'<option value="'.$listoption_item.' .(($selectedId>0)?:" selected ":"").'">'.$listoption_item.'</option>'; } echo "</select>"; ?> Copy Share Post a Comment for "Populate Dropdown From Database And Set Default Value" Top Question Firefox, Chrome, Safari Have Grey Background For Mp4 Html5 Video Any video (that I can make) with a white background becomes… Red Border Still Appears On Inputs After Resetting An HTML 5 Form Firefox I have an HTML 5 form! Great, I'm excited! I decide to … Svg Animation And Firefox I'm struggle with SVG animation drawing, which is work … How To Save Html5 Geolocation Data To Python Django Admin? Is it possible to save the javascript html5 geolocation lat… How To Hide/show Tabs Using Jquery I have four tabs.On loading only the first tab is opened an… Form To Iframe Submission - How Do You Reset The Form After File Upload? I have a form that submits the results to an iframe. The su… How To Limit The Html Table Records In Each Page I am populating a table in my jsp which has more than 20 re… How To Display A Python String As Html In Jupyter Notebook In IPython notebook, I used to be able to display a python … How To Add Placeholder Field? I've been trying to add placeholder in input type='… Bounce Animation On A Scaled Object What is the best way to have an object scale and then perfo…