Jump to content

Archived

This topic is now archived and is closed to further replies.

b_kitty

create search from multiple choises!

Recommended Posts

I have a PHP-MYSQL application and i want to create an advanced search. I have 4 tables and i want to make it possible for the user to chose from different fields and to make a search based on each field. the point is that i only know a way in which you check which field isset() end u make a search based on that. for the moment i have 5 fields so i have to make quite a lot combinations if i want to see which fields are set. I' am sure there is another way that i have to learn..can someone help me expand my horizons?!(sorry for being so begginner!!) :)

Share this post


Link to post
Share on other sites
Yeah, so you have 4 tables that you want to be searched that are on a MYSQL database. Well okay first make sure to connect to your data base or include(); your database scripts and than just use mysql querys and "if" statements to do the rest. The only time i have ever used isset() is for getting something in $_FILES, $_POST, or $_GET.
for example if you have a form where you want to upload a file you would do:
if(isset($_POST['upload_image']))
{
if($_FILES['imgupload']['name] !=null)
{
$this->*some-type-of-function*(*anyvariablesyouwanttobeincluded*)

so maybe if you define the mysql queries above^ and than make a function where the actual search is occuring you could use an ISSET($_POST['submit']))
$this->search_function['name']['file']['etc']

and than go on from there

Share this post


Link to post
Share on other sites
This is an example of the method I use

http://www.phpfreaks.com/forums/index.php/topic,89842.msg360739.html#msg360739

Share this post


Link to post
Share on other sites
Here is a "lighter" version:
[code]
<?php

$query = "select * from table";

$count = 0;
foreach($_POST as $key => $value)
{
if($count == 0): $qq = " WHERE"; else: $qq = " AND"; endif;
if(!empty($value)): $query .= "$qq $key = '$value'"; endif;
$count++;
}
$query .= " order by id desc";

$sql = mysql_query($query) or die(mysql_error());

?>
[/code]
This is dependant that you name your formfield name the same as your table field-names

Share this post


Link to post
Share on other sites
Too many 'WHERE' keywords

--> .... WHERE aaa = '123' AND WHERE bbb = '345'

Share this post


Link to post
Share on other sites
Barand - you are as always correct - sorry.
I've edited the above code.

Not easy writing php whilst changing dipers, feeding, washing and putting 3 kids to sleep at the same time... *

Share this post


Link to post
Share on other sites
I've got the point and i appreciate all the help but the point is that as i mentioned i have to gather information from different tables. For example..3 tables, A, B and C where B holds primary keys of A and C. now if i make a search from fields of table A then i have to search on table B based on A's ID and then in table C from C's ID in B. if i search with a field from C than it is the viceversa. so..now i am lost....:)))))..i don't know if i made the point but i tried to figure out how to build this search page that goes through 4 tables and i couldn't!!! If anyone understood what i meant...the help ME!! ::)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.