我正在对管理部分的 K2 项目进行手动评级。要了解我的意思,您可以查看此图像。我在 k2 默认文件中添加了这样的代码
<?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" />
看起来它正在工作,我的意思是它为每个 K2 项目设置评级。现在我想在选择表单下显示当前评级并且不知道如何获取参数。在我的代码中,我使用 /5.00,但它只显示 0.00/5.00。我也试过 $this->row->ratingSum 和 $row->ratingSum 但它不起作用(所以我的第一个问题是如何获得这些参数?
接下来是第二个问题。我插入了重置评级的按钮,但不知道如何让它工作。在 K2 文件中,我发现了一个可以重置评级但不知道如何使用它的功能。也许some1可以帮助我?!此功能如下
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);
}