Forum Moderators: coopster

Message Too Old, No Replies

sorting by category

mysql, php, category, rows, tables,

         

beebob1

11:56 am on Aug 14, 2008 (gmt 0)

10+ Year Member



Ok i have a table with a row that stores three options input from a dropdown option-select list in a form.

Row's Name: Colour
Either: RED, GREEN or BLUE

Say i want to display only one of these categories instead of all three how would i implement that..would it be in the following statement:

$result = mysql_query("SELECT * FROM example .... etc

ta in advance

chronic

12:22 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



What do your tables look like? Do you have a linking table, or just one table?

If you have (Id, Item, Category) in one table, and Category was a text field, you'd do something like

$result = mysql_query("SELECT * FROM example where Category = '" . $category "'");

beebob1

12:55 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



its just one table ie:

id title story color date
1 Tshirt hfdh ddj Red 2008-08-14

The colour row will have either red, green or blue, so "color" is the catagory, colour is a varchar field.

Etheco

1:13 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



just do
$q = mysql_query("SELECT * FROM table ORDER BY color");
You can add ASC or DESC after color to force it to go ethier way.

beebob1

1:16 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



ok so how can i get it just to show only entries with the color Red for example

StoutFiles

1:18 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would never use a SELECT * if you're not working with every column. It's just extra processing it has to do; if you have a lot of traffic you'll notice the extra time it takes.

beebob1

1:19 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



ok, sorry im new to php but working hard to understand it all, what would be a preferred way of doing this?

beebob1

2:35 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



would it be better to create a sub catagory table somehow?

Paul Bedford

2:55 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



Unless I'm missing something wouldn't this work

$q = mysql_query("SELECT * FROM table where color='Red'");

eeek

11:40 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would never use a SELECT * if you're not working with every column. It's just extra processing it has to do; if you have a lot of traffic you'll notice the extra time it takes.

True, but for examples here, where we do not know the details of the application, it is fine.

beebob1

2:55 pm on Aug 29, 2008 (gmt 0)

10+ Year Member



thanks Paul bedford, what yu said produces the result i was after