setQuery LIMIT parameter

534 views
Skip to first unread message

gioarvan

unread,
Feb 23, 2011, 6:17:53 AM2/23/11
to Joomla! General Development
Hello guys..

i want to limit my results with setQuery(), i have this block of code:

function showHotArticle($params) {

$db = &JFactory::getDBO();

$count = intval($params->get('count', 5));

$query = "SELECT title,id,hits ".
"FROM ".$db->nameQuote('#__content')." WHERE state" .
"=" . $db->Quote('1') . " ORDER BY hits desc ;";

$db->setQuery($query, 0, $count);

$rows = $db->loadObjectList();
if($rows){
$i = 0;
$lists = array();
foreach ( $rows as $row )
{

$lists[$i]->link = JRoute::_( 'index.php?view=article&id='.
$row->id );

$lists[$i]->text = htmlspecialchars( $row->title );
$i++;
}

return $lists;
}
}

}

but when i use the $count parameter in sqlQuery nothing
returned...when i use sqlQuery without parameters i get my results. I
tried to build the query with the LIMIT 0,5 option but..did not work.

any suggestions?

gioarvan

unread,
Feb 23, 2011, 6:31:22 AM2/23/11
to Joomla! General Development
ok. this is working with this:

$query = "SELECT title,id,hits ".
"FROM ".$db->nameQuote('#__content')." WHERE state" . "=" . $db-

>Quote('1') . " ORDER BY hits desc LIMIT 0,".$count.";";

but why i cant use sqlQuery to limit the results?

Andy Nagai

unread,
Feb 23, 2011, 11:23:12 AM2/23/11
to joomla-de...@googlegroups.com
I think you need to take out the semi-colon in the sql and setQuery()
with limit will work.

> --
> You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
> To post to this group, send an email to joomla-de...@googlegroups.com.
> To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
>
>

Andrew Eddie

unread,
Feb 23, 2011, 4:49:31 PM2/23/11
to joomla-de...@googlegroups.com
I suggest you turn Site Debug on, and turn on the Debug system plugin
and have a look for that query in the list. That might give you a
clue as to what's going wrong.

Also, throwing in a:

echo $db->getQuery();

never hurts.

Regards,
Andrew Eddie
http://learn.theartofjoomla.com - training videos for Joomla 1.6 developers

gioarvan

unread,
Feb 26, 2011, 6:48:04 AM2/26/11
to Joomla! General Development
Thanks guys for your tips. it is working perfectly now!

also, i would like to thanks Andrew for the great tutorials about
Joomla 1.6 Development. I am waitng for more material in the
future..thats a request! :)

Keep in touch!

On 23 Feb, 23:49, Andrew Eddie <mambob...@gmail.com> wrote:
> I suggest you turn Site Debug on, and turn on the Debug system plugin
> and have a look for that query in the list.  That might give you a
> clue as to what's going wrong.
>
> Also, throwing in a:
>
> echo $db->getQuery();
>
> never hurts.
>
> Regards,
> Andrew Eddiehttp://learn.theartofjoomla.com- training videos for Joomla 1.6 developers
Reply all
Reply to author
Forward
0 new messages