Showing Star Rating based on text file number in array [duplicate] - php

This question already has answers here:
Displaying star rating with Font Awesome icons
(4 answers)
Laravel - Star rating - Optimization?
(5 answers)
Closed last month.
I have a script which reads an array from a text file and grabs each line and produces star rating images based on number in array. The script is kind of big and was wondering if there was a better way of doing this.
<?php if ($row[2] == '5') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>
<?php } elseif ($row[2] == '4.5') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i>
<?php } elseif ($row[2] == '4') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '3.5') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '3') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '2.5') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '2') { ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '1.5') { ?>
<i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '1') { ?>
<i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '.5') { ?>
<i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } elseif ($row[2] == '0') { ?>
<i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php } ?>

Yes, better ways... A loop will help, maybe something like this (PHP demo at tio.run).
function star_printer($num)
{
$star_class = [
'blank' => 'far fa-star',
'half' => 'fas fa-star-half',
'full' => 'fas fa-star'];
$star_html = '<i class="%s"></i>';
$star_str = "";
for($i=1; $i<=5; $i++)
{
$is_half = $i==ceil($num) && ceil($num)!=$num;
$star = $is_half ? "half" : ($i>$num ? "blank" : "full");
$star_str .= sprintf($star_html, $star_class[$star]);
}
return $star_str;
}
echo star_printer(3.5);

You can work off this function (doesn't include the half stars, shows you how to do the full star rating - I just noticed you run halfs as well);
function getStars($rating){
$stars = "";
$x = 0;
while($x < $rating) {
$stars = $stars.'<i class="fa fa-star"></i>';
$x++;
}
return $stars;
}
echo getStars(3);
in your case -
echo getStars($row[2]);
You can tweak this that if the number contains a decimal you can append to the string '' for example; you get the gist :P
-------------- Edit
Tweaked it and added the half star rating :)
function getStars($rating){
$stars = "";
$x = 1;
while($x <= $rating) {
$stars = $stars.'<i class="fa fa-star"></i>';
$x++;
}
if (strpos($rating, '.') !== false) {
$stars = $stars . '<i class="fa fa-star-half">';
}
return $stars;
}
echo getStars(3.5);

function getStar($rating) {
$wholeStar = "<i class="fa fa-star"></i>";
$halfStar = "<i class="fa fa-star-half-o"></i>";
// round the rating for whole stars
$roundedRating = round($rating);
// Add the whole stars using str_repeat
$starString = str_repeat($wholeStar, round($rating));
// Add the half star if the rating is bigger than it's rounded value
if ($roundedRating < $rating) {
$starString .= $halfStar;
}
return $starString
}
echo getStar($row[2]);

Related

Sort movies with dropdown

