Hi Again,
Sorry but i still seem to have problems and wonder if you'd help.
Here is what I have in the sql:
SELECT vt.listing_id, ld.user_ID, ld.Title
FROM listingsDB ld
left JOIN listingsDBElements vt
ON vt.listing_id = ld.ID
left JOIN listingsDBElements vc
ON vc.listing_id = ld.ID
left JOIN listingsDBElements vn
ON vn.listing_id = ld.ID
left JOIN listingsDBElements vci
ON vci.listing_id = ld.ID
WHERE ld.active = 'yes'
AND ((vt.field_name = 'venue_type' AND vt.field_value like '%$venue_type%') OR '$venue_type' = '')
AND ((vc.field_name = 'county' AND vc.field_value like '%$county%') OR '$county' = '')
AND ((vn.field_name = 'venue_name' AND vn.field_value like '%$venue_name%') OR '$venue_name' = '')
AND ((vci.field_name ='city' AND vci.field_value like '%$city%') OR '$city' = '')
This works fine when I do a search such as this using all 4 fields:
php?venue_name=Weston%20Park&city=Shifnal&county=Shropshire&venue_type=Country%20House
But if I search just by county eg.
php?venue_name=&city=&county=Cornwall&venue_type=
The server freaks out and I have to restart.
If I use just three of the 4 fields then the venue results seem to appear multiple times.
This is the sql query that works when I include all 4 search fields:
SELECT vt.listing_id, ld.user_ID, ld.Title FROM listingsDB ld left JOIN listingsDBElements vt ON vt.listing_id = ld.ID left JOIN listingsDBElements vc ON vc.listing_id = ld.ID left JOIN listingsDBElements vn ON vn.listing_id = ld.ID left JOIN listingsDBElements vci ON vci.listing_id = ld.ID WHERE ld.active = 'yes' AND ((vt.field_name = 'venue_type' AND vt.field_value like '%Country House%') OR 'Country House' = '') AND ((vc.field_name = 'county' AND vc.field_value like '%Shropshire%') OR 'Shropshire' = '') AND ((vn.field_name = 'venue_name' AND vn.field_value like '%Weston Park%') OR 'Weston Park' = '') AND ((vci.field_name ='city' AND vci.field_value like '%Shifnal%') OR 'Shifnal' = '')
Can anyone please suggest how I can run the sql query but amend it so that it give a result even if users only fill in one, two or three fields in the form instead of all of them.
My only thought was to check which fields have been used and write multiple versions of the query and match one to the other. But this is rather clumsy and long winded.
eg.
SELECT vt.listing_id, ld.user_ID, ld.Title
FROM listingsDB ld
left JOIN listingsDBElements vt
ON vt.listing_id = ld.ID
left JOIN listingsDBElements vc
ON vc.listing_id = ld.ID
left JOIN listingsDBElements vn
ON vn.listing_id = ld.ID
left JOIN listingsDBElements vci
ON vci.listing_id = ld.ID
WHERE ld.active = 'yes'";
if ($venue_type !=""){ // if we have a name search for it
$sql3 .= "AND ((vt.field_name = 'venue_type' AND vt.field_value like '%$venue_type%') OR '$venue_type' = '') ";
}
if ($county !=""){ // if we have a name search for it
$sql3 .= "AND ((vc.field_name = 'county' AND vc.field_value like '%$county%') OR '$county' = '') ";
}
if ($venue_name !=""){ // if we have a name search for it
$sql3 .= "AND ((vn.field_name = 'venue_name' AND vn.field_value like '%$venue_name%') OR '$venue_name' = '') ";
}
if ($city !=""){ // if we have a name search for it
$sql3 .= "AND ((vci.field_name ='city' AND vci.field_value like '%$city%') OR '$city' = '') ";
}
$sql3 .= " order by ld.user_ID DESC
But this still repeats the listings multiple times should I omit even just one of the fields searched. eg. omiting the venue name results in 56 of repeats of the same single venue listing information for instance.
Any suggestions would be welcomed.
Bob