Jump to content

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


robertlob
Go to solution Solved by Psycho,

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
 
 
 
 
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.