Simple Rating system in PHP - php

I created this code to Show the Current Rating of a product using stars.
The number of stars to be displayed will be determined by the results we get when we divide the review points by 2.
For example:
Product Shoe has a review points of 4 that means 4/2 =2 (2 stars)
or lets say 6 points 6/2 = 3 (3 stars).
Since the maximum number of stars is 5.
If the results we get when we divide points is greater than 5 the stars will stay at 5.
Here is my example code, but it's not working correctly and it's confusing.
$star = "<li><a href='#'><i class='fa fa-star' aria-hidden='true'></i></a></li>";
$total_stars = $product_review / 2;
for($i=1; $i<=5; $i++) {
if($product_review >= $i) {
if($total_stars) {
echo $star;
}
}
}
Thanks in advance.

If you want to still use your code above, Here is the fixed code.
<?php
$star = "<li><a href='#'><i class='fa fa-star' aria-hidden='true'></i></a></li>";
$total_stars = (int)($product_review / 2);
for ($i=0; $i < $total_stars ; $i++) {
if ($i === 5) {
break;
}else{
echo $star;
}
}
?>

Related

Number counter game

Im trying to make a random number counter game that uses a for loop to print out 6 random numbers 1 - 6. I want to make it so the code can say how many times the number 6 shows in the loop.
At the moment I have the code it prints out for a loop of 6 random numbers but it only counts the numbers printed out.
For example
Welcome to the Dice Game!
How many sixes will you roll?
4 2 4 6 4 6
You rolled 2 six(es)!
<?php
echo"<h1>Welcome to the guess game thing guess how many 6s!</h1>";
$counter = 0;
for ($i=0; $i <=6;$i++) {
$randomNum = rand(1,6);
if ($randomNum <= 6) {
echo "<br> $randomNum";
$counter++;
}
else
{
echo"$randomNum <br>";
}
}
echo"<br>You rolled $counter sixes";
Some minor changes but you were almost there. Being consistent with your line breaks and verifying you check specifically for 6
$numberToMatch = 6;
for ($i = 0; $i <= 6; $i++) {
$randomNum = rand(1,6);
if ($randomNum == $numberToMatch) {
$counter++;
}
echo "$randomNum <br>";
}
You can do it like this:
$num = $_POST["num"];
for ($i=0; $i <=100;$i++) {
$randomNum = rand(1,10);
if ($randomNum == $num) {
echo $randomNum;
break;
}
echo $randomNum;
}
echo"<h2> there are $i</h2>";

How to build a pagination with ellipsis

