如何获得 Joomla 4.x Zoo Rating Element 中每颗星的票数

问题描述 投票:0回答:0

我正在通过模板覆盖自定义 Joomla 4.x 中的 Joomla Zoo Rating 元素。文件路径为http://example.com/media/zoo/applications/blog/templates/article/elements/rating/tmpl/rating.php 代码如下:

```    
<?php
defined('_JEXEC') or die('Restricted access');
$db = Joomla\CMS\Factory::getDbo();
$query = $db->getQuery(true);

use Joomla\CMS\Language\Text;
// include assets js/css
$this->app->document->addScript('elements:rating/assets/js/rating.js');
$this->app->document->addStylesheet('elements:rating/assets/css/rating.css');
$id = $this->identifier.'-'.uniqid();
$rateJS = 'jQuery(function($){$("#'
          .htmlspecialchars($id).'").ElementRating({ url: "'
          .htmlspecialchars($link).'"});});';
$this->app->system->document->addScriptDeclaration($rateJS, 'text/javascript');
?>
<div id="<?php echo $id; ?>" class="yoo-zoo rating rt00">
<h2 class="rt01">User Reviews &amp; Rating</h2>
<div class="rt02 cfix">
<div class="rt03 cfix">
<div class="rt06">
<span class="rt07"><?php echo $rating; ?></span><span class="rt08">Based on <?php echo $votes; ?> votes</span>
</div><!-- /.rt06 -->
<div class="rt09">

<?php if ($votes < 1) : ?>
<?php for($i = $stars; $i > 0; $i--) : ?>
<div class="rt10" data-star="<?php echo $i; ?>" data-rating="0"><div class="rt11 ttip tipB" data-title="<?php echo $i.' '.Text::_('out of').' '.$stars; ?>" style="width:0%"></div></div>
<?php endfor; ?>
<?php else: ?>
<?php for($i = $stars; $i > 0; $i--) : 
$uniqueVotes = 0;
?>
<div class="rt10" data-star="<?php echo $i; ?>" data-rating="<?php echo $uniqueVotes; ?>"><div class="rt11 ttip tipB" data-title="<?php echo $i.' '.Text::_('out of').' '.$stars; ?>" style="width:<?php echo intval($uniqueVotes / $votes * 100); ?>%"></div></div>
<?php endfor; ?>
<?php endif; ?>

</div><!-- /.rt09 -->
</div><!-- /.rt03 -->
<div class="rt04">
<button id="<?php echo $id; ?>-review" class="rt05" type="button">Rate &amp; Review</button>
</div><!-- /.rt04 -->
</div><!-- /.rt02 -->   

<?php if ($show_message) : ?>
<div class="vote-message">
<?php echo $rating.'/<strong>'.$stars.'</strong> '.Text::sprintf($votes == 1 ? 'rating %s vote' : 'rating %s votes', $votes); ?>
</div>
<?php endif; ?>

<?php if ($show_microdata) : ?>
<div itemscope itemtype="http://data-vocabulary.org/Review-aggregate">
<meta itemprop="itemreviewed" content="<?php echo $this->getItem()->name; ?>" />
<div itemprop="rating" itemscope itemtype="http://data-vocabulary.org/Rating">
<meta itemprop="average" content="<?php echo number_format($rating, 1); ?>" />
<meta itemprop="best" content="<?php echo $stars; ?>" />
</div>
<meta itemprop="votes" content="<?php echo $votes; ?>"/>
</div>
<?php endif; ?>
</div><!-- /.rt00 -->    
```

其中 $rating 是总体评分,$votes 是所有参与投票的用户的总票数,$stars 是一个变量,它可能是 5 或 10,或者其他基于我使用的评分系统的变量。现在的问题是$uniqueVotes,$uniqueVotes 必须是每个star 的票数。我如何获得 $uniqueVotes 的价值。

json格式的SQL示例数据如下:

```    
[
{"type":"header","version":"5.2.0","comment":"Export to JSON plugin for PHPMyAdmin"},
{"type":"database","name":"bspk_db"},
{"type":"table","name":"uzlei_zoo_rating","database":"bspk_db","data":
[
{"id":"8","item_id":"144","element_id":"c2f19082-507f-4778-9ddb-aa2d6889d6be","user_id":"0","value":"2","ip":"127.0.0.1","created":"2023-02-19 02:41:00"}
]
}
]    
```

sql的sampledata包含以下数据:

"id" = rating id of the Zoo Rating Element.
"item_id" = the item id of the Zoo Items.
"value" = indicates the stars a user give.
"user_id" = id of the joomla user who participated in voting. If the "user_id = 0" then it means it this vote is given by the unregistered user.
"ip" = in this column IP address of the user is recorded.
php mysql joomla
© www.soinside.com 2019 - 2024. All rights reserved.