Link to home
Start Free TrialLog in
Avatar of ivanhalen
ivanhalenFlag for Italy

asked on

Filter a PHP/MySQL recordset as I did in Classic ASP with ADO Filter

Hello,
In the good old Classic ASP days I used sometimes to filter a recordset like this:
myRS = "SELECT myid, myname, mykey FROM mytable" ' just to give the idea
...
myRS.Filter = "mykey = " & myfirstvariable
...[loop through filtered records]...
myRS.Filter = "mykey = " & mysecondvariable
...[loop through filtered records]...

Open in new window

In this way, I had to create a recordset only once, instead of creating multiple recordsets:
myRS1 = "SELECT myid, myname, mykey FROM mytable WHERE mykey = " & myfirstvariable
...[loop]...
myRS2 = "SELECT myid, myname, mykey FROM mytable WHERE mykey = " & mysecondvariable
...[loop]...

Open in new window


Well, is there a similar "filter" capabilities in PHP/MySQL, or should I create so many recordset as are my filters?
Thanks
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,

I think this could be done with place markers as in example 2 in this link?  
http://php.net/manual/en/pdo.prepare.php

Darren
Avatar of zappafan2k2
zappafan2k2

How about using "IN" instead of "="?
// assuming you have a list of keys in array $myKeys
$myRS = "SELECT myid, myname, mykey FROM mytable WHERE mykey IN (" . implode(',', $myKeys) . ");";
// loop

Open in new window

Avatar of ivanhalen

ASKER

@darren-w-: this seems a nice solution, but what about without PDO?
@zappafan2k2: mmm... I can't understand how your example could be useful to me... the idea is to reach something like:
create a general recordset
filter only records that match a certain parameter X
loop only the filtered records X
re-filter only records that match a certain parameter Y
loop only the filtered records Y
etc...

Open in new window

I need this becouse my HTML structure MUST be so:
<table>
// here goes the first filter
<tbody>
// loop through the filtered records
</tbody>
// here goes the second filter
<tbody>
// loop through the filtered records
</tbody>
etc...
</table>

Open in new window

And no, I can't put my records in a single <tbody>...</tbody>, I MUST use as many as my filters are
ASKER CERTIFIED SOLUTION
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial