Forums

PHP

This topic is locked

Dynamic queries

Posted 05 Oct 2006 05:06:05
1
has voted
05 Oct 2006 05:06:05 Sam Maxwell posted:
Newbie trying to figure out how to build a dynamic query. If I have a MySQL database, and am trying to use PHP to query the database with criteria that are completed by the user.

For example, say the db is about ice cream. There are 20 records. I want to display records that meet one or more characteristics as selected by the user for ice cream orders. In example 1, you want to show all records for orders based on container - e.g., a field called container with possible values of smallcup, largecup, sugarcone, wafercone.

I GUESS I need a variable called $container. Basically the query will be like:
SELECT `customer` , `flavor` , `container` FROM `icecream` WHERE `container` like `$container`;

On the php page I include a dropdown for container. The response determines the WHERE constraint, e.g., WHERE `container` like `sugarcone`; I want the answer - in this case, sugarcone - to go into the query and show the results.

I am sure this is super-easy but I'm scratching my head.

And I really want to allow the specification of more than one constraint - for example, if they specify a container, it has a constraint for their choice of container, but otherwise the query retrieves all records regardless of container type, but in another dropdown they specify flavor (flavor column contains values of chocolate, vanilla, strawberry).

Your suggestions, samples, or references would be greatly appreciated!

Replies

Replied 05 Oct 2006 11:43:38
05 Oct 2006 11:43:38 Roddy Dairion replied:
If i understand ur question you want if a user search for sugarcone it lists only sugarcone and if there no sugarcone u want to list everything. Why would u want to do that?
Replied 05 Oct 2006 16:19:18
05 Oct 2006 16:19:18 Sam Maxwell replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
If i understand ur question you want if a user search for sugarcone it lists only sugarcone and if there no sugarcone u want to list everything. Why would u want to do that?
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

Well, i'm using an example. Let's say your database tracks ice cream sales. I have a page that dynamically retrieves all sales so you can view that list; but let's say you want to view sales based on dynamically determined criteria, including container type, ice cream flavor, topping, or salesperson. My idea is to have a page that lists your 3 different search criterion. The basic query with no criteria specified would be "select all from icecream sales". If you specify a container type, e.g., sugarcone, then it adds the constraint "where container like sugarcone"; if you add a flavor, then it adds to the query "where flavor like vanilla". If you specify a salesperson, then it will add the constraint "where salesperson is keisha". And you can select zero, one, two, or three of the criteria. When you've finished your selections, you click a submit button and the query is retrieved - probably to the same page, so you can change your criterion again if you want.

Make sense?

Reply to this topic