WAMFT1 Posted February 25, 2013 Share Posted February 25, 2013 I am trying to get my php form to pull data from an sql database but I want to use the check boxes to filter the information it brings back. Can anybody help me on how to get this coded correctly. checkbox filter.php Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/ Share on other sites More sharing options...
Barand Posted February 25, 2013 Share Posted February 25, 2013 You give the checkboxes the same values that are stored in the table (This code assumes the type columns in the database table are INT) $typeList = join(',', array_map('intval', $_POST['Type'])); $sql = "SELECT * FROM APS WHERE Paycode='ANM' AND type IN ($typeList) ORDER BY `Date` DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1414921 Share on other sites More sharing options...
WAMFT1 Posted February 25, 2013 Author Share Posted February 25, 2013 The Type column in SQL is text, would it still work? Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1414925 Share on other sites More sharing options...
Barand Posted February 25, 2013 Share Posted February 25, 2013 if text then the values need to be escaped with real_escape_string eg $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); $arr = array ( 'Jones', "O'Neill", 'Smith' ); $list = "'" . join("','", array_map(array($db,'real_escape_string'), $arr)) . "'"; echo $list; //--> 'Jones','O\'Neill','Smith' Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1414956 Share on other sites More sharing options...
WAMFT1 Posted February 25, 2013 Author Share Posted February 25, 2013 Thanks, I will give that a try Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1414972 Share on other sites More sharing options...
WAMFT1 Posted February 26, 2013 Author Share Posted February 26, 2013 Sorry to be a nuisance Barand, Still can't get the coding to work properly. I have been at it all day and about to pull my hair out. I can't get any data to display at all. Not sure what is going on. Even if I change checkbox selections noting happens. comms.php Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1415036 Share on other sites More sharing options...
Barand Posted February 26, 2013 Share Posted February 26, 2013 Your form method is GET Your checkboxes are now called SelType You haven't submitted the query Apart from that ... try $SelType = join(',', array_map('intval', $_GET['SelType'])); $sql = "SELECT * FROM APS WHERE Paycode='ANM' AND Type IN ($SelType) ORDER BY `Date` DESC"; $result = mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1415076 Share on other sites More sharing options...
WAMFT1 Posted February 26, 2013 Author Share Posted February 26, 2013 Awesome!! Thanks heaps. It is working and just how I want it to. Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1415081 Share on other sites More sharing options...
Barand Posted February 26, 2013 Share Posted February 26, 2013 The Type column in SQL is text, would it still work? What happened to "text" type Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1415084 Share on other sites More sharing options...
WAMFT1 Posted February 26, 2013 Author Share Posted February 26, 2013 I added another field into the SQL to use the int value for querying and print the text on the display. I then wrote into the export query my end to assign 1 to commission, 2 to wealthtrac and 3 to rebate to insert into the online SQL. That way I would have the best of both worlds. Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1415234 Share on other sites More sharing options...
Barand Posted February 27, 2013 Share Posted February 27, 2013 Better to create a lookup table TABLE type +------+------------+ | type | typeDesc | +------+------------+ | 1 | commission | | 2 | wealthtrac | | 3 | rebate | +------+------------+ and use a join to get the description Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1415245 Share on other sites More sharing options...
WAMFT1 Posted February 27, 2013 Author Share Posted February 27, 2013 That is what I have done internally before I export to SQL, but I guess it makes more sense to reduce the size of the file and server space. Quote Link to comment https://forums.phpfreaks.com/topic/274910-using-check-boxes-as-part-of-filter-in-phpsql/#findComment-1415262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.