40544

Retrieve numbers and turn as star [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/44491290/display-star-in-front-end-based-on-rating" dir="ltr" rel="nofollow">Display Star in Front End based on rating</a> <span class="question-originals-answer-count"> 3 answers </span> </li> </ul>

Is that possible to I show my database number eg. 3 as font-awesome numbers using PHP?

I have Testimonials table where people write their feedback and give stars (these stars save as number 1 to 5), now i can show then in my template as number it is, but i want to know is there anyway to show them as font-icon?

Using: Laravel PHP framework.

UPDATE

I added this to my testimonial model and now i return stars but issue is all of them are empty stars and not get color to selected numbers.

public function tesstars() { $rates = ''; $stars = floor($rates); $rates = ''; $j = 0; for ($i = 0; $i < 5; $i++) { for ($j; $j < $stars; $j++) { $rates .= '<span class="fa fa-star"></span>'; $i++; } if ($i < 5) { $rates .= '<span class="fa fa-star-o"></span>'; } } return $rates; }

<a href="https://i.stack.imgur.com/GqlnT.png" rel="nofollow"><img alt="image one" class="b-lazy" data-src="https://i.stack.imgur.com/GqlnT.png" data-original="https://i.stack.imgur.com/GqlnT.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer1:

You can do it but just simple foor loop like this

@for ($i = 0; $i < $testimonial->star; $i++) <span class="fa fa-star"></span> @endfor @for ($i = $testimonial->star; $i < 5; $i++) <span class="fa fa-star-o"></span> @endfor

This is simple Javascript Example with for loop

<pre class="snippet-code-js lang-js prettyprint-override">var star = 3; makeStar(star); function makeStar(star){ var str = ""; for(var j=1;j<=star;j++){ str += '<span onclick="makeStar('+j+')" class="fa fa-star"></span>'; } for(var k=star+1;k<=5;k++){ str += '<span onclick="makeStar('+k+')" class="fa fa-star-o"></span>'; } document.querySelector("#output").innerHTML = str; } <pre class="snippet-code-css lang-css prettyprint-override">.fa{ width:50px; height:50px; font-size:50px; } .fa-star::before{ content:'*'; color:green; } .fa-star-o::before{ content:'*'; color:lightgrey; } <pre class="snippet-code-html lang-html prettyprint-override"><div id="output"></div>

Answer2:

You can do this in your view:

@if ($rating > 0) @while ($rating > 0) <i class="fa fa-star" aria-hidden="true"></i> {{ $rating = $rating - 1 }} @endwhile @endif

Answer3:

Try this:

$stars = 3; for($i = 0; $i < 5; $i++) { if($i < $stars){ $rates .= '<span class="fa fa-star"></span>';//coloured star }else{ $rates .= '<span class="fa fa-star-o"></span>';//colourless star } }

Answer4:

SOLVED

Well as I couldn't make it happen from model I decided to make else,elseif in my blade and load stars manually in there.

Thanks for all helps.

@if($testimonial->star == 1) <span class="fa fa-star"></span> <span class="fa fa-star-o"></span> <span class="fa fa-star-o"></span> <span class="fa fa-star-o"></span> <span class="fa fa-star-o"></span> @elseif($testimonial->star == 2) <span class="fa fa-star"></span> <span class="fa fa-star"></span> <span class="fa fa-star-o"></span> <span class="fa fa-star-o"></span> <span class="fa fa-star-o"></span> @elseif($testimonial->star == 3) <span class="fa fa-star"></span> <span class="fa fa-star"></span> <span class="fa fa-star"></span> <span class="fa fa-star-o"></span> <span class="fa fa-star-o"></span> @elseif($testimonial->star == 4) <span class="fa fa-star"></span> <span class="fa fa-star"></span> <span class="fa fa-star"></span> <span class="fa fa-star"></span> <span class="fa fa-star-o"></span> @else($testimonial->star == 5) <span class="fa fa-star"></span> <span class="fa fa-star"></span> <span class="fa fa-star"></span> <span class="fa fa-star"></span> <span class="fa fa-star"></span> @endif

Recommend

  • Get common part between two strings c# [closed]
  • Django ModelForm not saving data
  • hasManyThrough with an intermediate pivot table
  • How can I use “not like” on laravel mongodb?
  • PHP PDO Add filter to all queries
  • Cordova 3.6: how can I extract GPS Exif data from photo library in Android?
  • Lumen - Change default Storage path
  • Replacing NULL value in current row based on 'closest' matching 'earlier' row. (
  • JQuery: select paragraphs that only contain “ ” [duplicate]
  • laravel use $app inside routes error Undefined variable: app
  • Use Laravel @include directive with non PHP files?
  • Laravel 5 - Validate Multiple Request - do all Requests at the same time
  • Laravel S3 File Upload MimeType Issue
  • PHP Carbon “month()” method generates wrong DateTime
  • Laravel 5 how to include autoload.php
  • Saving Files in silverlight and asynchronous call backs
  • iOS - Changing frame of a subview of a subclassed UIView (using storyboard with auto layout enabled)
  • Laravel at least one field is required
  • Scala multiline string placeholder
  • Does Mobilefirst provide a provision to access web services directly?
  • How can I set a binding to a Combox in a UserControl?
  • xtable package: Skipping some rows in the output
  • AJAX Html Editor Extender upload image appearing blank
  • How to define and use opencv mat of user type
  • C++ Partial template specialization - design simplification
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • javaw.exe and eclipse startup problems
  • Cassandra Data Model
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Getting Messege Twice Using IMvxMessenger
  • Android Google Maps API OnLocationChanged only called once
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • Python/Django TangoWithDjango Models and Databases
  • How to push additional view controllers onto NavigationController but keep the TabBar?