Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building a query via a form

Status
Not open for further replies.

rogwilco

Programmer
Mar 21, 2005
21
GB
I'm struggling building a SQL query from the output of a form, i.e. the user inputs into a form which in turn decides the query.

I have never done this before and was just wondering if anyone had any links tutorials of something like this!? I have searched but haven't found anything too useful yet.

Basically all I want to do is for the user to pick from a drop down menu how they want a leaderboard displayed, i.e. top 50 results, bottom 50, 50 to 100 results, etc.

Do I just tie a complete SQL statement with the corresponding LIMIT info inside to a variable. The variable being the value of the chosen item in the drop down menu.

Pseudocode as follows;

If chosen value = A
then SQL= this
ELSE IF chosen value = B
then SQL=this
ELSE IF chosen value =C
then SQL=this
else
SQL statement without any LIMIT.

Sorry if that sounded confused. I know it's only basic stuff but I'd really like to see an example of it in action.

By the way I am using PHP and MySQL.

Thanks.
 
That is exactly the right way to do it.

Remember that the SQL query you create in PHP is nothing more than a string. This string will be interpreted by MySQL as a SQL statement. So your code can use the variable directly to construct the string.

Pseudocode:

x = chosen value

If chosen value is not unlimited
      then vLimitClause = "LIMIT " + x
else
vLimitClause = ""

Build SQL statement
vSQL = "SELECT good things FROM InterestingTable " + vLimitClaus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top