| | |
complex sql database search using php code
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 6
Reputation:
Solved Threads: 0
Hi, I am trying to create a search function on an html page. I have written the page and part of a php page.
I trying to get help with the search as there are 3 criteria;
So far I have made this search form;
(textbox name=searchterm) Brand:
(dropdown name=type) Type: (3 options; 'PC', 'LAPTOP', 'PC and LAPTOP')
(dropdown name=on0) Online or Highstreet: (2 options; 'ONLINE', 'HIGHSTREET')
(textbox name=postcode) postcode: (this is hidden until the user chooses 'HIGHSTREET' on 'on0'
so I am looking for the php code for the 'type' choice if the user says 'PC' then it will search for the 'Brand' in field 'PCBrand' but if the user chooses 'LAPTOP' then it searches for the 'Brand' in field 'LBrand' and also if they make choice 'PC and LAPTOP' then it searches for 'Brand' in both fields 'PCBrand' and 'LBrand'
To add to this I am trying to find how to also query the 'Online or highstreet' by selecting only records where field 'ONLINEHIGHSTREET' equals 'ONLINE' or 'HIGHSTREET' and if 'HIGHSTREET' then take first 2 letters of 'postcode' and search in field 'postcode_code'
Anyones advice would be very much appreciated
I trying to get help with the search as there are 3 criteria;
So far I have made this search form;
(textbox name=searchterm) Brand:
(dropdown name=type) Type: (3 options; 'PC', 'LAPTOP', 'PC and LAPTOP')
(dropdown name=on0) Online or Highstreet: (2 options; 'ONLINE', 'HIGHSTREET')
(textbox name=postcode) postcode: (this is hidden until the user chooses 'HIGHSTREET' on 'on0'
so I am looking for the php code for the 'type' choice if the user says 'PC' then it will search for the 'Brand' in field 'PCBrand' but if the user chooses 'LAPTOP' then it searches for the 'Brand' in field 'LBrand' and also if they make choice 'PC and LAPTOP' then it searches for 'Brand' in both fields 'PCBrand' and 'LBrand'
To add to this I am trying to find how to also query the 'Online or highstreet' by selecting only records where field 'ONLINEHIGHSTREET' equals 'ONLINE' or 'HIGHSTREET' and if 'HIGHSTREET' then take first 2 letters of 'postcode' and search in field 'postcode_code'
Anyones advice would be very much appreciated
Last edited by mattyp791; Nov 7th, 2009 at 8:33 am.
-1
#2 Nov 7th, 2009
I suggest that you use a relational model for your DB. The Type should have its own table, as should OnlineHighstreet (although this isn't essential). Your main table should then have integers (tinyint) stored under these fields (foreign key on the other tables). This means that your 'dropdowns', should have the following structure:
The $_POST['type'] variable will then be either 1, 2 or 3. You then just search for the integer in your table.
PHP Syntax (Toggle Plain Text)
<select id="type" name="type"> <option value="1">PC</option> <option value="2">LAPTOP</option> <option value="3">PC and LAPTOP</option> </select>
The $_POST['type'] variable will then be either 1, 2 or 3. You then just search for the integer in your table.
Last edited by ardav; Nov 7th, 2009 at 10:19 am.
Happy Humbugging Christmas
•
•
Join Date: Nov 2009
Posts: 6
Reputation:
Solved Threads: 0
0
#3 Nov 7th, 2009
Hi, many thanks for your prompt response. I understand what you mean and I have a field in the database for both 'type' and also for 'onlinehighstreet' but could i ask which php code i should be writing that will actually request this from the sql database?
here is the code i have used so far in php
$result=$db->query("SELECT PCBrand, LBrand, PCLAPTOP, ONLINEHIGHSTREET, Retailer, Address, Address_2, Town, County, Postcode, Postcode_code, Phone, Website FROM
database WHERE PCLAPTOP LIKE 'type' AND PCBrand LIKE '%$searchterm%'");
however obviously the above will only currently search field PCBrand whereas that needs to be a variable based upon the answer to type
Thanks
here is the code i have used so far in php
$result=$db->query("SELECT PCBrand, LBrand, PCLAPTOP, ONLINEHIGHSTREET, Retailer, Address, Address_2, Town, County, Postcode, Postcode_code, Phone, Website FROM
database WHERE PCLAPTOP LIKE 'type' AND PCBrand LIKE '%$searchterm%'");
however obviously the above will only currently search field PCBrand whereas that needs to be a variable based upon the answer to type
Thanks
Last edited by mattyp791; Nov 7th, 2009 at 1:11 pm.
•
•
Join Date: May 2009
Posts: 4
Reputation:
Solved Threads: 1
1
#4 Nov 7th, 2009
Process your $_POST and do some switch function to see what type the user selected and set your $search_type
If this is wrong i might have understood your question wrong.
PHP Syntax (Toggle Plain Text)
$result=$db->query("SELECT PCBrand, LBrand, PCLAPTOP, ONLINEHIGHSTREET, Retailer, Address, Address_2, Town, County, Postcode, Postcode_code, Phone, Website FROM database WHERE PCLAPTOP LIKE 'type' AND " .$search_type. " LIKE '%$searchterm%'");
Last edited by ax8l; Nov 7th, 2009 at 3:17 pm. Reason: wrong answear
•
•
Join Date: Nov 2009
Posts: 6
Reputation:
Solved Threads: 0
0
#5 Nov 8th, 2009
•
•
•
•
Process your $_POST and do some switch function to see what type the user selected and set your $search_type
If this is wrong i might have understood your question wrong.PHP Syntax (Toggle Plain Text)
$result=$db->query("SELECT PCBrand, LBrand, PCLAPTOP, ONLINEHIGHSTREET, Retailer, Address, Address_2, Town, County, Postcode, Postcode_code, Phone, Website FROM database WHERE PCLAPTOP LIKE 'type' AND " .$search_type. " LIKE '%$searchterm%'");
I am going to try to work it out - so far i have tried the following code but it is nt working.
$result=$db->query("SELECT PCBrand, LBrand, PCLAPTOP, ONLINEHIGHSTREET, Retailer, Address, Address_2, Town, County, Postcode, Postcode_code, Phone, Website FROM
database WHERE PCLAPTOP LIKE 'type' AND switch ($type){
case "LAPTOP":
echo "LBrand";
break;
case "PC":
echo "PCBrand";
break;
case "both":
echo "PCLAPTOP";
break;
}
LIKE '%$searchterm%'");
I think your suggestion is to create a search_type that takes type and changes to names of fields - is this what you mean? I am going to try and do that - thanks for you help - much appreciated
Last edited by mattyp791; Nov 8th, 2009 at 9:45 am.
•
•
Join Date: Nov 2009
Posts: 6
Reputation:
Solved Threads: 0
0
#6 Nov 8th, 2009
I am trying to set up the switch function but i have something wrong with my code.
$search_type=$db->escapeString($_GET['type']);
switch ($type){
case "PC":
echo "PCBrand";
break;
case "LAPTOP":
echo "LBrand";
break;
case "PC and LAPTOP":
echo "PCLBrand";
break;
}
The issue is that the search ends up looking for a field in database called the value of type so i don't think the switch is set up incorrectly - please could someone take a look. Should I be putting this code within the html page that has the form or is it fine as it is in the php process search page?
$search_type=$db->escapeString($_GET['type']);
switch ($type){
case "PC":
echo "PCBrand";
break;
case "LAPTOP":
echo "LBrand";
break;
case "PC and LAPTOP":
echo "PCLBrand";
break;
}
The issue is that the search ends up looking for a field in database called the value of type so i don't think the switch is set up incorrectly - please could someone take a look. Should I be putting this code within the html page that has the form or is it fine as it is in the php process search page?
-1
#7 Nov 8th, 2009
It seems to me that you have a bit of an odd DB structure (sorry no offence intended).
Models
id (PK)
manufacturer (FK on manufacturer id)
name (e.g. Inspiron 1520)
description (blurb for model)
type (PC/Laptop)
Manufacturers
id (PK)
manufacturer_name
Retailers
id (PK)
retailer_name
onlineH
address_1
address_2
post_code
tel
fax
www
email
map_url
Stock
id (PK)
retailer_id (FK on retailer id)
model_id (FK on model id)
This structure should allow easy additions to each table.
SQL queries will require INNER JOINs.
I don't know if this helps.
Models
id (PK)
manufacturer (FK on manufacturer id)
name (e.g. Inspiron 1520)
description (blurb for model)
type (PC/Laptop)
Manufacturers
id (PK)
manufacturer_name
Retailers
id (PK)
retailer_name
onlineH
address_1
address_2
post_code
tel
fax
www
map_url
Stock
id (PK)
retailer_id (FK on retailer id)
model_id (FK on model id)
This structure should allow easy additions to each table.
SQL queries will require INNER JOINs.
I don't know if this helps.
Happy Humbugging Christmas
•
•
Join Date: Nov 2009
Posts: 6
Reputation:
Solved Threads: 0
0
#8 Nov 9th, 2009
Hi, no offence taken - i'm fairly new to all this so am greatful to any advise.
In reply to your comments, i can change the db format but at the moment I have one row for each retailer store and for the manaufactures i put them all into one field. If I have one retailer store for every invidicual brand i will end up with a huge database though?
In reply to your comments, i can change the db format but at the moment I have one row for each retailer store and for the manaufactures i put them all into one field. If I have one retailer store for every invidicual brand i will end up with a huge database though?
-1
#10 Nov 9th, 2009
•
•
•
•
Hi, no offence taken - i'm fairly new to all this so am greatful to any advise.
In reply to your comments, i can change the db format but at the moment I have one row for each retailer store and for the manaufactures i put them all into one field. If I have one retailer store for every invidicual brand i will end up with a huge database though?
The the 'stock' table, to my mind, is essential, even though it seems as though it will be massive. However, if ALL stores/retailers stock exactly the same (ALL) models, you could dispense with this table.
If you use one table for everything, you are storing duplicate addresses, brand names etc etc. This setup invites errors. If a simple typo is made, your resultset will not list all records you expect.
Last edited by ardav; Nov 9th, 2009 at 10:02 am.
Happy Humbugging Christmas
![]() |
Similar Threads
- MySQL has run out of memory (MySQL)
- Insert HMTL Code into MySQL Database through a Form with PHP (MySQL)
- php code to search and show results on same page (PHP)
- This php code here isn't working... I think it needs to be converted to SQL (PHP)
- create table in sql using php code (PHP)
- php drop down menu to search multiple sql tables (PHP)
- JSP Problem Student database search (Java)
- Beginner: Insert, Update, Delete & Search records from/to sql database (ASP.NET)
- Snyc'n Local SQL database online (MS SQL)
Other Threads in the PHP Forum
- Previous Thread: find user visiting topic in my website?
- Next Thread: PHP email script incomplete for windows and does not send
| Thread Tools | Search this Thread |
Tag cloud for php, sql
.net 2008 access air ajax array asp autosuggest beginner c# c++ class cms connection curl data database date dbsize developer development display dojofoundation dropdown duplicates email execution file files flash form forms forum function hack head html image include insert integration java javascript jquery lamp limit link linux login longisland loop mail menu microsoft mining ms msdn multiple mysql news number office oop oracle paypal php post projectmanager provider query remote report script search security server session sms soap sockets software spam sql survey training tutorial up-to-date upload user validation vb.net vbulletin video vista web webbrowser webdesign xml youtube zend







