Re: listbox problems

From: Date: Mon, 03 Feb 2003 00:17:53 +0000
Subject: Re: listbox problems
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-134065@lists.php.net to get a copy of this message
On Sun, Feb 02, 2003 at 04:55:08PM -0500, Matt wrote: > > > i have a listbox: > > <select name="users"> > > <?php > > //connect to db and get query > > mysql_connect("....."); > > mysql_select_db("..."); > > $query=mysql_query("select username from users); > > /*finish the listbox*/ > > while($account=mysql_fetch_array($query)){ > > echo "<option value=$account[username]>$account[username]<br>"; > > Try: > echo "<option value={$account['username']}"; > $selected = ($account['username'] == $_POST['user']) ? 'selected' > : ''; > echo $selected; > echo ">{$account['username']}<br>"; > > > } > > ?> > > </select> > > <input type="submit" value="send"> This is good. I've always liked things like this: $query="SELECT ..."; if ($result=mysql_query($q)) { $sel[$user]=" selected"; print "<select name='users'>\n"; while ($row=mysql_fetch_array($result)) { printf(" <option value='%s'%s>%s</option>\n", $row['id'], $sel[$row['id']], $row['text']); } print "</select>\n"; } -- Paul Chvostek <paul@it.ca> Operations / Abuse / Whatever it.canada, hosting and development http://www.it.ca/

« previous php.general (#134065) next »