Negating a where clause

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markyboy17
Forum Newbie
Posts: 7
Joined: Mon Jan 16, 2012 3:38 am

Negating a where clause

Post by markyboy17 »

OK i have a page which posts to three pages within it like this
http://localhost/EDI_Project/DisplayRes ... orders=yes
http://localhost/EDI_Project/DisplayRes ... nvoice=yes
http://localhost/EDI_Project/DisplayRes ... credit=yes

each one of these links has its own button and code

at the moment i have a basic select stored proc in SQL and it prints a table depending on what one of the above URL's is loaded.
i am now creating a filter for each on of these pages using a drop down
my question is would it be possible to add a where clause to the stored procedure but then if nothing is entered as a parameters it would just display all the records as before. this would really shorten my code if i can just have a where clause that when nothings entered it takes all rows.
thanks in advance
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Negating a where clause

Post by Celauran »

You could try something like this:

Code: Select all

$where = 1;
if (isset($_GET['where']))
{
    $where = $_GET['where'];
}

$sql = "SELECT foo, bar FROM tablename WHERE {$where}";
markyboy17
Forum Newbie
Posts: 7
Joined: Mon Jan 16, 2012 3:38 am

Re: Negating a where clause

Post by markyboy17 »

Thanks but that didn't work
i have solved the problem so if anyone is wondering you have to change the query in the stored proc to this

create proc SelectPracticeIf (@name char(50))
as
if (@name = '')
begin
select customer_id,name,sales_rep,credit_limit from customer
end
else
begin
select customer_id,name,sales_rep,credit_limit from customer WHERE name=@name
end

and then in the php i have this

Code: Select all

$dropdown = $_POST['DropDownList'];
                if(!(isset ($dropdown)))
                {
                    $dropdown = '';
                }
$sql = "SelectPracticeIf ?";
                    $params = array(&$dropdown);
                    $results = sqlsrv_query($conn,$sql,$params);
i appreciate your help thought i would post this if anyone else was wondering
Post Reply