Re: [PHP] populate form input option dropdown box from existing data

[email protected] (PJ)
Newsgroups php.general
Message-ID <[email protected]>
jenai tomaka wrote:
> You can try like this,
>
> $row = stored data;
>  
> and write the options like this
> <option value="id" (id == $row ? "selected" : "") >xxxx</option>
>
>
> Yuri Yarlei.
>
> <http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmail&utm_medium=Tagline&utm_campaign=IE8>
Yuri, I'm still "wet behind the ears" on this so I don't quite
understand what you mean by "stored data" ;  and what does the "id"
(id==$row?"selected":"") mean?
I get the idea that this might be translated into something in the code
I have dreamed up - further down.

echo "<option value=", $row['id'], ">", $row['category'], "</option><br />";
I suppose that I must add an if clause to insert the selected option for
the categories that are relevant...


Gentlemen,
I have been diligently studying all this and have just returned my
attention to the list, so I've read the replies/inputs and now comment:

BTW, I had some problems with the multiple statement - it does not take
any parameters; it must simply be stated multiple without quotes.

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?

-- 
Hervé Kempf: "Pour sauver la planète, sortez du capitalisme."
-------------------------------------------------------------
Phil Jourdan --- [email protected]
   http://www.ptahhotep.com
   http://www.chiccantine.com/andypantry.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.