Multiple List Boxes - best way?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gumshoe
Forum Newbie
Posts: 4
Joined: Fri May 11, 2007 11:09 am

Multiple List Boxes - best way?

Post by gumshoe »

I would like to have a user select values from 7 fields in a MySQL database. Selectable non-editable list boxes seems like a good way to do it.
I was wondering if having seven separate list boxes side by side is going to cause problems. Will they shove each other around, are there going to be placement issues? Is there a better way to do this, like have a grid of some sort with cells instead of list boxes, or a table to hold this all in to avoid spacing problems? How would you do it?
I searched here but did not see anything like this listed.

Regards,
Gord
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Hi,

I've never needed to display so many list boxes side-by-side, but if I were to do so I would seriously consider using some css to help position and arrange the list boxes and style the form itself.

Hope this helps.
gumshoe
Forum Newbie
Posts: 4
Joined: Fri May 11, 2007 11:09 am

Post by gumshoe »

Yes, thank you. I just need people's opinion. I am sure I am not the only one wanting to know this.

I have two online examples that I have found:

http://www.e-sonic.com/acc/products.asp ... =Reel+Deal

and

http://www.newark.com/jsp/search/browse ... D=resistor

Hopefully those links work for you. One example above is definitely separate list boxes. The other, I would have to check the source in the browser to see but it is hard for me at this point to discern the flow and what means what. I come from Delphi and this PHP / HTML takes getting used to. As you can see I am in the electronics industry, and I want customers to be able to select components based on their characteristics.

Regards,
Gord
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Hi,

From looking at the page source of the first link example you gave a table has been used to create seperate columns for each list box.

The person who wrote the source has also populated the list boxes directly as opposed to populating them from a database!!!

Anyway, from my point of view tables seem like a fine choice in order to display the list boxes. Plus, tables are very useful when extracting data (loop wise that is).
gumshoe
Forum Newbie
Posts: 4
Joined: Fri May 11, 2007 11:09 am

Post by gumshoe »

Yes, I saw that too, thought it was really odd! I mean, that's got to be incredibly awkward for a site that size. I'm just starting out in all this and I can clearly see that a database is the simple and obvious choice for displaying lists like that. And really easy to do especially with all the free PHP database templates out there.

I'm not quite clear on what you mean by tables. You mean just HTML tables to position the list boxes, right? That seems like it would work. That doesn't help me with loops though, does it? Anyway thanks for the opinion.

As this is a new field for me, I am trying to discover if Ruby on Rails, or Perl, would be the simplest to try out on a basic project like this. I am getting the impression that basic PHP programming is a thing of the past, and more difficult to get the results you want. Is this true? When I say I want 7 list boxes, guys that program PHP look at me and just shake their head and say good luck. That scares me. :=) lol

Regards,
Gord
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

I think it'd be fine displaying 7 list boxes inline. The only considerations I would have would be the appearance i.e. might the user have to scroll horizontally to view subsequent list boxes? Maybe not, but its certainly a design consideration.

In terms of using tables to display the data I would certainly say yes, tables are a good structure. Simple to implement and easy to style etc...

Here is an (un-tested) example of how I might implement a database populated collection of 7 list boxes using HTML and PHP.

Code: Select all

<?
#CONNECT TO DB
include"conn.inc.php";
?>

<table width="100%">

<tr><!--ROW IN WHICH LIST BOXES WILL APPEAR-->

<td><!--FIRST LIST BOX COLUMN-->

<select name="listBox1">
<!--DEFAULT OPTION IN LIST BOX-->
	<option value="Select" selected="selected">Select...</option>

<?
#SET UP QUERY USED TO GET DATA IN ORDER TO POPULATE FIRST LIST BOX e.g. Laptops
	$popListBox1 = mysql_query("SELECT field_names FROM tblName WHERE category = 'laptop' ORDER BY field_price") or die(mysql_error());

#INITIALISE LOOP - THIS PART WILL EXTRACT MATCHED RECORDS AND DISPLAY EACH RESULT ROW BY ROW IN A LIST BOX
while($row=mysql_fetch_assoc($popListBox1))

{	#START OF LOOP

#MAKE THE FIELD DATA AVAILABLE
	extract($row);

#NOW POPULATE LIST BOX (WE ARE STILL IN THE LOOP)
	echo "<option value='$field_id'>$field_name</option>\n";

}	#END OF LOOP

?>
</select> <!--MAKE SURE BOTH THE BEGINNING AND ENDING <select></select> ARE OUTSIDE THE LOOP-->
</td>

<td>LIST BOX 2 - DO THE SAME THING BUT OBVIOUSLY WITH DIFFERENT DATA</td>

<td>SO ON AND SO FORTH</td>

</tr>
</table>
The code snippit above simply displays the populated list boxes using a table.

Hope it helps.
Post Reply