Re: populate form input option dropdown box from existing data

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

 



2009/6/18 PJ <af.gourmet@xxxxxxxxxxxx>:
> 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.

It's best practice to initialise all variables before using them. The
code you have will not create the $selected variable if there are no
results...

(code repeated for clarity)
> if ( ( $results = mysql_query($sql, $db) ) ) {

If there are no results this will still "work" so will drop through to...

>  while ( $row = mysql_fetch_assoc($results) ) {
>    $selected[] = $row['id'];
>    }

But since there are no results the first call to mysql_fetch_assoc
will return false so the line in the middle will never get executed.

>  }
> else $selected = Array( 0 => '0');

Drop this else line and instead put $selected = array(); before the
mysql_query line. Not sure why you want an element 0 => '0' in there,
I'm guessing it's one of your attempts to get rid of the notice.

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

Can you post the HTML you're getting for this select using the above
code. If the selected attributes are in the code correctly then there
must be a syntax error in there somewhere and the easiest way to find
it will be by looking at the HTML.

-Stuart

-- 
http://stut.net/

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