so, I read every related questions here but they were not what Im asking here so please if you feel this question is a duplicate consider some one new to PHP asking this who can't find any good answer and I'm also asking please do not down vote this maybe some one wants to help me. Thanks
btw this is my code for pagination:
<?php
$per_page = 10;
if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
if($page == "" || $page == 1) {
$page_1 = 0;
} else {
$page_1 = ($page * $per_page) - $per_page;
}
$item_count = "SELECT * FROM products";
$find_count = mysqli_query($connection, $item_count);
$count = mysqli_num_rows($find_count);
$count_pages = ceil($count / $per_page) ;
?>
<ul class="pagination">
<?php
for($i = 1; $i <= $count_pages; $i++){
if($i == $page) {
echo "<li><a class='active-page' href='./latest.php?page=$i'>$i</a></li>";
} else {
echo "<li><a href='./latest.php?page=$i'>$i</a></li>";
}
}
?>
</ul>
the code get the page number with a Get request and do a loop for number of pages. and pagination out put is like this :
1 2 3 4 5 6 7 8 9 10
but I dont want to display like above, I want to make this shorter with "..." , like this if the user is on page 3:
1 2 3 4 5 ... 9 10
or user is on page 9 :
1 2 ... 7 8 9 10
how can I manipulate this code to achieve this?
Hey there The Big Ash!
You're almost there already. You just need to check a couple of things:
is $i within two pages of the beginning or the end of the page count. That's easy, right?
if ($i <= 2 || $i >= $count_pages - 2)
is $i within two pages of the current page?
This is achieved with
if (abs($i - $page) <= 2)
So now the question remains: when to put the ellipsis?
If you just echo '...' every time the above conditions are not met, you'll just end up with a whole bunch of ellipses, right?
Also, it's possible that you'll need two ellipses (imagine there are 20 pages and you're on page 10. You'd want '1 2 ... 8 9 10 11 12 ... 19 20).
I'm sure there's a more elegant way to this, but I'd just use a flag ($outOfRange) which is set to false when any of the above conditions are met, but set to true when they are not. Then we echo '...' only when the conditions are not met but $outOfRange is still false. So we have:
$outOfRange = false;
for($i = 1; $i <= $count_pages; $i++) {
if ($i <= 2 || $i >= $count_pages - 2 || abs($i - $page) <= 2) {
// page number should be echoed so do as you did before
$outOfRange = false;
if($i == $page) {
echo "<li><a class='active-page' href='./latest.php?page=$i'>$i</a></li>";
} else {
echo "<li><a href='./latest.php?page=$i'>$i</a></li>";
}
} else {
// we are out of range! if not already out of range, echo ellipsis
if (!$outOfRange) {
echo ' ... ';
}
$outOfRange = true;
}
}

Style of pagination php

I have an pagination code:
//pagination
$result1=$mysqli->query("SELECT * FROM product");
$row_per_page=10 ;
$rows=$result1->num_rows;
if ($rows>$row_per_page) $page=ceil($rows/$row_per_page);
else $page=1;
if(isset($_GET['start']) && (int)$_GET['start'])
$start=$_GET['start'];
else
$start=0;
$result=$mysqli->query("SELECT * FROM product limit $start,$row_per_page");
//End pagination
while ($rows = $result->fetch_assoc()) {
echo $rows['name'];
}
$page_cr=($start/$row_per_page)+1;
for($i=1;$i<=$page;$i++)
{
if ($page_cr!=$i) echo "<div class='pagination'>"."<a href='index.php?go=product&start=".$row_per_page*($i-1)."'>$i </a>"."</div>";
else echo "<div class='pagination'>".$i." "."</div>";
}
This code create pagination like this
Yeah, i want to remove 6 7 8 9 ect... and replace with "...". When i click to page 6, it will remove 1 2 3 4 5 - 10 11 12 13 etc..,it only show 6 7 8 9 as image below
[![Pagination][2]][2]
I hope you can understand my ideal, i try the best to show you.
Thank you so much
Here's a solution to your pagination problem, the kind of solution Google uses to paginate it's search results. Basically, the idea behind this solution is this:
At any point of time, at most 5 pagination links will be displayed i.e. from the current page's perspective, display two predecessor and successor pages. Let me explain this thing using two scenarios,
Case(1): When the number of pages is equal to or less than 5 (Lets say we have 4 pages)
In this case, we have to display all pagination links without any preceding or succeeding dots, like this:
// User is on page 1
1 2 3 4
- - -
// User is on page 3
1 2 3 4
- - -
// User is on page 4
1 2 3 4
- - -
Case(2): When the number of pages is greater than 5 (Lets say we have 10 pages)
In this case, we have to display both the pagination links and dots accordingly, like this:
// User is on page 1
1 2 3 4 5 ...
- - - -
// User is on page 5
... 3 4 5 6 7 ...
- - - -
// User is on page 10
... 6 7 8 9 10
- - - -
So after getting the current page number using $page_cr=($start/$row_per_page)+1;, the algorithm of this custom pagination system would be like this:
Check whether the number of pages is greater than 5 or not. If it's greater than 5, go to step 2 otherwise go to step 5.
Find the superset range of pages, like 1-10, or 1-20 etc. For example, if $page = 10 then this superset range would be 1-10. The code for this step is this:
// Superset range of pages
$superset_range = range(1, $page);
Find the subset range of pages to display, like 1-5, or 3-7 etc. For example, if $page = 10 then this subset range would be 1-5, or 3-7, or 6-10 etc., it can be any consecutive five page between 1 and 10. Also, adjust this range whenever necessary. The code for this step is this:
// Subset range of pages to display
$subset_range = range($page_cr - 2, $page_cr + 2);
// Adjust the range
foreach($subset_range as $p){
if($p <= 0){
array_shift($subset_range);
$subset_range[] = $subset_range[count($subset_range) - 1] + 1;
}elseif($p > $page){
array_pop($subset_range);
array_unshift($subset_range, $subset_range[0] - 1);
}
}
Display the pagination links and dots accordingly. The code for this step is this:
// Display pagination links and dots
if($subset_range[0] > $superset_range[0]){
echo "<div class='pagination'>... </div>";
}
foreach($subset_range as $p){
if($page_cr != $p){
echo "<div class='pagination'>"."<a href='index.php?go=product&start=".$row_per_page*($p-1)."'>$p </a>"."</div>";
}else{
echo "<div class='pagination'>".$p." "."</div>";
}
}
if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
echo "<div class='pagination'> ...</div>";
}
Display all pagination links using your old code, like this:
// Display all page links
for($i = 1; $i <= $page; $i++){
if($page_cr != $i){
echo "<div class='pagination'>"."<a href='index.php?go=product&start=".$row_per_page*($i-1)."'>$i </a>"."</div>";
}else{
echo "<div class='pagination'>".$i." "."</div>";
}
}
So here's the complete code:
$result1 = $mysqli->query("SELECT * FROM product");
$row_per_page= 10;
$rows=$result1->num_rows;
if ($rows > $row_per_page){
$page=ceil($rows/$row_per_page);
}else{
$page=1;
}
if(isset($_GET['start']) && (int)$_GET['start']){
$start=$_GET['start'];
}else{
$start=0;
}
$result=$mysqli->query("SELECT * FROM product limit $start,$row_per_page");
while ($rows = $result->fetch_assoc()) {
echo $rows['name'];
}
$page_cr=($start / $row_per_page) + 1; // Page number
if($page > 5){
// From the current page's perspective, display two predecessor and successor pages
// Superset range of pages
$superset_range = range(1, $page);
// Subset range of pages to display
$subset_range = range($page_cr - 2, $page_cr + 2);
// Adjust the range
foreach($subset_range as $p){
if($p <= 0){
array_shift($subset_range);
$subset_range[] = $subset_range[count($subset_range) - 1] + 1;
}elseif($p > $page){
array_pop($subset_range);
array_unshift($subset_range, $subset_range[0] - 1);
}
}
// Display pagination links and dots
if($subset_range[0] > $superset_range[0]){
echo "<div class='pagination'>... </div>";
}
foreach($subset_range as $p){
if($page_cr != $p){
echo "<div class='pagination'>"."<a href='index.php?go=product&start=".$row_per_page*($p-1)."'>$p </a>"."</div>";
}else{
echo "<div class='pagination'>".$p." "."</div>";
}
}
if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
echo "<div class='pagination'> ...</div>";
}
}else{
// Display all page links
for($i = 1; $i <= $page; $i++){
if($page_cr != $i){
echo "<div class='pagination'>"."<a href='index.php?go=product&start=".$row_per_page*($i-1)."'>$i </a>"."</div>";
}else{
echo "<div class='pagination'>".$i." "."</div>";
}
}
}

