Jump to content


Photo

Using check boxes as part of filter in PHP/SQL


  • Please log in to reply
11 replies to this topic

#1 WAMFT1

WAMFT1

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

Posted 25 February 2013 - 01:37 AM

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.

Attached Files



#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,174 posts
  • LocationCheshire, UK

Posted 25 February 2013 - 04:47 PM

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

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 WAMFT1

WAMFT1

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

Posted 25 February 2013 - 05:01 PM

The Type column in SQL is text, would it still work?

#4 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,174 posts
  • LocationCheshire, UK

Posted 25 February 2013 - 05:53 PM

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'

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#5 WAMFT1

WAMFT1

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

Posted 25 February 2013 - 06:23 PM

Thanks, I will give that a try

#6 WAMFT1

WAMFT1

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

Posted 25 February 2013 - 10:40 PM

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.

Attached Files



#7 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,174 posts
  • LocationCheshire, UK

Posted 26 February 2013 - 05:51 AM

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

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#8 WAMFT1

WAMFT1

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

Posted 26 February 2013 - 06:40 AM

Awesome!! Thanks heaps. It is working and just how I want it to.



#9 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,174 posts
  • LocationCheshire, UK

Posted 26 February 2013 - 07:09 AM

The Type column in SQL is text, would it still work?

What happened to "text" type :confused:


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#10 WAMFT1

WAMFT1

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

Posted 26 February 2013 - 06:26 PM

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. 



#11 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,174 posts
  • LocationCheshire, UK

Posted 26 February 2013 - 07:30 PM

Better to create a lookup table

 

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

 

and use a join to get the description


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#12 WAMFT1

WAMFT1

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts

Posted 26 February 2013 - 08:37 PM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com