I'm new to PHP and I'm having some trouble with a sort by with a dropdown.
I have a page of movies already sorted by genre and within that page I want to be able to sort by title, rating and year and I don't know how to do it.
In the index page I was able to sort the movies easily because they weren't already sorted by genre, so I just had to put ORDER BY something in the query and it returned what I wanted.
The problem now is that I have to check which genre is chosen at that moment and associate it with what I choose from the dropdown list and I dont know how to do it...
I don't know if this is explicit enough, but if you have any doubt please ask me...
I have the following code which I copied from an existing question here:
<form class="sortby_form" action="">
<div class="sortby" method="POST">
<select name="sort_by" class="drop-box">
<option selected hidden <?php if(isset($_POST['sort_by']) && $_POST['sort_by'] == 'option') echo 'selected="selected"'; ?> value="option">Sort by</option>
<option <?php if(isset($_POST['sort_by']) && $_POST['sort_by'] == 'title') echo 'selected="selected"'; ?> value="title">Title</option>
<option <?php if(isset($_POST['sort_by']) && $_POST['sort_by'] == 'year') echo 'selected="selected"'; ?> value="year">Year</option>
<option <?php if(isset($_POST['sort_by']) && $_POST['sort_by'] == 'rating') echo 'selected="selected"'; ?> value="rating">Rating</option>
</select>
</div>
<form>
This is the code that I have which returns the movies ordered by genre:
<?php
if (isset($_REQUEST["g"])) {
$g = $_REQUEST["g"];
} else {
$g=0;}
$q_sortg = "SELECT id_movie, title, image, plot, rating, id_genre, genre
FROM movie_genre AS mg
INNER JOIN movie AS m ON mg.movie_id_movie = m.id_movie
INNER JOIN genre AS g ON mg.genre_id_genre = g.id_genre
WHERE id_genre = '$g'";
$r_sortg = mysqli_query($dbc, $q_sortg);
$i=0;
echo '<div class="row row-eq-height">';
while ($row_sortg = mysqli_fetch_array($r_sortg)) {
if($row_sortg['id_genre'] == $g) {
echo '<div class="col-sm-3 col-lg-3 col-md-3 column">';
echo '<div class="thumbnail movie-box">';
echo '<img src="data:image/jpg;base64,'.base64_encode($row_sortg['image']).'" width="250px" height="300px" />';
echo '<div class="caption">';
echo "<h4 class=\"title-subs\"><a href='movies_info.php?m=".$row_sortg['id_movie']."'>".$row_sortg['title']."</a></h4>";
echo "<p class=\"title-subs\">".$row_sortg['plot']."</p>";
echo "</div>";
echo '<div class="ratings align-center">';
echo '<p>';
$ratings = $row_sortg['rating'];
$rates = array('$row_sortg' => $ratings);
foreach ($rates as $r => $value) {
if($value <= 1.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 2.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 3.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 4.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 5.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 6.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 7.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 8.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-o" aria-hidden="true"></i>';
}
else if($value <= 9.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
}
else if($value <= 10.0){
echo '<p>'.$value.'</p>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
echo '<i class="fa fa-star" aria-hidden="true"></i>';
}
}//fecha o foreach
echo '</p>';
echo '</div>';
echo "</div>";
echo "</div>";
$i++;
if ($i%4 == 0){
echo '</div><div class="row row-eq-height">';
} //fecha o if
} //fecha o While
}
?>
Here's the link to the current website:
http://www.zoomlab.pt/2016-web2/pub/grupo4/index.php
In the first page that appears its possible to sort by with no problem.
The problem is when you click in one of the genres in the left column. They appear ordered by genre and then I cant get the sort by to work...
I was initially trying to use bootstrap dropdown but it uses <li><a> instead of <select> and <option>, so I didn't know if it was possible.
Thanks in advance for any help possible.
It looks like you just need to resubmit the form once a user changes the sort option? If so, you'd need to use JavaScript to add an onchange event on your <select>, which will resubmit the form:
<script>
window.addEventListener('DOMContentLoaded', function () {
var form = document.querySelector('form.sortby_form'),
select = form.querySelector('select[name="sort_by"]');
select.addEventListener('change', function () {
form.submit();
});
});
</script>
Also, if this is for a production web site, you really should not use raw user input as part of a SQL query. See: http://php.net/manual/en/mysqli-stmt.bind-param.php

how do I edit the output of fgetcsv

I'm using eKomi and want to pull in reviews to my website. I've managed to get this working but there's a slight problem. Currently every single review is getting added, and in reverse order.
You'll see from the code below that I am using fgetcsv.
Is it possible to limit to the 12 latest reviews? and then order them from newest to oldest?
Here's the code I've put together.
<div class="row">
<div class="row__colspaced">
<?php
$row = 1;
if (($handle = fopen("http://api.ekomi.de/get_feedback.php?interface_id=66630&interface_pw=f2d097e83db1880e85e2f77aa&range=3m&type=csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo "<div class='colspan12-4 colspan6-3 as-grid with-gutters'><div class='content-module--review-list__item item' data-mh='review-item'>";
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
if($c == 0){
echo gmdate('Y-m-d H:i:s',$data[$c]);
}
else if($c == 2){
if($data[$c] == 1){
echo '<div class="fa-star-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
</div>';
}
if($data[$c] == 2){
echo '<div class="fa-star-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
</div>';
}
if($data[$c] == 3){
echo '<div class="fa-star-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
</div>';
}
if($data[$c] == 4){
echo '<div class="fa-star-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o"></i>
</div>';
}
if($data[$c] == 5){
echo '<div class="fa-star-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
</div>';
}
}
else {
echo '<div class="inner-item item'.$c.'">' . $data[$c] . "</div>";
}
}
echo "</div></div>";
}
fclose($handle);
}
?>
</div>
</div>

PHP Hide text from array

I'm pulling data from an array, but show certain parts of the data above others, I've managed to choose certain text to go above others. Is there anyway to hide text from the array if it matches the piece of text I give it, using the below code?
<?php $datas = array_slice($facilities, 0, $leftoshownumber); // First 6 items
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
This is the full code for this section
<div class="checkmark-outer">
<?php $startnumber = 6; $parkingnumber = 0; $gardennumber = 0; $dishwashernumber = 0; $summerrentnumber = 0; ?>
<?php if (strpos($facilitiesstring, 'Reduced Summer') !== false) { $summerrentnumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Reduced Summer Rent Available</div></div>';}; ?>
<?php if (strpos($facilitiesstring, 'Parking') !== false) {$parkingnumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Parking Available</div></div>';}; ?>
<?php if (strpos($facilitiesstring, 'Garden') !== false) {$gardennumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Garden</div></div>';}; ?>
<?php if (strpos($facilitiesstring, 'Dishwasher') !== false) {$dishwashernumber = 1; echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">Dishwasher</div></div>';}; ?>
<?php $totalfixednumber = $parkingnumber + $gardennumber + $dishwashernumber + $summerrentnumber; $leftoshownumber = $startnumber - $totalfixednumber;?>
<?php $datas = array_slice($facilities, 0, $leftoshownumber); // First 6 items
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
<div class="clearfix"></div>
<div class="read-more-show hide">Show More</div><div class="read-more-content"><?php $datas = array_slice($facilities, 6); // Items after 6
foreach($datas as $data) {
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?><div class="clearfix"></div><div class="read-more-hide hide">Show Less</div></span></div>
</div>
Any help would be great!
Do you mean you don't want to print the whole line, when the name matches a given variable?
Then I would do:
$iDontWant="bla"
foreach($datas as $data) {
if($data->Name == $iDontWant) continue;
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
EDIT:
if you want multiple names not to show you could do:
$iDontWant1="bla1"
$iDontWant2="bla2"
$iDontWant3="bla3"
foreach($datas as $data) {
if($data->Name == $iDontWant1 || $data->Name == $iDontWant2 || $data->Name == $iDontWant3) continue;
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
('||' stands for 'or' by the way)
So basically what you are doing is saying, when
either the name is the same as the content of $iDontWant1, which is "bla1"
or the name is the same as $iDontWant2, which is "bla2"
or the name is the same as the content of $iDontWant3, which is "bla3"
do continue (don't print this part).
However if you know what "bla1", "bla2" ... are, it is easier to just do
foreach($datas as $data) {
if($data->Name == "bla1" || $data->Name == "bla2" || $data->Name == "bla3") continue;
echo '<div class="checkmark-33"><div class="fa-stack fa-1x checkmark-icon"><i class="fa fa-circle fa-stack-2x icon-background"></i><i class="fa fa-check fa-stack-1x icon-text"></i></div><div class="checkmark-inner">'. $data->Name .'</div></div>';
}; ?>
You will just compare to the String directly instead of comparing to the variable containing the string.
I hope I can help you with this.
But if you'd like to do more stuff with PHP I'd recommend a tutorial like https://secure.php.net/manual/en/tutorial.php to really understand the concepts behind this language

Simplify star rating code

I'm currently working on a rating system for a website but I'm stuck with the conditional statements in RAIN Language. I have 2 questions:
Is there a way to make this piece of code more efficient?
Is it a problem to leave it like this or will it slow down the loading speed of a website?
<div class="product-rating">
{% if (product.score * 5) < '1.3' %}
<i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '1.3' and (product.score * 5) < '1.7' %}
<i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '1.7' and (product.score * 5) < '2.2' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '2.2' and (product.score * 5) < '2.7' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '2.7' and (product.score * 5) < '3.2' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '3.2' and (product.score * 5) < '3.7' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '3.7' and (product.score * 5) < '4.2' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i>
{% endif %}
{% if (product.score * 5) > '4.2' and (product.score * 5) < '4.7' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i>
{% endif %}
{% if (product.score * 5) > '4.7' %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>
{% endif %}
</div>
It's better to implement a function to draw the score, something like this without templating :
function drawScore($score){
$solid = floor($score);
$empty = 4 - $solid;
$half = $score - $solid;
for($i = 1; $i <= $solid; $i++){
echo '<i class="fa fa-star"></i>';
}
if($half > 0){
echo '<i class="fa fa-star-half-o"></i>';
}
for($j = 0; $j < $empty; $j++){
echo '<i class="fa fa-star-o"></i>';
}
}
Then all you need inside <div class="product-rating"> is to call the function like:
$pScore = $product.score * 5;
drawScore($pScore);
Try this (not sure if there are syntax problems as I wrote it here..
The thing I change is that numbers are now not strings and I don't skip all numbers which you have set for ranges.
Using elseif confirms that the calculation is bigger than the previous IF. So there shouldn't be any problem.
+ This calculation score*5 can be made before the how IF statement as somebody purposed.
<div class="product-rating">
{% if (product.score * 5) < 1.3 %}
<i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 1.7 %}
<i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 2.2 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 2.7 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 3.2 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 3.7 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 4.2 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i>
{% elseif (product.score * 5) < 4.7 %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-half-o"></i>
{% else %}
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>
{% endif %}
</div>
Here is a method using a for loop.
<div class="product-rating">
{% if (product.score * 5) < 1.3 %}
<i class="fa fa-star"></i>
{% endif %}
# array = [1.3,2.2,3.2,4.2]
{% for item in array %}
{% if if (product.score * 5) > item %}
<i class="fa fa-star"></i>
{% elseif (product.score * 5) < (item + 0.5) %}
<i class="fa fa-star-half-o"></i>
{% endif %}
{% endfor %}
{% if (product.score * 5) > 4.7 %}
<i class="fa fa-star"></i>
{% endif %}
</div>

Trying to retrieve data from a string in database

I have the following output in one field in Joomla ($item->attribs):
{"show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_layout":"ja_teline_v:p4p50","content_type":"p4p","ads":"1","ctm_boxer":{"name":["Floyd Mayweather","Manny Pacquiao","Wladimir Klitschko","Miguel Cotto","Gennady Golovkin","Sergey Kovalev","Guillermo Rigondeaux","Juan Manuel Marquez","Carl Froch","Saul Alvarez","Danny Garcia","Roman Gonzalez","Amir Khan","Mikey Garcia","Jhonny Gonzalez","Leo Santa Cruz","Adonis Stevenson","Abner Mares","Terence Crawford","Adrien Broner","Timothy Bradley","Juan Francisco Estrada","Robert Guerrero","Bernard Hopkins","Marco Huck","Nicholas Walters","Sergio Martinez","Julio Cesar Chavez Jr","Arthur Abraham","Nonito Donaire","Orlando Salido","Fernando Montiel","Humberto Soto","Marcos Maidana","Naoya Inoue","Donnie Nietes","Alexander Povetkin","Juan Carlos Reveco","Peter Quillin","Lucas Matthysse","Brian Viloria","Jamie McDonnell","Juergen Braehmer","Kell Brook","Deontay Wilder","Erislandy Lara","Jean Pascal","Carl Frampton","Omar Narvaez","Tomoki Kameda"],"rating":["5","5","4","4","3","3","3","4","3","3","3","3","3","3","3","2","3","2","2","3","3","2","3","5","2","2","3","2","3","4","2","3","2","3","3","2","2","2","2","2","2","2","2","1","1","1","2","1","2","1"],"movement":["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],"record":["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","1-0, 1 KO","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","1-0","0","0","0","0","0"],"ranking":["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50"],"highlight":["","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]},"ctm_topic_id":""}
How do I retrieve every value from ctm_boxer -> name in a list?
I've tried the below, but to no avail!
<?php $boxers = $item->attribs->get('ctm_boxer', array()); ?>
<?php foreach($boxers['name'] as $index => $boxer_type): ?>
<?php echo $boxer_type; ?>
but it's returning 'Fatal error: Call to a member function get() on a non-object'
Can anyone help as to why please?
Thanks,
Matt
As you can see from var_dump inside $item->attrib is a string shown as string(2494). Not an object/PHP class. That's why you're getting Fatal Error: Call to a member function on a non-object. Because again, $item->attrib is a normal string.
The -> dereference operator can only be used on PHP Objects/Classes.
More on how to use the object operator.
That variable seems to contain a string in JSON format.
Try the following:
$BoxerCollectionObject = json_decode($item->attribs);
$boxerArray = $BoxerCollectionObject->ctm_boxer->name;
foreach ($boxerArray as $boxer) {
var_dump($boxer); //Should output the name of your boxers.
}
DEMO
That's great - thanks - works perfectly... I expanded it a bit with the following to bring in some of the other parts of the string:
<?php
$BoxerCollectionObject = json_decode($item->attribs);
$boxerArray = $BoxerCollectionObject->ctm_boxer->name;
$boxerRating = $BoxerCollectionObject->ctm_boxer->rating;
?>
<table class="table">
<tbody>
<?php $i = 1;?>
<?php $ic = 1;?>
<?php foreach ($boxerArray as $index => $boxer) : ?>
<tr itemprop="boxer" class="boxer<?php echo $ic++; ?>">
<td><strong><?php echo $i++; ?></strong></td>
<td><span><?php echo $boxer; ?></span></td>
<td itemprop="rating"><?php if ($boxerRating[$index] <= 0): ?>
<i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php endif;?>
<?php if ($boxerRating[$index] == 1): ?>
<i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php endif;?>
<?php if ($boxerRating[$index] == 2): ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php endif;?>
<?php if ($boxerRating[$index] == 3): ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>
<?php endif;?>
<?php if ($boxerRating[$index] == 4): ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i>
<?php endif;?>
<?php if ($boxerRating[$index] >= 5): ?>
<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>
<?php endif;?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
Thanks again, Matt.

Categories