Jump to content

filling a form list/menu with data from an sqlite database


robertlob

Recommended Posts

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
 
 
 
 

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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.