suess0r Posted December 6, 2006 Share Posted December 6, 2006 Hi, i'm trying to run a query based on 4 drop down POST's from a previous page: city, typesearch, subtypesearch, and cost. I managed to get 3 of them to work, but when i added the 4th I got lost in my IF's and where to place the AND's... Here's what i'm working with.$sql= 'SELECT * FROM fulltable WHERE '; if($city != ""){ $sql .= 'fulltable.Regions_Landmark = "'.$city.'" '; if($typesearch != "" || $subtypesearch != "" || $cost != ""){ $sql .= " AND ";}} if($typesearch != ""){ $sql .= 'fulltable.'.$typesearch.' = "Yes" '; if($subtypeserach != "" || $cost != ""){ $sql .= " AND ";}} if($subtypesearch != ""){ $sql .= 'fulltable.'.$subtypesearch.' = "Yes" '; if($cost != ""){ $sql .= " AND ";}} if($cost != "") $sql .= 'fulltable.'.$costsearch.' = "Yes"';See the AND's is where it get's tricky b/c if I select by City and Subtype i need just 1 AND, etc.. Thanks, look forward to your responses :D Link to comment https://forums.phpfreaks.com/topic/29683-getting-lost-in-my-ifs-can-someone-see-something-i-dont/ Share on other sites More sharing options...
kenrbnsn Posted December 6, 2006 Share Posted December 6, 2006 Put the "where" clauses into a temporary array and the use the implode() function to put the " AND " strings between each:[code]<?php$tmpq = array();$sql= 'SELECT * FROM fulltable WHERE ';if($city != "") $tmpq[] = 'fulltable.Regions_Landmark = "'.$city.'" ';if($typesearch != "") $tmpq[] .= 'fulltable.'.$typesearch.' = "Yes" ';if($subtypesearch != "") $tmpq[] .= 'fulltable.'.$subtypesearch.' = "Yes" ';if($cost != "") $tmpq[] .= 'fulltable.'.$costsearch.' = "Yes"';$sql .= implode(' AND ',$tmpq);echo $sql . '<br>'; // just to show what you've generated?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/29683-getting-lost-in-my-ifs-can-someone-see-something-i-dont/#findComment-136242 Share on other sites More sharing options...
Psycho Posted December 6, 2006 Share Posted December 6, 2006 I actually like kenrbnsn's method, but I had already put this together and thought I'd show another alternative.I usually use logic like this. I create a variable just for the sub parameters. Each time I have a parameter to add I check to see if the variable already has data - if so, I add an "AND"[code]<?php$sql= "SELECT * FROM fulltable";$where = "";if ($city) { $where = "fulltable.Regions_Landmark = '" . $city . "'";}if($typesearch) { if ($where) { $where .= " AND "; } if($typesearch != ""){ $where .= "fulltable." . $typesearch . " = 'Yes'";}if($subtypesearch) { if ($where) { $where .= " AND "; } $where .= "fulltable." . $subtypesearch . " = 'Yes'";}if($cost) { if ($where) { $where .= " AND "; } $where .= "fulltable." . $costsearch . " = 'Yes'";}$sql .= ($where) ? " WHERE ".$where : "";?>[/code] Link to comment https://forums.phpfreaks.com/topic/29683-getting-lost-in-my-ifs-can-someone-see-something-i-dont/#findComment-136254 Share on other sites More sharing options...
suess0r Posted December 6, 2006 Author Share Posted December 6, 2006 thanks guys, i'm still playing with your codes... and after echo'ing my query I found that i'm having some other issues... with type i'm passing in 2 letter values (ie: NC (night club), SB (sports bar), etc) and i want to rename typesearch to match the type that was passed (to match the field name in the table), did i go about this the wrong way.. (kinda newb question) ;x $type = addslashes($_REQUEST['type']); if($type = "NC") $typesearch = "`Night_Club`"; else if($type = "SB") $typesearch = "`Sports_Bar`"; else if($type = "LO") $typesearch = "`Lounges`"; else if($type = "MV") $typesearch = "`Music_Venue`"; else if($type = "AD") $typesearch = "`Adult`"; else if($type = "EV") $typesearch = "`Events`"; else $type = "";B/c my query is choosing the first one NC no matter what get's passed through... Link to comment https://forums.phpfreaks.com/topic/29683-getting-lost-in-my-ifs-can-someone-see-something-i-dont/#findComment-136258 Share on other sites More sharing options...
Psycho Posted December 6, 2006 Share Posted December 6, 2006 This will work as well:[code][<?php$sql= "SELECT * FROM fulltable";$where .= ($city) ? "fulltable.Regions_Landmark = '" . $city . "'":"";$where .= ($typesearch) ? (($where) ? " AND ":"") . "fulltable." . $typesearch . " = 'Yes'":"";$where .= ($subtypesearch) ? (($where)?" AND ":"") . "fulltable." . $subtypesearch . " = 'Yes'":"";$where .= ($cost) ? (($where)?" AND ":"") . "fulltable." . $costsearch . " = 'Yes'":"";$sql .= ($where) ? " WHERE ".$where : "";?>[/code] Link to comment https://forums.phpfreaks.com/topic/29683-getting-lost-in-my-ifs-can-someone-see-something-i-dont/#findComment-136262 Share on other sites More sharing options...
AbydosGater Posted December 6, 2006 Share Posted December 6, 2006 Sorry guys replyed to the wrong topic :( :P Link to comment https://forums.phpfreaks.com/topic/29683-getting-lost-in-my-ifs-can-someone-see-something-i-dont/#findComment-136264 Share on other sites More sharing options...
Psycho Posted December 6, 2006 Share Posted December 6, 2006 When checking to see if two values are equal you need to use double equal signs. By using a single equla sign you are saying try to set this value and if so return true. But, I would suggest using an array so you don't need all those if statements.[code]<?php$typeList = array ( "NC" => "Night_Club", "SB" => "Sports_Bar", "LO" => "Lounges", "MV" => "Music_Venue", "AD" => "Adult", "EV" => "Events");$type = addslashes($_REQUEST['type']);$sql= "SELECT * FROM fulltable";$where .= ($city) ? "fulltable.Regions_Landmark = '" . $city . "'":"";$where .= ($typesearch) ? (($where) ? " AND ":"") . "fulltable." . $typeList[$type] . " = 'Yes'":"";$where .= ($subtypesearch) ? (($where)?" AND ":"") . "fulltable." . $subtypesearch . " = 'Yes'":"";$where .= ($cost) ? (($where)?" AND ":"") . "fulltable." . $costsearch . " = 'Yes'":"";$sql .= ($where) ? " WHERE ".$where : "";echo $sql;?>[/code] Link to comment https://forums.phpfreaks.com/topic/29683-getting-lost-in-my-ifs-can-someone-see-something-i-dont/#findComment-136266 Share on other sites More sharing options...
suess0r Posted December 6, 2006 Author Share Posted December 6, 2006 Sweet, thanks guys ;D Link to comment https://forums.phpfreaks.com/topic/29683-getting-lost-in-my-ifs-can-someone-see-something-i-dont/#findComment-136271 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.