Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 01-15-2005, 06:19 PM   PM User | #1
morpheusoptic
New to the CF scene

 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
morpheusoptic is an unknown quantity at this point
Question allowing users to do an advanced search on my database

I have successfully finished my registration, login and ad placement page for a free classifieds section of my website that I have been working on. Now that I have a way of my users entering information into the database, I am trying to find a way for them to be able to do an advanced search of everything instead of having to display only a few pre-defined columns from the tables in the database that I have set up, "for now".

The advanced search page is going to be made up of text boxes, radio buttons and check boxes.... I am going to have it set up to have a default setting for everything, so if the user decides not to click a radio button or check a box, or fill out a field, the script will resort to my preset defaults for those categories, to simplify the script.

The biggest feature I need is to allow a user to select a price range of products, say I have 3 set price ranges.... If I set that up in 2 or 3 radio buttons the user can choose from, the value of them will be set to a single variable.... take a look at my current code and I will explain more below it.

Code:
$sql = "SELECT * FROM ad_details WHERE ptype LIKE '%$_POST[ptype]%' AND asking < '%$_POST[price]%' AND state LIKE '%$_POST[state]%' ORDER BY price DESC  LIMIT 25";
$query = mysql_query($sql);
$total_results = mysql_num_rows($query);

If you look at the asking < $_POST[price], it basically is saying show columns that only have a price less than whatever the variable $_POST['price'] is set to. Now if my form is currently set up to allow the user to search a price at or below $50 or above $50... how do I make the code work. I know with the current way, if they picked below $50, it would only show ads below $50... if they picked above $50, I would have to set the variable really high, and than it would show not only above but also below $50. Hopefully there is a way of doing this easily... I am sure there is because you see it all the time, however I am lost.

Last thing.... I was reading somewhere about database queries and they said the word LIKE in the SQL command means "="... is this true? If not, should I change LIKE to = in the statement above... I need it to equal that variable, that why I placed LIKE in there. Please check they syntax of my statement as I am new to adding all these LIKE & AND words in it, and also parsing variables into the SQL command... I am sure I have errors!

Thanks in advance everyone, I look forward to getting this running.

Paul
morpheusoptic is offline   Reply With Quote
Old 01-16-2005, 04:53 AM   PM User | #2
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 1,890
Thanks: 5
Thanked 22 Times in 18 Posts
chump2877 is on a distinguished road
I'm new to SQL, too, and I didn't follow some of the stuff you said there (And there was a lot of it!), but I can tell you that LIKE is generally used in SQL to match a variable string with a similar string in your database....

For example:

Quote:
SELECT price FROM parts WHERE product_code LIKE '%$product_code[0]%'
In this line of SQL you are telling the server to find the element of the column "price" in the table "parts" where that element's corresponding "product_code" (an element of another column) matches the pattern of the string contained in the variable "$product_code".....So LIKE has more to do with matching a database element with a variable that has the same pattern, as opposed to declaring a variable and database element are equal...If you want to set a variable equal to a database element, or vice versa, there is probably another SQL operator that you could use...

Hope this helps a little...
__________________
Regards, R.J.

Media Moguls
Cleveland, Ohio Web Site Design and Application Development
chump2877 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:39 PM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.