RE: populate form input option dropdown box from existing data

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

 



sorry, maybe I have been lazy in that comment, I admit, whem wrote that solution i was in a such hurry and without time. I dont really read what i wrote, but now i think this solution is good


<select name="categories" multiple style='width:120px;height:150px'>
<?
$sql = "SELECT id,name FROM categories";
if ( ( $results = mysql_query($sql, $conn) ) !== false ) {
  while ( $row = mysql_fetch_assoc($results) ) {
    $selected = ($id == $row['id'] ? 'selected="selected"' : '');
    echo "<option value=".$row['id']." ".$selected.">".$row['name']."</option>";
  }
}
?>
</select>

> Date: Tue, 16 Jun 2009 20:46:29 -0400
> From: af.gourmet@xxxxxxxxxxxx
> To: ash@xxxxxxxxxxxxxxxxxxxx
> CC: gargarinbr@xxxxxxxxxxx; me@xxxxxxxxxxx; php-general@xxxxxxxxxxxxx; tedd.sperling@xxxxxxxxx; nospam@xxxxxxxxxxxxx
> Subject: Re:  populate form input option dropdown box from	existing	data
> 
> Ashley Sheridan wrote:
> > On Tue, 2009-06-16 at 18:19 -0400, PJ wrote:
> >   
> >> Ashley Sheridan wrote:
> >>     
> >>> On Tue, 2009-06-16 at 15:48 -0400, PJ wrote:
> >>>   
> >>>       
> >>>> 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 --- pj@xxxxxxxxxxxxx
> >>>>    http://www.ptahhotep.com
> >>>>    http://www.chiccantine.com/andypantry.php
> >>>>
> >>>>
> >>>>     
> >>>>         
> >>> <option value="id" (id == $row ? "selected" : "") >xxxx</option> is
> >>> pretty bad HTML, as attributes should always have a value. Personally, I
> >>> do something like this as it tends not to confuse the code too much
> >>>
> >>> $selected = ($id == $row)?'selected="selected"':'';
> >>> print "<option value=\"$id\" $selected >xxxx</option>";
> >>>   
> >>>       
> >> I was unable to get any of the suggestions to work, except in_array().
> >> However, the selected item refuses to become highlighted.
> >> code:
> >> <select name="categoriesIN[]" multiple size="8">
> >> <?php
> >> $sql = "SELECT * FROM categories";
> >>         //$selected = ($id == $row)?'selected="selected"':'';
> >>   if ( ( $results = mysql_query($sql, $db) ) !== false ) {
> >>         while ( $row = mysql_fetch_assoc($results) ) {
> >>             if (in_array($row['id'], $ccc)) {
> >>               echo "<option value=", $row['id'], " selected >",
> >> $row['category'], "</option><br />";
> >>                   }
> >>               else echo "<option value=", $row['id'], ">",
> >> $row['category'], "</option><br />";
> >>             }
> >>         }
> >> ?>
> >>     </select>
> >> I can't find anything that explains why the selected item is not
> >> highlighted.
> >>
> >> -- 
> >> Hervé Kempf: "Pour sauver la planète, sortez du capitalisme."
> >> -------------------------------------------------------------
> >> Phil Jourdan --- pj@xxxxxxxxxxxxx
> >>    http://www.ptahhotep.com
> >>    http://www.chiccantine.com/andypantry.php
> >>
> >>     
> > Have you actually looked at the html this produces to see if any of the
> > elements are being marked with the selected="selected" attribute?
> >
> > Thanks
> > Ash
> > www.ashleysheridan.co.uk
> >   
> I just cannot find a way to pass the selected fields to the options script.
> Even if I add the selected to all the fields, they show up in the source
> code but the fields are still not highlighted.
> There has to be a way to get this right??? :'(
> 
> -- 
> 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
> 

_________________________________________________________________
Descubra todas as novidades do novo Internet Explorer 8
http://brasil.microsoft.com.br/IE8/mergulhe/?utm_source=MSN%3BHotmail&utm_medium=Tagline&utm_campaign=IE8

[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