Forums

If the query result is empty...

menchee 20 Jun, 2009
Hi,
In the new version of CC, how can I check if there is no result from the query?

If the table is empty or at least the WHERE statement can't find any record, usually we get an empty page (front-end, standard connection). Maybe, if there is something in the header or footer, it appears.
But I want to show a message if there is nothing to present...

Max, related to this question and to similar others, is it possible to get from you a list of objects and variables which we can use in php code (like $MyRow for example)?
The same goes for CF (for example - if I edit a record, what is the container of the table row for that record?

Emanuel.
GreyHead 20 Jun, 2009
Hi Emmanuel,

I've just done a little digging and I found that this will work in the Header box
<?php
$MyData =& CFChronoConnectionData::getInstance($MyConnection->connectionrow->id);
if ( !count($MyData->getDataRows() ) ) {
  echo "No records found";
} 
?>

Bob

PS A variable list would be great!
menchee 20 Jun, 2009
Hey Bob,

Thanks. It works.

Actually I found this class before, but thought that maybe there is already an object ready to work with, so it won't be necessary to go to the database again and through the entire class.

But this is a call for Max I guess..

Anyway, thanks again!!
menchee 20 Jun, 2009
At the end, I put the code in the footer, since I wanted to control also the appearance of the pagination (no records matched, no pagination).

Any way, if someone is interested, here is the code I use (as I said, in the footer block of code in the general tab of connectivity), with some comments:

<?php 
global $category;
//the query is based on a category from a dedicated table.
//The variable was defined in the header and imported to the footer by using the global definition.
//I use it to construct a dynamic message. Not necessary in other cases.

$no_files = 0;
//a flag for convenience. Default is 0, meaning I guess there are matched records to present.

$MyData =& CFChronoConnectionData::getInstance($MyConnection->connectionrow->id);
//returnes the array with the matched records of the current connection

if ( !count($MyData->getDataRows() ) ) {
$no_files = 1;
echo 'We are sorry,<br/>Currently there are no files for download under the ' . strtolower($category->category) . ' section.';
//The message which will be presented if there are no records to present. The dynamic category name is specific to my needs and not necessary. 
}
?>

{new_record}<br />

<?php if($no_files == 0): //only if there are records to present, I add the pagination?>
{pagination}
<?php endif; ?>

So now, If there are matched records, I get the page with the relevant content from the table and a pagination at the end.
If there aren't matched records, I get a dynamic message.
This way I save a bit of the embarrassment from my client, how supposes to update the table by his own 🤣

There is cleaner way to write the code, but I thought it will be clearer for novice joomla programmers.

Emanuel.
This topic is locked and no more replies can be posted.