Lee_Jones Posted March 6, 2021 Share Posted March 6, 2021 I have a column in my db called 'Sold' which is set to Binary. I need a dropdown in my form, that converst the binary into 'True', 'False' for the options, and also selects the one that is set in the db. This is for an edit form, so will then need to adapt it for a add form. This is my code, which doesnt seem to be working <?php $id = $_GET["id"]; $sqlSold = "SELECT Sold FROM Products WHERE idProducts = $id;"; $products = [ 'True' => true, 'False' => false ]; ?> <select name="Sold"> <option selected="selected">Choose one</option> <?php foreach($products as $item){ echo "<option value='strtolower($item)'>$item</option>"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/312252-form-select-with-binary-options/ Share on other sites More sharing options...
Barand Posted March 6, 2021 Share Posted March 6, 2021 You don't execute any queries and you don't have anything in your query string that references the 'sold' column. Nor do have a form or any form submission. So what are you expecting it to do? Quote Link to comment https://forums.phpfreaks.com/topic/312252-form-select-with-binary-options/#findComment-1584907 Share on other sites More sharing options...
Lee_Jones Posted March 6, 2021 Author Share Posted March 6, 2021 At this point i just want to create the dropdown, it sits inside a form, I have just posted this bit. Sorry if its a bit vague. Quote Link to comment https://forums.phpfreaks.com/topic/312252-form-select-with-binary-options/#findComment-1584913 Share on other sites More sharing options...
Barand Posted March 6, 2021 Share Posted March 6, 2021 What RDBMS are you using? (If it's MySQL, boolean is stored as tinyint) Try $products = [ 'True' => 1, 'False' => 0 ]; then foreach($products as $txt => $val){ echo "<option value='$val'>$txt</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/312252-form-select-with-binary-options/#findComment-1584915 Share on other sites More sharing options...
Lee_Jones Posted March 8, 2021 Author Share Posted March 8, 2021 Thanks Barand, Sorry its so basic, Im just starting out with PHP. To get the option to be selected accordign to the db value, i a trying the code below,and its not quite working. <?php $id = $_GET["id"]; $sqlSold = "SELECT Sold FROM Products WHERE idProducts = $id;"; $products = [ 'True' => 1, 'False' => 0 ]; ?> <select name="Sold"> <option selected="selected">Choose one</option> <?php foreach($products as $item => $val){ echo "<option value='strtolower($item)' if($val == $products ? 'selected=selected' : ''')>$item</option>"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/312252-form-select-with-binary-options/#findComment-1584951 Share on other sites More sharing options...
Barand Posted March 8, 2021 Share Posted March 8, 2021 (edited) Perhaps <?php $res = $pdo->prepare("SELECT Sold FROM Products WHERE idProducts = ?"); $res->execute([ $_GET["id"] ]); $sqlSold = $res->fetchColumn(); function productOptions($current) { $products = [ 'True' => 1, 'False' => 0 ]; $opts .= ''; foreach ($products as $txt => $val) { $sel = $val==$current ? 'selected' : ''; $opts .= "<option value='$val' $sel>$txt</option>"; } return $opts; } ?> <html> <body> <select name="Sold"> <option selected="selected">Choose one</option> <?= productOptions($sqlSold)?> </select> </body> </html> Edited March 8, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/312252-form-select-with-binary-options/#findComment-1584955 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.