MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 18th, 2005, 05:52 PM
RonnyMc RonnyMc is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 1 RonnyMc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 6 sec
Reputation Power: 0
Cool PHP/MySQL Search, Searching using a LINK table

Hello Everyone,

I am new to PHP/MySQL. That being said I am attempting to convert a site from one language into PHP.

I have created the database/tables and imported the data. The relevent table schema is:

Code:
Locations Table
-- LID - Primary Key

LocationCategory Table
-- LCID
-- CATEGORY

LocationCategoryLink Table
-- LocationID (This is the LID from Locations Table)
-- LocationCID (This is the LCID from LocationCategory Table)
-- Level

There are multiple levels (currently 3) to all categories. All locations will have a category assign on all 3 levels. Level 2 is a sub to level 1, Level 3 a sub to level 2.

So what we are attempting to do is create < select > menus using the categories. So when the page is first loaded the Category one < select > menu will be populated. Then we use an onChange to refresh the page after a Category 1 is selected and using that Category we want to populate the Category 2 < select > menu with all categories that have a level 2 that correlate with a location that has the selected Category 1.

I was turned on to using the IN operator for MySQL, but have found that certain categories will be associated with a large number of locations (ie. 7,000+) and stuffing that many LocationIDs into the IN() slows the search down tremendously. Just for the sake of completeness we did test this and it works. We do return the results that we want, but the search is extremely SLOW.

Also I do not know how to get the list of locations into the IN() operator. We did a cut and paste deal so we could test the search.

The code we have to populate < select menu > one and to do the search for menu 2.

Code:
$linkid = @mysql_connect("localhost", "", "");
mysql_select_db("database_name", $linkid);
$cat1id = mysql_query("select 
DISTINCT(locationcategorylink.locationCID), locationcategory.category
from 
locationcategorylink, locationcategory
where
locationcategorylink.locationCID = locationcategory.lcid 
and 
locationcategorylink.locationlevel = 1
ORDER BY 
locationcategory.category ASC ");



// ------------------------- // 
// CATEGORY 2 SELECT MENU //
// ------------------------- //
if (isset($_POST['category1']) == TRUE)
{
$category1 = $_POST['category1'];

$cat2id = mysql_query("select 
DISTINCT(locationcategorylink.locationID)
from 
locationcategorylink
where
locationcategorylink.locationCID = $category1
ORDER BY 
locationcategorylink.locationID ASC");


// NEED TO GET THE LOCATION TO USE WITHIN THIS SEARCH


$cat2aid = mysql_query("
select DISTINCT(locationcategorylink.locationCID), locationcategory.category
from 
locationcategorylink, locationcategory
where
locationcategorylink.locationCID = locationcategory.lcid 
and 
locationcategorylink.locationlevel = 2
and
locationcategorylink.locationID
in
( $locs )
ORDER BY 
locationcategory.category ASC

");


I know this may sound a bit confussing but please bear with me I am a noob. Just ask and I will attempt to answer it.

To summarize:

1. We show all level 1 categories in a < select > menu.
2. onChange on this menu we submit the selected Category ID.
3. Take submitted CID and get all locationsID from LocationCategoryLink table.
4. Use LocationID to get all Level 2 Categories.

If this is not the ideal way to do this please let me know.

Thanks,
Ron

Reply With Quote
  #2  
Old August 20th, 2005, 10:31 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
Your story is a bit unclear. Maybe you can give some example entries in the tables.

Do I understand correctly that there are a number of locations, which each location having three catagories (on each level). So:
Code:
Level    Cat1     Cat2     Cat3
L_A      L_A-1    L_A-2    L_A-3
L_B      L_B-1    L_B-2    L_B-3
L_C      L_C-1    L_C-2    L_C-3


The first menu would then show L_A-1, L_B-1 and L_C-1. But what would be shown when L_A-1 is choosen?

And what is the purpuose of the Locations table? It only contains an ID. This way the whole table has no meaning at all...

As far as I would go at this point my first query would be:
Code:
SELECT DISTINCT(locationID), category
FROM locationcategorylink
LEFT JOIN locationcategory ON locationCID = lcid 
WHERE level=1 


Which is basically the same as yours but easier to understand (in my humble opinion). There is no exact need to specify the table name in front of each field name. When you want or need to I would suggest using short aliases.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > PHP/MySQL Search, Searching using a LINK table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT