robertlob Posted May 24, 2014 Share Posted May 24, 2014 I am trying to convert and old website from mysql database to sqlite. One of the chores it must do is collect information from the database and put it in a list/menu select box on a page so the user can choose which item to pursue. In the following (incomplete) snippit, I am doing something incorrectly because the sql query does get the proper information (I can put it in a table on the page just fine). But I'm having trouble getting the information into the select options on a list/menu. It appears to be putting them all, one after the other in the first option spot. The last one seems to be the only one of 6 or 7 that shows up. It's been 10 or 12 years since I've messed with php, so I think I'm way behind... any help would be appreciated. --------------------------- <form action="sqlPropDisplay.php" method="post" id="Residential"> <select name="ResidentialListMenu" size="7" id="ResidentialListMenu"> <?php try { //open the database $db = new PDO('sqlite:bilgerdb'); $result = $db->query('SELECT PropID, PropLocation FROM property WHERE PropCategory = "Residential" ORDER BY PropListingDate'); } catch (PDOException $e) { print 'Exception: ' .$e->getMessage(); } foreach($result as $row) $rec = $row['PropID']; $loc = $row['PropLocation']; ?> <option value=<?php echo $rec ;?> selected><?php echo $loc ;?></option> </select> //submit button code omitted. I think I can handle that when I get to it ---------------------------- Thank you, Robert Quote Link to comment https://forums.phpfreaks.com/topic/288747-filling-a-form-listmenu-with-data-from-an-sqlite-database/ Share on other sites More sharing options...
fastsol Posted May 24, 2014 Share Posted May 24, 2014 Couple things I see, you're missing your opening anclosing {} for the foreach loop, so it's most likley including more code in the loop than it should. Second, you are telling the option tag that EVERY option is selected, that's not right and not really possible without using the "multiple" attribute. Quote Link to comment https://forums.phpfreaks.com/topic/288747-filling-a-form-listmenu-with-data-from-an-sqlite-database/#findComment-1480748 Share on other sites More sharing options...
Solution Psycho Posted May 25, 2014 Solution Share Posted May 25, 2014 Couple things I see, you're missing your opening anclosing {} for the foreach loop, so it's most likley including more code in the loop than it should. No, it's including less code. WHen you don't use {} for control structures (loops, if, etc.) it only assumes the one line immediately after the control structure is associated with it. <?php try { //open the database $db = new PDO('sqlite:bilgerdb'); $query = "SELECT PropID, PropLocation FROM property WHERE PropCategory = 'Residential' ORDER BY PropListingDate"; $result = $db->query($query); $options = ''; foreach($result as $row) { $options .= "<option value='{$row['PropID']}'>{$row['PropLocation']}</option>"; } } catch (PDOException $e) { print 'Exception: ' .$e->getMessage(); } ?> <form action="sqlPropDisplay.php" method="post" id="Residential"> <select name="ResidentialListMenu" size="7" id="ResidentialListMenu"> <?php echo $options; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/288747-filling-a-form-listmenu-with-data-from-an-sqlite-database/#findComment-1480763 Share on other sites More sharing options...
fastsol Posted May 25, 2014 Share Posted May 25, 2014 Oh yeah, duh. I know that, not sure what I was thinking when I was typing. Quote Link to comment https://forums.phpfreaks.com/topic/288747-filling-a-form-listmenu-with-data-from-an-sqlite-database/#findComment-1480767 Share on other sites More sharing options...
robertlob Posted May 25, 2014 Author Share Posted May 25, 2014 (edited) Gentlemen- Thank you. Fastsol for reminding me I forgot the {}; and Psycho for straightening us both out. It's working fine now. I appreciate your help. -Robert Edited May 25, 2014 by robertlob Quote Link to comment https://forums.phpfreaks.com/topic/288747-filling-a-form-listmenu-with-data-from-an-sqlite-database/#findComment-1480783 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.