Re: populate form input option dropdown box from existing data

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

 



I snipped to make it short... continue at bottom...
> Step back from the code and consider the steps you need to perform...
>
> 1) Get an array of the categories, ideally in the form $cats[<catid>]
> = categoryname.
>
> 2) Get an array of the category IDs that should be selected, i.e.
> $selectedcats = array(3, 5, 7, 9).
>
> 3) Start the HTML select element
>
> 4) foreach ($cats as $id => $catname)
>
> 5) Determine whether it should be selected. e.g. $selected =
> (in_array($id, $selectedcats) ? 'selected="selected"' : ''.
>
> 6) Output the HTML option element, like <option value="$id"
> $selected>$catname</option>, escaping where appropriate.
>
> 7) End of loop, job done.
>
> If your code doesn't have that structure then you may want to consider
> starting again.
>   
I'm quite sure the structure is correct.
> Secondly, check that you're not using the same variable name twice.
I did find that in an instance of $id being repeated so I changed it to
$bid.
>  In
> one of your previous emails you used $selected to hold the array of
> selected categories, and in another you used it for the text to be
> inserted into the option element. The latter will blat over the former
> leading to no more than 1 option selected, and even then only if it's
> the first option displayed.
>   
The $selected were not mine... as I was using $ccc ; only started using
$selected a couple of hours ago.
> If you're still stuck please post more of your code in a single chunk
> including all the elements in my step-by-step above. The snippets
> you're currently posting are not giving us enough context to spot even
> the most common mistakes.
I'm including the relevant code:

// select categories for book to be updated
$sql = "SELECT id, category FROM categories, book_categories
        WHERE book_categories.bookID = '$bid' &&
book_categories.categories_id = categories.id";
if ( ( $results = mysql_query($sql, $db) ) ) {
  while ( $row = mysql_fetch_assoc($results) ) {
    $selected[] = $row['id'];
    }
  }
else $selected = Array( 0 => '0');
echo $selected;
print_r($selected);

$sql = "SELECT * FROM categories";
echo "<select name='categoriesIN[]' multiple size='8'>";
  if ( ( $results = mysql_query($sql, $db) ) !== false ) {
while ( $row = mysql_fetch_assoc($results) ) {
            if (in_array($row['id'], $selected)) {
               echo "<option value=", $row['id'], " selected='selected'
>", $row['category'], "</option><br />";
                   }
               else echo "<option value=", $row['id'], ">",
$row['category'], "</option><br />";
            }
        }

Problem #1)    in the first query result. I can't figure out how to deal
with it. The code works fine if there are categories assigned to the
book. If not, an undefined variable error is spewed out for selected.

Problem #2) in the second query, the selected is in the source code but
it is not highlited. Several times I did get the categories highlighted,
but I could never catch what it was that made it work. When I had the
$id problem, i was trying this code from Yuri (but I don't understand
where he got the $id from ) :

<select name="categoriesIN[]" multiple>
<?
$sql = "SELECT id, category FROM categories";
if ( ( $results = mysql_query($sql, $db) ) !== false ) {
  while ( $row = mysql_fetch_assoc($results) ) {
    $selected = ($id == $row['id'] ? 'selected="selected"' : '');
    echo "<option value=".$row['id']." ".$selected.">".$row['name']."</option>";
  }
}
?>
</select>

I think there is an error in this last code... could it be $id is
supposed to be the array holding the category ids?


-- 
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


[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