Forums

Filter and display database row based on two or more columns

phicts 26 Jan, 2016
Where and how to filter and display data based on two or more columns or fields in a database table?
The database table consists of the following columns or fields, among others:
[list]ID (the entry id)
user_id (the id of the user who created the entry)
ProviderID
RequesterID[/list]

Currently, my filter code in the Conditions box is:
<?php
$user = \JFactory::getUser();
return array('BayanihanAdminDispatch.providerid' => $user->id);
?>

This code displays only the data where the logged-in user's ID is the same as the ProviderID.

In one instance, I also want to display the data or rows where the logged-in user's ID is the same as the RequesterID. In other words, if either of the two fields - ProviderID OR RequesterID, is the same, then it should be displayed.

In another instance, I want only the records where both the ProviderID AND the RequesterID are the same.

I've been googling for the correct code for 3 days already and still no luck.

What is the correct code and how do I go about it, please? Thanks in advance.
GreyHead 26 Jan, 2016
Answer
Hi phicts,

Please see the Complex Query example in this FAQ

Bob
phicts 26 Jan, 2016
Thank you very much Bob. I would not have made it without you pointing me to the exact tutorial. The code I now use:
<?php
$user = \JFactory::getUser();
return array (":BayanihanAdminDispatch.providerid = '$user->id' OR BayanihanAdminDispatch.requesterid = '$user->id'");
?>

.... and it works!

Tip: the colon as well as the double quotation and single quotation marks are really important to create the correct syntax.
phicts 26 Jan, 2016
Thank you very much Bob. I would not have made it without you pointing me to the exact tutorial. The code I now use:
<?php
$user = \JFactory::getUser();
return array (":BayanihanAdminDispatch.providerid = '$user->id' OR BayanihanAdminDispatch.requesterid = '$user->id'");
?>

.... and it works!

Tip: the colon as well as the double quotation and single quotation marks are really important to create the correct syntax.
This topic is locked and no more replies can be posted.