如何获得评级值并将其重置为K2

时间:2016-06-06 12:07:16

标签: php joomla rating k2

我在管理部分对K2项目进行手动评分。要了解我的意思,您可以查看image。我在k2 defauld文件中添加了这样的代码



<?php
        $submittedValue = "";
        $value0 = "0.0";
        $value1 = "0.5";
        $value2 = "1.0";
        $value3 = "1.5";
		$value4 = "2.0";
		$value5 = "2.5";
        $value6 = "3.0";
        $value7 = "3.5";
        $value8 = "4.0";
		$value9 = "4.5";
 		$value10 = "5.0";

if (isset($_POST[ $row->id ])) {
            $submittedValue = $_POST[ $row->id];
        }
        ?>
        <form name="ratingk2" method="post">
			<select project="<?php echo $row->id;?>" id="<?php echo $row->id; ?>" name="<?php echo $row->id; ?>" style="width:70px;">
         <option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>0</option>
         <option value = "<?php echo $value1; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>1</option>
         <option value = "<?php echo $value2; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>2</option>
         <option value = "<?php echo $value3; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>3</option>
		 <option value = "<?php echo $value4; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>4</option>
		 <option value = "<?php echo $value5; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>5</option>
         <option value = "<?php echo $value6; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>6</option>
         <option value = "<?php echo $value7; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>7</option>
         <option value = "<?php echo $value8; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>8</option>
		 <option value = "<?php echo $value9; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>9</option>
         <option value = "<?php echo $value10; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>>10</option>
        </select>
        <input type="submit" name="submit" id="submit" value="Submit" />
        </form>					
					
	<?php
   $rate =  $submittedValue;

		if ($rate >= 0.5 && $rate <= 5.0)
		{
			$db = JFactory::getDBO();
			$userIP = $_SERVER['REMOTE_ADDR'];
			$query = "SELECT * FROM #__k2_rating WHERE itemID =".(int)$row->id;
			$db->setQuery($query);
			$rating = $db->loadObject();

			if (!$rating)
			{
				$query = "INSERT INTO #__k2_rating ( itemID, lastip, rating_sum, rating_count ) VALUES ( ".(int)$row->id.", ".$db->Quote($userIP).", {$rate}, 1 )";
				$db->setQuery($query);
				$db->query();
				echo JText::_('THANKS FOR RATING');

			}

		}
   ?>
						
<span><?php echo number_format(($ratingSum/$ratingCount),2); ?>/5.00</span>

<input id="resetRatingButton" type="button" value="<?php echo JText::_('K2_RESET'); ?>" class="button" name="resetRating" />

						
&#13;
&#13;
&#13;

看起来它正在工作,我的意思是它为每个K2项目设置评级。现在我想在选择表格下显示当前评级,不知道如何获取参数。在我的代码中我使用/5.00,但它只显示0.00 / 5.00。此外,我尝试了$ this-&gt; row-&gt; ratingSum和$ row-&gt; ratingSum但它不起作用(所以我的第一个问题我怎样才能得到那些副手?

第二个问题如下。我插入按钮重置评级,不知道如何使其工作。在K2文件中,我发现了一个可以重置评级并且不知道如何使用它的功能。也许some1可以帮助我?! 这个功能如下

&#13;
&#13;
function resetRating()
	{
		$mainframe = JFactory::getApplication();
		$id = JRequest::getInt('id');
		$db = JFactory::getDBO();
		$query = "DELETE FROM #__k2_rating WHERE itemID={$id}";
		$db->setQuery($query);
		$db->query();
		if ($mainframe->isAdmin())
			$url = 'index.php?option=com_k2&view=item&cid='.$id;
		else
			$url = 'index.php?option=com_k2&view=item&task=edit&cid='.$id.'&tmpl=component';
		$mainframe->enqueueMessage(JText::_('K2_SUCCESSFULLY_RESET_ITEM_RATING'));
		$mainframe->redirect($url);
	}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

重置K2评级可以使用Ajax完成 - 您将项目ID发送到特定模块,您可以在名为resetK2RatingsAjax的函数中使用该模块中的上述代码。我们在这里发布了一篇关于如何使用com_ajax的冗长帖子:http://www.itoctopus.com/how-to-use-joomlas-ajax-interface-component-com_ajax

我不记得K2存储评级的具体位置,但您可以执行以下操作:

print_r($this>row);

它会告诉您K2评级的确切位置。