I am running into a problem rendering a star rating and was hoping I can get some extra eyeballs on my problem. I have the normal rating working just fine with whole numbers but I am struggling to display the half star. For example I created a service that provides me with a rating 0-5 so I get a value like 2.5, 3 or 5 etc...
Before I go and create a switch case and create an svg for each variation I was hoping to get a little a pointer. Below is what I have currently, any tips would be greatly appreciated.
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
}
else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
Ideally I would like to add a condition at the end of the loop and check for the half and echo "";
There is probably an easier way to do it but this works, checks if $starRating is a float and then rounds it up and checks against $i to place the half star in the correct position.
<?php
$totalRating = 5;
$starRating = 2.5;
for ($i = 1; $i <= $totalRating; $i++) {
if($starRating < $i ) {
if(is_float($starRating) && (round($starRating) == $i)){
echo "<img src=\"/icons/star-half.svg\">";
}else{
echo "<img src=\"/icons/star-empty.svg\">";
}
}else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
You can verify if the value of $starRating is an integer, doing something like this (considering only half values):
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if ($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
} elseif(is_int($starRating) === false) {
echo "<img src=\"/icons/star_half.svg\">";
} else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
If you want to show stars with a more precise value you can create images with the float values in the name, like "star-3.svg" (representing a .3 float value, and do something like this:
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if ($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
} elseif(is_int($starRating) === false) {
echo "<img src=\"/icons/star-" . floatval($starRating)) . ".svg\">";
} else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
But in this case you need to take care to only receive float values with one number (2.2, 3.4, etc.).
I hope it helps...
Related
Here is my Code
<?php
for ($x = 0; $x <= $n; $x++)
{
//Counting of rows
$countrow = get_rows($getvids);
if($offline == "Offline")
{
$apsys = "APSYS";
$offlined = "";
if ($getvids == $apsys || $getvids == $offlined )
{
echo "<tr>
<th>$x</th>
<th>$accntid[$x]</th>
<th>$fname[$x]</th>
<th>$lname[$x]</th>
<th>$vid[$x]</th>
<th>$vplatenum[$x]</th>
<th>$imei[$x]</th>
<th>$datas[1]</th>
<th>$datas[0]</th>
<th>$offline</th>";
echo "<th>$getvids</th>";
echo "<th><button type = 'button' class = 'btn btn-success viewbtn'> View Troubleshoot Report History</button></th>";
echo "</tr> ";
echo "total number of Rows:";
echo str_word_count($countrow);
}
else
{
}
}
else
{
}
}
?>
and here is my Function
<?php
function get_rows($getrowscount)
{
require ('db_connection.php');
$getrowscounts = $getrowscount;
//echo $getrowscounts;
return $getrowscounts;
}
//get_rows(); // call the function
?>
the table shows Two Rows with the value of = "APSYS"
I want to count the rows and show the output like this:
Total number of Rows: 2
but the output I can only get is like This:
Total number of Rows:1
Total number of Rows:1
Total number of Rows:0
can someone help me, I am a little bit confused about what will I use to count the rows?
Thanks.
How about this? Does this satifsfy your requirement?
<?php
$countrow = 0;
for ($x = 0; $x <= $n; $x++)
{
//Counting of rows
$countrow = $countrow + get_rows($getvids);
if($offline == "Offline")
{
//omitted
}
else
{
}
}
}
echo "total number of Rows:";
echo str_word_count($countrow);
?>
If you are trying to count them after fetching from the database, try to write a SQL query instead of your current approach.
Let's say I have this loop:
for($i = 1; $i <= 10; $i++){
if($i > 4){
echo 'Greater Than 4.' . '<br/>';
}
}
The previous loop will output the following:
Greater Than 4.
Greater Than 4.
Greater Than 4.
Greater Than 4.
Greater Than 4.
Greater Than 4.
As the condition is true 6 times.
What I want to do is to print this message only once so the output from the same loop and condition will be :
Greater Than 4.
I think this could be achieved by using a variable and give it this value at a specific point , But I don't know how to do it.
Well, it will be like your code expect that you print the message once:
$msg = 'There is no greater than 4';
for($i = 1; $i <= 10; $i++){
if($i > 4){
$msg = 'Greater Than 4.' . '<br/>';
// Make any additional required code here...
}
}
echo $msg;
;
Have you tried this :)
for($i=1;$i<=10;$i++){
echo ($i>4 && !isset($rslt))?$rslt='Greater Than 4.':'';
}
Or, you can loop first
then print the result
like this
$bucket = '';
for($i=1;$i<=10;$i++){
$bucket = ($i>4)?'Greater Than 4.':$bucket;
}
echo $bucket;
Hmmmm, I guess you case like this :D
$bucket = $condition = '';
$flag = 1;
for($i=1;$i<=10;$i++){
if($i>4){
$bucket = 'Greater Than 4. <br>';
$condition .= "Proses :{$flag}<br>";
$flag++;
//more condition process 6 times
}
}
echo $condition;
echo $bucket;
Or like this :D
$j=1;
for($i=1;$i<=10;$i++){
if($i>4){
if($i<=5){
echo 'Greater Than 4. <br>';
}
echo 'Proses '.($j++).'<br>';
//more condition process 6 times
}
}
Or, using the short hand
for($i= $j =0;$i<=10;$i++){
if($i>4){
echo ($i<=5)?'TheGreater Than 4. <br>':false;
$j++;
echo "Process number : {$j} <br>";
}
}
$run_once = true;
for($i = 1; $i <= 10; $i++){
if($i > 4 AND $run_once){
echo 'Greater Than 4.' . '<br/>';
$run_once = false;
}
}
you could also use if ($i == 4)
EDIT:
for($i = 1; $i <= 10; $i++){
if($i > 4){
if($i == 4){
echo 'this runs only once';
}
echo 'this runs every time';
}
}
Echoing out results from a database in a while loop so that I can echo 5 of them out at once.
Currently have put a standard string into the loop in order to test. The <li> elements have a border-bottom attribute in their stylings.
Is it possible to have the last result echoed (number 5) to have another class applied that would rule out the border?
$i = 1;
while($i <= 5) {
// On the 5th, change the class here to rule out the border-bottom.
echo "<li>jQuery &HTML5 audio player.</li>";
$i++;
}
So, somewhere in there, throw in an if statement?
Sorry. This could be really simple, but it's been a long day
Can't you use a simple check inside the loop and output depending on your counter?
<?php
$i = 1;
while($i <= 5)
{
if($i<5)
{
echo "<li>jQuery &HTML5 audio player.</li>";
}
else
{
echo "<li class='fluffeh'>jQuery &HTML5 audio player.</li>";
}
// On the 5th, change the class here to rule out the border-bottom.
$i++;
}
?>
Or if you wanted every fifth one different, you could do:
<?php
$i = 1;
while($i <= 10)
{
if($i%5!=0)
{
echo "<li>jQuery &HTML5 audio player.</li>";
}
else
{
echo "<li class='fluffeh'>jQuery &HTML5 audio player.</li>";
}
// On the 5th, change the class here to rule out the border-bottom.
$i++;
}
?>
<?php
$i = 1;
while($i <= 5){
$class = "";
if($i===5)
$class = " class=\"last\"";
echo "<li$class>jQuery &HTML5 audio player.</li>"; // On the 5th, change the class here to rule out the border-bottom.
$i++;
}
of course that this is a blind answer to a closed question, you would do greater if you follow other people answers's advice.
Rather than always using the number five as a placeholder, it would probably be better to count the elements in the array, and if it's the last one, omit the line.
$elementsCount = count($elements);
for ($index = 0; $index < $elementsCount; ++$index) {
$class = $index == $elementsCount - 1 ? 'lastElement' : 'standardElement';
echo '
<li class="', $class, '">', $elements[$index], '</li>';
}
You have already an counter, your variable $i. The variable $i which you count to know when reach 5th loop to end the loop.
Also you can use it for if- or other statments to do somethin in third or secend element or other stuff like, when $i counts 4 it should ouput other class or do a function or somenthing else.
All you have to do is to asking, when the 5 loop is reached insert an class:
<?php
$class = null;
$li_element = null;
for($i=0;$i<=5;$i++)
{
if($i==5)
{
$class="last_element";
}
$li_element .= '<li '.$class.'>...</li>';
}
echo '<ul>'.$li_element.'</ul>';
If you don't want to add to much lines to your code you could also do something like this:
$i = 1;
while ($i<= 5) {
echo ($i < 5) ? "<li>jQuery &HTML5 audio player.</li>" : "<li class='someclass'>jQuery &HTML5 audio player.</li>";
$i++;
}
Being "someclass" the style you want to apply to the last li.
I am just a beginner in PHP. I have tried to write a program for prime numbers, but the result is not correct. I couldn't find the error. How can I correct this? Here is my code:
<?php
$n=15;
for($i=2; $i<=$n; $i++)
{
echo "<br />";
for($j=2; $j<=$i-1; $j++)
{
$k=$i%$j;
if($k==0)
{
break;
}
else echo $i."is prime";
break;
}
}
?>
Try this:
<?php
$n=15;
for($i=2; $i<=$n; $i++)
{
$k = 1; //assume that it is prime
for($j=2; $j<$i; $j++) //if $i is 2, then it won't enter the loop as it will not match the condition ($j<$i)
{
$k=$i%$j;
if($k==0)
break; //if not prime, $k will be set as 0. So, break.
}
if($k!=0) // if $k <> 0, then it is prime
echo "<br />" . $i." is prime";
}
?>
Edit
Updated the code to take care of the "2"
Try this:(whitout using loop)
function is_prime($p) {
return ($p > 1) && (($p%2 >= 1) && ($p%3 >= 1) && ($p%5 >= 1)) || in_array($p, [2,3,5]);
}
echo is_prime(15);
You are breaking the loop the first time it's run by calling this basically:
if (something) {
break;
} else {
break;
}
it will break no matter what. you need to take out the last break.
Well, your code is kind of confusing to understand; at first glance it seems as if it's trying to determine primality by checking every divisor up to N, but you've got that outer-loop going on which I didn't see at first... Oh boy.
If you're just trying to figure out if some number N is prime, the following should work:
$n = 15
$prime = true;
for ($i = 2; $i < sqrt($n); $i++) {
if ($n % $i == 0) {
$prime = false;
break;
}
}
echo $n . " is " . ($prime ? "" : "not") . " prime.";
<?php
echo "TEST\r\n";
$n=15;
for($i=2; $i<=$n; $i++)
{
echo "I= $i \r\n";
for($j=2; $j<=$i-1; $j++)
{
$k = $i%$j;
if($k==0)
{
break;
} else {
echo $i."is prime \r\n";
}
break;
}
}
I think your secod for loop is incorrect.
for($i=2; $i<=$n; $i++) {
echo "<br />";
for($j=2; $j<=$i-1; $j++) {
...
The first value for $j is 2. And for the first time, the first value for $i is 2 again. Now, look at your second for loop code.
It will be like this at the first time:
for($j=2; $j<=2-1; $j++) ... // for($j=2; $j<=1; $j++)
And this condition is not valid at all: 2<=1
When I launch my web page, increment doesn't work correctly!
It should go like this: $i = from 1 to x (0,1,2,3,4,5,6 etc..).
But instead it jumps over every step giving result of (1,3,5,7 etc..).
Why is this code doing this?
<ul class="about">
<?php
$result = mysql_query("SELECT * FROM info WHERE id = 1");
while ($row = mysql_fetch_assoc($result))
{
$bioText = $row['bio'];
}
$endBioTxt = explode("\n", $bioText);
for ($i=0; $i < count($endBioTxt);)
{
if (checkNum($i) == true)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
$i++;
}
// Function to check if number is prime
function checkNum($num){
return ($num % 2) ? TRUE : FALSE;
}
?>
</ul>
Output:
Sometext!(right side)
0
1
Sometext2!(right side)
2
3
...
Please DONT do this as other suggested:
for ($i=0; $i < count($endBioTxt); $i++)
do this:
$count = count($endBioTxt);
for ($i=0; $i < $count; $i++) {
}
No need to calculate the count every iteration.
Nacereddine was correct though about the fact that you don't need to do:
$i++;
inside your loop since the preferred (correct?) syntax is doing it in your loop call.
EDIT
You code just looks 'strange' to me.
Why are you doing:
while ($row = mysql_fetch_assoc($result))
{
$bioText = $row['bio'];
}
???
That would just set $bioText with the last record (bio value) in the recordset.
EDIT 2
Also I don't think you really need a function to calculate the modulo of a number.
EDIT 3
If I understand your answer correctly you want 0 to be in the left li and 1 in the right li 2 in the left again and so on.
This should do it:
$endBioTxt = explode("\n", $bioText);
$i = 0;
foreach ($endBioTxt as $txt)
{
$class = 'left';
if ($i%2 == 1) {
$class = 'right';
}
echo '<li class="'.$class.'"><div>'.$txt.'</div></li>';
echo $i; // no idea why you want to do this since it would be invalid html
$i++;
}
Your for statement should be:
for ($i=0; $i < count($endBioTxt); $i++)
see http://us.php.net/manual/en/control-structures.for.php
$i++; You don't need this line inside a for loop, it's withing the for loop declaration that you should put it.
for ($i=0; $i < count($endBioTxt);$i++)
{
if (checkNum($i) == true)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
echo $i;
}
//$i++; You don't need this line inside a for loop otherwise $i will be incremented twice
}
Edit: Unrelated but this isn't how you check whether a number is prime or not
// Function to check if number is prime
function checkNum($num){
return ($num % 2) ? TRUE : FALSE;
}
This code works, please test it in your environment and then uncomment/comment what you need.
<?php
// This is how query should look like, not big fan of PHP but as far as I remember...
/*
$result = mysql_query("SELECT * FROM info WHERE id = 1");
$row = mysql_fetch_assoc($result);
$bioText = $row['bio'];
$endBioTxt = explode("\n", $bioText);
*/
$endBioTxt[0] = "one";
$endBioTxt[1] = "two";
$endBioTxt[2] = "three";
$endBioTxt[3] = "four";
$endBioTxt[4] = "five";
$totalElements = count($endBioTxt);
for ($i = 0; $i < $totalElements; $i++)
{
if ($i % 2)
{
echo "<li class='left'><div>".$endBioTxt[$i]."</div></li>";
}
else
{
echo "<li class='right'><div>".$endBioTxt[$i]."</div></li>";
}
/*
// This is how you should test if all your array elements are set
if (isset($endBioTxt[$i]) == false)
{
echo "Array has some values that are not set...";
}
*/
}
Edit 1
Try using $endBioTxt = preg_split('/$\R?^/m', $bioTxt); instead of explode.