Jump to content


Photo

create search from multiple choises!


  • Please log in to reply
6 replies to this topic

#1 b_kitty

b_kitty
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 02 October 2006 - 02:34 PM

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!!) :)

#2 bizerk

bizerk
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 02 October 2006 - 06:03 PM

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

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 02 October 2006 - 06:19 PM

This is an example of the method I use

http://www.phpfreaks....html#msg360739
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 02 October 2006 - 06:40 PM

Here is a "lighter" version:
<?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());

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

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 02 October 2006 - 06:47 PM

Too many 'WHERE' keywords

--> .... WHERE aaa = '123' AND WHERE bbb = '345'
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 02 October 2006 - 07:07 PM

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... *

#7 b_kitty

b_kitty
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 03 October 2006 - 11:29 AM

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!! ::)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users