Keyword

Accessing K2 rating information

  • William Gollinger
  • William Gollinger's Avatar Topic Author
  • Offline
  • New Member
More
13 years 5 months ago #58032 by William Gollinger
Accessing K2 rating information was created by William Gollinger
I'm writing a module that incorporates ratings from K2 articles, but when I query the database for this information, I haven't been able to process the results of queries properly. I copied the header and some code from com_K2/models/item.php since that file makes an SQL query for rating data.

Here's the gist of it:
<?php
//don't allow other scripts to grab and execute our file
defined('_JEXEC') or die('Direct Access to this location is not allowed.');

jimport('joomla.application.component.model');
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');

$db = & JFactory::getDBO();
$query = "SELECT * FROM #__k2_rating WHERE 'itemID'=27";
$db->setQuery($query,0,1);
$row = $db->loadObject();
if($row){
	$itemID = $row['itemID'];
} else {
	$itemID = "Huh?";
}

echo "Database prefix is : " . $db->getPrefix(); 
echo "Item ID is : " . $itemID; 
?>

The output is always
"Database prefix is: jos_
Item ID is : Huh?"

It returns the right prefix, so $db must be a good database object. But clearly $db->loadObject() is returning FALSE every time. I've been trying different syntaxes (like "$db->loadObject($row);") and different methods for a couple hours now, so I know I must be missing something. PLEASE help, it's almost there, I just can't see what's wrong.

Please Log in or Create an account to join the conversation.

More
13 years 5 months ago - 13 years 5 months ago #58033 by SDKiller
Replied by SDKiller on topic Re: Accessing K2 rating information
You're using loadObject() method, so you have to retrieve value as
					
Russian K2 support on joomlaforum.ru

Please Log in or Create an account to join the conversation.

  • William Gollinger
  • William Gollinger's Avatar Topic Author
  • Offline
  • New Member
More
13 years 5 months ago #58034 by William Gollinger
Replied by William Gollinger on topic Re: Accessing K2 rating information
I tried it, but still no luck. The program is still going into the "else" clause, which is telling me that $db->loadObject() is returning false. What am I doing wrong with the database calls?

Please Log in or Create an account to join the conversation.

More
13 years 5 months ago #58035 by SDKiller
Replied by SDKiller on topic Re: Accessing K2 rating information
1. Are you shure you have item with id 27 and rating values are not empty for it ?

2. EXPLAIN SELECT does not want to accept itemID without reversed quotes - maybe its reserved.

In your query try to enclose itemID in $db->nameQuote() like
					
Russian K2 support on joomlaforum.ru

Please Log in or Create an account to join the conversation.

  • William Gollinger
  • William Gollinger's Avatar Topic Author
  • Offline
  • New Member
More
13 years 5 months ago - 13 years 5 months ago #58036 by William Gollinger
Replied by William Gollinger on topic Re: Accessing K2 rating information
Success!! :D It must have been the quote thing, because now I'm getting output "Item ID is : 27"! Thank you so much :D:D:D

Let it be known (in case anyone out there hasn't noticed) that SQL is a HUGE pain in the neck if you don't get the syntax right!

Here's the working code for reference:
<?php
//don't allow other scripts to grab and execute our file
defined('_JEXEC') or die('Direct Access to this location is not allowed.');

jimport('joomla.application.component.model');
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');

$db = & JFactory::getDBO();
$query = 'SELECT * FROM #__k2_rating WHERE '.$db->nameQuote('itemID').'=27';
$db->setQuery($query);
$row = $db->loadObject();
if(!$row){
        $itemID = 'ERROR';
} 
else{
        $itemID = $row->itemID;
}

echo "Item ID is : " . $itemID;
?>
And the output is "Item ID is : 27"

(Thanks again!)

Please Log in or Create an account to join the conversation.

More
13 years 4 months ago #58037 by BBC
Replied by BBC on topic Re: Accessing K2 rating information
Can you please give all code for parsing rating in module ?
I don´t know how to use this piece of code here.

Please Log in or Create an account to join the conversation.