Jump to content

Using check boxes as part of filter in PHP/SQL


WAMFT1

Recommended Posts

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";

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'

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

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);
  

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. 

Better to create a lookup table

 

 

TABLE type
 +------+------------+
 | type | typeDesc   |
 +------+------------+
 |  1   | commission |
 |  2   | wealthtrac |
 |  3   | rebate     |
 +------+------------+

 

and use a join to get the description

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.