1

I'm basically trying to create a Joomla JFactory query to COUNT and SELECT data if a particular field's value meets my criteria.

In "myTable", I may have several records where Field4 may either be NULL or equal to YES or NO.

Ideally, I'd like a single query that would return a COUNT of records where Field1, Field2, and Field3 equal $Field1, $Field2, and $Field3 ... AND then where Field4 = YES or NO, I'd like it to echo the text YES or NO, depending on if one of the records meets that criteria.

This code kind of does what I want, but it only returns a value of YES when Field4 equals YES. And I have a previous query above it ($db4 and $query4) that returns the COUNT that I want. Seems like there should be a more efficient and easy way to do this.

$db5 = JFactory::getDbo();
$query5 = $db5->getQuery(true);

$query5 = $db5
->getQuery(true)
->select(array('ID', 'Field4'))
->from($db->quoteName('myTable'))
->where($db->quoteName('Field1') . " = " . $db->quote($Field1))
->where($db->quoteName('Field2') . " = " . $db->quote($Field2))
->where($db->quoteName('Field3') . " = " . $db->quote($Field3))
->where($db->quoteName('Field4') . " = " . $db->quote('YES'));

$db5->setQuery($query5);
$result5 = $db5->loadResult();
$Field4 = $result5;
echo $Field4;
1
  • Welcome to JSE. Please take our tour. Commented Nov 13, 2020 at 20:05

1 Answer 1

1

There are a few tips to offer here.

  1. You only need one connection object for your file. Don't bother with multiple $db variables -- just keep re-using the same one. *Notice how your quote() and quoteName() method calls did not fail when they used an earlier $db variable.
  2. Since your Field4 column can only be NULL, YES, or NO, and you only want YES and NO values, then the simplest attack is to filter out the NULL values.
  3. To make your code more "D.R.Y.", you can pass an array of clauses to the where() method.
  4. loadResult() is appropriate if you want a single column value and have adequate WHERE logic to guarantee a maximum of one row in the result set. If you might receive more than one row (and you only want to receive one column of data), then loadColumn() is appropriate. If you intend to use ID and Field4 as a single row, then use loadRow(). If you intend to generate a result set with potentially multiple rows and multiple columns then you will need to call a method that can produce a multidimensional result set such as loadObjectList() or loadAssocList().

My recommended approach will look like this:

$db = JFactory::getDbo();
$query5 = $db
    ->getQuery(true)
    ->select($db->qn('Field4'))
    ->from($db->qn('myTable'))
    ->where([
        $db->qn('Field1') . ' = ' . $db->q($Field1),
        $db->qn('Field2') . ' = ' . $db->q($Field2),
        $db->qn('Field3') . ' = ' . $db->q($Field3),
        $db->qn('Field4') . ' IS NOT NULL',
    ]);

The output of $query5 via echo $query5->dump(); is:

SELECT `Field4`
FROM `myTable`
WHERE `Field1` = '1' AND `Field2` = '2' AND `Field3` = '3' AND `Field4` IS NOT NULL

Note: If you wanted to cache the column quoting of Field4 instead of calling qn() on it twice, you could save it as a variable.


Alternatively, if you wanted to explicitly write the YES and NO where conditions, you can use one of the two following techniques:

  1. andWhere() with OR as the glue parameter.

     ->where([
         $db->qn('Field1') . ' = ' . $db->q($Field1),
         $db->qn('Field2') . ' = ' . $db->q($Field2),
         $db->qn('Field3') . ' = ' . $db->q($Field3),
     ])
     ->andWhere(
         [
             $db->qn('Field4') . ' = ' . $db->q('YES'),
             $db->qn('Field4') . ' = ' . $db->q('NO')
         ],
         'OR'
     );
    

    This will produce:

    SELECT `Field4`
    FROM `myTable`
    WHERE 
    (`Field1` = '1' AND `Field2` = '2' AND `Field3` = '3') AND 
    (`Field4` = 'YES' OR `Field4` = 'NO')
    
  2. or an element containing an IN clause in the original where() call.

     ->where([
         $db->qn('Field1') . ' = ' . $db->q($Field1),
         $db->qn('Field2') . ' = ' . $db->q($Field2),
         $db->qn('Field3') . ' = ' . $db->q($Field3),
         $db->qn('Field4') . ' IN (' . $db->q('YES') . ',' . $db->q('NO') . ')',
     ]);
    

    This will produce:

    SELECT `Field4`
    FROM `myTable`
    WHERE `Field1` = '1' AND `Field2` = '2' AND `Field3` = '3' AND `Field4` IN ('YES','NO')
    
1
  • mickmackusa, thank you very much for your response and assistance! Sorry I'm so late getting back to you. Had a sudden death in the family and have been away from work for a couple of weeks. I will try your resolution as soon as I can. Thanks again! Commented Nov 25, 2020 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.