Divide an amout equally beteen no of user

I want to distribute an amount among some users equally.If amount is not divisible equally then all other member will get equal amount expect the last member who will get rest of the money.Below is what i have tried
$number = $_POST['number'];
$noOfTime = $_POST['no_of_time'];
$perHead = ceil($number / $noOfTime);
for ($i = 1; $i <= $noOfTime; $i++) {
if ($i == $noOfTime) {
echo $perHead * $noOfTime - $number;
} else {
echo $perHead;
}
}
Here if number is 7 and member are 4 the first 3 member will the 2 and the last will get 1. Like 2,2,2,1.
But this logic seems to be not working for all cases.
Please help.Thanks.
I think it should help you.
$no = 22;
$users = 8;
// count from 0 to $users number
for ($i=0;$i<$users;$i++)
// if the counting reaches the last user AND $no/$users rests other than 0...
if ($i == $users-1 && $no % $users !== 0) {
// do the math rounding fractions down with floor and add the rest!
echo floor($no / $users) + ($no % $users);
} else {
// else, just do the math and round it down.
echo floor($no / $users)." ";
}
OUTPUTS:
2 2 2 2 2 2 2 8
EDIT: I nested an if verification so the logic won't fail even if users are 1 or 2. And since it received more upvotes, I commented the code to make it more clear.

set every 1st and fourth element with a class

I have a jquery carousel set to 620px wide. I am using the grid 960 to place the images inside. I show 4 images at a time.
In order to do that I set the every first image class to 'grid_2 alpha' and every fourth to 'grid_2 omega' in the group of 4 shown while all in between I set to just grid_2.
This gives me the 620px I need. I am pulling from the database and I am trying to set the class dynamically but cant quite get every first and fourth in the group with the classes.
<?php $loopIndex = 1; ?>
<?php foreach ($pub_values as $v) {
if($v['pub_of_the_month'] == 1)
{
?>
<?php if ($loopIndex == 1 || $grid_class=="grid_2 omega") $grid_class="grid_2 alpha";
else if($loopIndex%4 == 0) $grid_class="grid_2 omega";
else $grid_class="grid_2";
$filename = "images/pub_images/120x160/".$v['id'].".jpg";
if (!file_exists($filename)) $filename = "images/pub_images/120x160/blank.gif";
?>
<div class="<?php echo $grid_class?>">
<a href="#">
<img src="<?=$filename;?>" alt="<?=$v['name'];?>" width="120" height="160" />
<?=$v['name'];?>
</a>
</div>
<?php $loopIndex = $loopIndex + 1; } }?>
The above code is my best attempt of achieving the following.
Images
1 - grid_2 alpha
2 - grid_2
3 - grid_2
4 - grid_2 omega
5 - grid_2 alpha
6 - grid_2
7 - grid_2
8 - grid_2 omega
9 - grid_2 alpha
Simple math, dividing with a remainder.
Maybe you can figure out by looking a this code:
for($i = 1; $i <= 9; $i++){
echo "\n $i ";
if($i % 4 == 1){
echo " alpha";
}elseif($i % 4 == 0){
echo " omega";
}
}
Live example: http://codepad.org/LIepxlJm
maybe you should just use a for-loop:
for ($i = 0; $i< sizeof($pub_values); $i++) {
$classes =['grid_2'];
if($i%4 == 0) $classes[] = 'alpha';
if($i%4 == 3) $classes[] = 'omega';
}
edit: this is just trying to show an approach, not trying to be syntactically correct.
in the end, you want to join (or implode in php) your classes to get the string.... (not implemented for you either)

Categories