Re: populate form input option dropdown box from existing data

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2009-06-17 at 10:01 -0400, PJ wrote:
> Ford, Mike wrote:
> > On 16 June 2009 20:48, PJ advised:
> >
> >> Now, I was happy to learn that it is simpler to populate the
> >> insert new
> >> books page dynamically from the db. Much shorter & neater.
> >> It looks to me like the best solution for the edit page is
> >> close to what
> >> Yuri suggests.
> >> Since the edit page is very similar to the insert new books page, I
> >> merely need to populate the Select options box slightly differently.
> >> This is the code to populate the insert page:
> >> <select name="categoriesIN[]" multiple size="8">
> >> <?php
> >> $sql = "SELECT * FROM categories";
> >> if ( ( $results = mysql_query($sql, $db) ) !== false ) {
> >> while ( $row = mysql_fetch_assoc($results) ) {
> >> echo "<option value=", $row['id'], ">", $row['category'],
> >> "</option><br />"; }
> >> }
> >> </select>
> >>
> >> The problem nowis to find a way to add a conditional clause above that
> >> will insert the option="selected" in the output.
> >> The input for this comes from:
> >> // do categories
> >> $sql = "SELECT id, category FROM categories, book_categories
> >> WHERE book_categories.bookID = $idIN &&
> >> book_categories.categories_id = categories.id";;
> >> if ( ( $results = mysql_query($sql, $db) ) !== false ) {
> >> while ( $row = mysql_fetch_assoc($results) ) {
> >> echo$row['id'], "<br />";
> >> }
> >> }
> >>
> >> This may return any number of category ids so the problem is to figure
> >> out a way to pass the ids from the above code to the right ids in the
> >> first code above. How & what do I search to match the two ids?
> >
> > Well, if I'm understanding your queries correctly, you need to compare
> > the two sets of $row['id'] from the two queries above -- so your first
> > query should be the second one above ("SELECT id, category FROM ..."),
> > and you need to save the ids it returns for use in the loop which emits
> > the <select>s. This can be done by replacing the "echo $row['id']" with
> > "$selected_ids[] = $row['id']". Now you have an array of the selected
> > ids which you can use in your in_array(). So your finished code is going
> > to look something like this:
> >
> > <select name="categoriesIN[]" multiple size="8">
> > <?php
> > // do categories
> > $selected_ids = array();
> > $sql = "SELECT id, category FROM categories, book_categories
> > WHERE book_categories.bookID = $idIN &&
> > book_categories.categories_id = categories.id";
> > if ( ( $results = mysql_query($sql, $db) ) !== false ) {
> > while ( $row = mysql_fetch_assoc($results) ) {
> > $selected_ids[] = $row['id'];
> > }
> > }
> > $sql = "SELECT * FROM categories";
> > if ( ( $results = mysql_query($sql, $db) ) !== false ) {
> > while ( $row = mysql_fetch_assoc($results) ) {
> > echo "<option value=", $row['id'],
> > (in_array($row['id'], $selected_ids)?" selected":""),
> > ">", $row['category'],
> > "</option>\n";
> > }
> > }
> > ?>
> > </select>
> >
> > Hope this helps.
> It does, indeed. This confirms my inexperienced conclusion that
> in_array() does not work on associative arrays per se; it works on
> simple arrays and I just don't have the experience to think of
> extracting only the id fields.
> I actually am using a slightly more complicated if else statement which
> works.
> Also, the other problem was the option selected definition required
> Shawn's clarification
> <select name='component-select' multiple ... which now highlights the
> selected fields.
> In all my searches (horrendously wasted time) I did not find any mention
> of "component-select" either in php.net or w3c.org (I don't think my
> queries on Google brought up anything from php.net) but w3c.org did and
> I had looked at the page but somehow missed it.
> I'm going to have to look at the way I search things. When you are
> looking for something specific, other, even relevant, solutions seem to
> get screened out.
> 
> Anyway, I learned quite a bit, here.
> Thank you very, very much, gentlemen.
> PJ
> 
> -- 
> Hervé Kempf: "Pour sauver la planète, sortez du capitalisme."
> -------------------------------------------------------------
> Phil Jourdan --- pj@xxxxxxxxxxxxx
> http://www.ptahhotep.com
> http://www.chiccantine.com/andypantry.php
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
in_array() does work with associative arrays, I used it myself before!
Don't forget, it only attempts to match the value in an associative
array, not the key.

Thanks
Ash
www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux