PHP user rating based on post and read count - php

Im trying to get user stars based on their post and read count.
0 post, 0 reads – 0 stars
3 post, 3,000 reads – 1 star
15 post, 15,000 reads – 2 star
25 post, 30,000 reads – 3 star
35 post, 40,000 reads – 4 star
50 post, 70,000 reads – 5 star
code:
$read = 15000;
$news = 13;
if($read < 3000 || $news < 3){
echo '0';
}
else if(($read >= 3000 & $news >= 3)
&& ($read < 15000 & $news < 15)){
echo '1';
}
else if(($read >= 15000 & $news >= 15)
&& ($read < 30000 & $news < 25)){
echo '2';
}
else if(($read >= 30000 & $news >= 25)
&& ($read < 40000 & $news < 35)){
echo '3';
}
else if(($read >= 40000 & $news >= 35)
&& ($read < 70000 & $news < 50)){
echo '4';
}
else if($read >= 70000 & $news >= 50){
echo '5';
}
this does not work well..

check this,
<?php
$read = 50;
$post = 44;
if($post >= 50 && $read >= 70000){
echo "5 stars";
}else if($post >= 35 && $read >= 40000){
echo "4 stars";
}else if($post >= 25 && $read >= 30000){
echo "3 stars";
}else if($post >= 15 && $read >= 15000){
echo "2 stars";
}else if($post >= 3 && $read >= 3000){
echo "1 stars";
}else{
echo "0 stars";
}
?>

Your code "doesn't work" now because it does not match any of your if else statements. If you change your code to this, it should work with the given arguments:
$read = 15000;
$news = 13;
if($read < 3000 || $news < 3){
echo '0';
}
else if(($read >= 3000 & $news >= 3)
&& ($read <= 15000 & $news <= 15)){
echo '1';
}
else if(($read >= 15000 & $news >= 15)
&& ($read <= 30000 & $news <= 25)){
echo '2';
}
else if(($read >= 30000 & $news >= 25)
&& ($read <= 40000 & $news <= 35)){
echo '3';
}
else if(($read >= 40000 & $news >= 35)
&& ($read <= 70000 & $news <= 50)){
echo '4';
}
else if($read >= 70000 & $news >= 50){
echo '5';
}
I reproduced it in an angular fiddle: http://jsfiddle.net/Lvc0u55v/10941/
Your second statements should also check for <=

Related

Display students grades in alphabetical order

I've been able to display the number of times each grade appears on a student's report sheet. However, they are not sorted alphabetically.
grade image
The image above shows that the student got 2Bs, 4B+, 3As, and 1E. My code displays it like this.
But I want the grades to be displayed in alphabetical order like this 3As, 2Bs, 4B+, and 1E.
How do I do that?
Here is my code
<?php $i = 1;
$total = 0;
$count = count($subjectScores);
$grades_count = [];
foreach ($subjectScores as $value) { ?>
<?php
if ($value->tot_score >= 90 && $value->tot_score <= 100) {
$grade = 'A+';
$remark = 'DISTINCTION';
} elseif ($value->tot_score >= 80 && $value->tot_score <= 89.99) {
$grade = 'A';
$remark = 'EXCELLENT';
} elseif ($value->tot_score >= 70 && $value->tot_score <= 79.99) {
$grade = 'B+';
$remark = 'VERY GOOD';
} elseif ($value->tot_score >= 60 && $value->tot_score <= 69.99) {
$grade = 'B';
$remark = 'GOOD';
} elseif ($value->tot_score >= 50 && $value->tot_score <= 59.99) {
$grade = 'C';
$remark = 'ABOVE AVERAGE';
} elseif ($value->tot_score >= 45 && $value->tot_score <= 49.99) {
$grade = 'D';
$remark = 'AVERAGE';
} elseif ($value->tot_score >= 40 && $value->tot_score <= 44.99) {
$grade = 'E';
$remark = 'FAIR';
} elseif ($value->tot_score >= 0 && $value->tot_score <= 39.99) {
$grade = 'F';
$remark = 'NEEDS SERIOUS IMPROVEMENT';
}
// declare count for grade initially with 0
if(isset($grades_count[$grade]) === false) {
$grades_count[$grade] = 0;
}
{
// increment count for given grade
$grades_count[$grade]++;
}
?>
<?php foreach ($grades_count as $grade=>$count) {
echo "$count$grade ";
} ?>

MySQL/PHP - page number pagination Only show 10 pages at times

I am having difficulties with my MYSQL / PHP dashboard. - Currently i am having 50 pages, but currently they are all showing on the same page.
http://imgur.com/wDfTWUa - as you can see in the attached file. - I only want 10 pages to be shown, and be able to click through the rest of the pages without seeing 4 rows of pages.
Exsampel <- 2 3 4 5 6 7 8 9 10 -> when you are on ?page=1, if you are on page ?page=10 <- 11 12 13 14 15 16 17 18 19->
Hope you can help me.
Code:
<?php
include 'config.php';
$sidenr = $_GET['page'];
$sidenr2 = ($sidenr -1) * 10;
echo $sidenr2;
echo "<br><br>";
$query100 = mysqli_query($conn, "SELECT * FROM `test` LIMIT $sidenr2,10") or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query100))
{
echo $row['id']."<br>";
}
$result = mysqli_query($conn, "SELECT * FROM test");
$num_rows = mysqli_num_rows($result);
$sideantal = $num_rows / 10;
echo "Der skal være antal sider: ". $sideantal;
echo "<br><br>antal rækker ". $num_rows . "<br><br>";
?>
<br><br>
<?php
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
?>
function getPageRange($current, $max, $total_pages = 10) {
$desired_pages = $max < $total_pages ? $max : $total_pages;
$middle = ceil($desired_pages/2);
if ($current <= $middle){
return [1, $desired_pages];
}
if ($current > $middle && $current <= ($max - $middle)) {
return [
$current - $middle,
$current + $middle
];
}
if ($current <= $max ) {
return [
$current - ($desired_pages - 1),
$max
];
}
}
list($min,$max) = getPageRange($sidenr, $sideantal);
foreach (range($min, $max) as $number) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
try to change this:
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
to this:
for ($number = 1; $number <= $sideantal; $number++) {
if (($number > $_GET['page']) && ($number <= $_GET['page'] + 10)) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
}
You can try the following code:
for ($number = 1; $number <= $sideantal; $number++) {
/** If the loop count is greater than the current page but less than current page plus 10 */
if ( ($number > $_GET['page'] && ($number < ($_GET['page'] + 10)))) $is_valid = true;
/** If the loop count is less than the current page but greater than current page -10 and the current page is the last page */
if ($number < $_GET['page'] && $_GET['page'] == $sideantal && $number > ($_GET['page'] - 10)) $is_valid = true;
else $is_valid = false;
if ($is_valid) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
}

PHP - Number of units between two numbers

I'm trying to find the number of units between 2 numbers that are under zero between 0 and a limit and over that limit. Here is my function. It works fine until I have to work with some huge numbers which takes a lot of time to process. I am trying to find a way to execute this code without using a loop.
public function getBetween($num1, $num2) {
$limit = 500000;
$array = array(0,0,0);
if ($num1 >= $num2) {
$low = $num2;
$high = $num1;
} else {
$low = $num1;
$high = $num2;
}
for($i=$low; $i < $high; $i++) {
if ($i < 0) {
$array[0]++;
} elseif ($i >= 0 && $i < $limit) {
$array[1]++;
} else {
$array[2]++;
}
}
return $array;
}
I have started to split my loop into elseif statements but this is getting messy really quick and I will also have to eventually be able to set more than one limit which will become impossible to use.
if ($low < 0 && $high < 0) {
} elseif ($low < 0 && $high >= 0 && $high < $limit) {
} elseif ($low < 0 && $high >= $limit) {
} elseif ($low >= 0 && $low < $limit && $high < 0) {
} elseif ($low >= 0 && $low < $limit && $high >= 0 && $high < $limit) {
} elseif ($low >= 0 && $low < $limit && $high >= $limit) {
} elseif ($low >= $limit && $high < 0) {
} elseif ($low >= $limit && $high >= 0 && $high < $limit) {
} elseif ($low >= $limit && $high >= $limit) {
}
I am trying to find a clean way to do it. Any ideas?
EDIT
Here is an example of the array I'm trying to get.
If my limit was 500, $num1 = -100 and $num2 = 700 i would get the array
$array[0] = 100
$array[1] = 500
$array[2] = 200
I didn't test it (didn't run a PHP script but I tried it "manually" with a few examples).
You still have loops, but only one iteration per limit (instead of one per unit).
// Example datas
$limits = array(0, 500, 800);
$low = -100;
$high = 1000;
$splittedResults = array();
// Get total of units
$totalUnits = abs($high - $low);
$totalCounted = 0;
foreach($limits as $limit) {
if ($low > $limit) {
// Nothing under the limit
$nbUnderLimit = 0;
} elseif($high < $limit) {
// Both values under the limit
$nbUnderLimit = $totalUnits;
} else {
// $low under the limit and $high over it
$nbUnderLimit = abs($limit - $low);
}
// Here we know how much units are under current limit in total.
// We want to know how much are between previous limit and current limit.
// Assuming that limits are sorted ascending, we have to remove already counted units.
$nbBetweenLimits = $nbUnderLimit - $totalCounted;
$splittedResults[] = $nbBetweenLimits;
$totalCounted += $nbBetweenLimits;
}
// Finally, number of units that are over the last limit (the rest)
$splittedResults[] = $totalUnits - $totalCounted;
You could create an array of the numbers with range() and use array_filter
$count = sizeof(array_filter (range(0,800), function($value){ return ($value > 500); }));
And one for < as well etc.
You only need to define range array once, separately.

Why does this return 1 number and then stops the loop? PHP

I'm a beginner at PHP, so my code might not be efficient or good.
Why does this code return 1 number and then stops the loop? It's supposed to stop the loop when "the dice" rolled two of every number (1,2,3,4,5,6). But now it stops after randomly generating 1 number..
<?php
$sixCount = 0;
$fiveCount = 0;
$fourCount = 0;
$threeCount = 0;
$twoCount = 0;
$oneCount = 0;
$rollCount = 0;
do{
$roll = rand(1,6);
$rollCount++;
if($roll == 6){
$sixCount++;
echo "6";
} else if($roll == 5){
$fiveCount++;
echo "5";
} else if($roll == 4){
$fourCount++;
echo "4";
} else if($roll == 3){
$threeCount++;
echo "3";
} else if($roll == 2){
$twoCount++;
echo "2";
} else {
$oneCount++;
echo "1";
}
} while($sixCount < 3 && $sixCount > 1 && $fiveCount < 3 && $fiveCount > 1 && $fourCount < 3 && $fourCount > 1 && $threeCount < 3 && $threeCount > 1 && $twoCount < 3 && $twoCount > 1 && $oneCount < 3 && $oneCount > 1);
echo "<br />It took {$rollCount} rolls!";
?>
This is an exercise from Codecademy.com!
Thanks,
Jesper (New at Stackoverflow!)
After first execution of loop, you cannot have $sixCount > 1 && $fiveCount > 1, among other conditions.
After first roll, suppose it's 3, your variables are:
$sixCount = 0;
$fiveCount = 0;
$fourCount = 0;
$threeCount = 1;
$twoCount = 0;
$oneCount = 0;
It doesn't suit while conditions, cuz, for example, $sixCount > 1 is false and other vars too.
The while expression says:
while ($sixCount < 3 && $sixCount > 1 && $fiveCount < 3 && $fiveCount > 1 ...
If $sixCount is less than 3 and more than 1 that implies $sixCount equals 2. Ditto for the others. So it means "keep looping while $sixCount equals 2 and $fiveCount equals 2 and [all the others equal 2]".
You start with those variables at 0:
$sixCount = 0;
$fiveCount = 0;
...
So the loop condition is not initially met. The loop allows at most one of them to be incremented at most once:
$roll = rand(1, 6);
if ($roll == 6) {
$sixCount++;
echo "6";
} else if ($roll == 5) {
$fiveCount++;
echo "5";
} ...
No matter what number is rolled it is impossible to get any of the counts to 2 by the end of a single roll, and certainly not all of them, so the loop condition will not be met, and the loop will inevitably stop.
It's supposed to stop the loop when "the dice" rolled two of every number (1,2,3,4,5,6)
In that case, the correct condition would be:
while ($sixCount < 2 && $fiveCount < 2 && ...
As others have said, your conditional for the while loop will never be true. Instead, you want to make sure the variables aren't all at 2. Try this instead:
while ($sixCount < 2 || $fiveCount < 2 || $fourCount < 2 || $threeCount < 2 || $twoCount < 2 || $oneCount < 2)
I just adapted your script. It can be a funny game.
It rolls 6 dice times 2 (6 x 2) and then if requirements are not met, it rolls the dice again :
$rollCount = 0;
do{
$sixCount = 0;
$fiveCount = 0;
$fourCount = 0;
$threeCount = 0;
$twoCount = 0;
$oneCount = 0;
$rollCount++;
for ($i= 0; $i< 2 * 6; $i++) {
$roll = rand(1,6);
if($roll == 6){
$sixCount++;
echo "6";
} else if($roll == 5){
$fiveCount++;
echo "5";
} else if($roll == 4){
$fourCount++;
echo "4";
} else if($roll == 3){
$threeCount++;
echo "3";
} else if($roll == 2){
$twoCount++;
echo "2";
} else {
$oneCount++;
echo "1";
}
}
echo "_";
} while(!($sixCount < 3 && $sixCount > 1 && $fiveCount < 3 && $fiveCount > 1 && $fourCount < 3 && $fourCount > 1 && $threeCount < 3 && $threeCount > 1 && $twoCount < 3 && $twoCount > 1 && $oneCount < 3 && $oneCount > 1));
echo "<br />It took {$rollCount} rolls!";
$a = array(
$sixCount,
$fiveCount,
$fourCount,
$threeCount,
$twoCount,
$oneCount);
echo '<pre>';
print_r($a);
echo '</pre>';
Giving this king of output :
262225451666_535451252543_666153663214_652652635413_522615315213_412123422526_113553235335_255616351453_124215216465_112544353243_161145351612_522462262355_114331531645_563664155335_455623424146_233336226515_213136514365_646344361534_445325236533_423153546564_324466143565_422464136444_631511342612_516266141216_613556242333_351541131651_554665566244_261433652145_
It took 28 rolls!
Array
(
[0] => 2
[1] => 2
[2] => 2
[3] => 2
[4] => 2
[5] => 2
)

PHP - if-statement not working properly

I am currently in a project to make an auction website. To make sure people can't bid too little above the current bid, I have made this if-statement:
if(
($this->getHighestBid() < 50 && $this->getHighestBid() >= 0 && $bid > $this->getHighestBid()+0.50) ||
($this->getHighestBid() < 500 && $this->getHighestBid() >= 50 && $bid > $this->getHighestBid()+1) ||
($this->getHighestBid() < 1000 && $this->getHighestBid() >= 500 && $bid > $this->getHighestBid()+5) ||
($this->getHighestBid() < 5000 && $this->getHighestBid() >= 1000 && $bid > $this->getHighestBid()+10) ||
($this->getHighestBid() >= 5000 && $bid > $this->getHighestBid()+50)
){
...
}
This should make it so that when the current highest bid is in a specified range, you have to raise the price by at least the price specified.
My problem here is that it won't let me bid on anything at all. Does anyone know what's going on here?
Your code appears to work in my testing, it sounds like the type of the variable is not a numeric type(float or integer) and is actually a string or similarly strange type.
To confirm:
var_dump($this->getHighestBid());
I would like to add while I'm writing my answer though that you will probably want to edit your code to read more like:
function getMinimumBid() {
$maxBid = (float)$this->getHighestBid();
return $maxBid + $this->getBidIncrement();
}
function getBidIncrement() {
$maxBid = (float)$this->getHighestBid();
switch (true) {
case $maxBid < 50:
return 0.50;
case $maxBid < 500:
return 1;
case $maxBid < 1000:
return 5;
case $maxBid < 5000:
return 10;
default:
return 50;
}
}
// ...
if ($bid >= $this->getMinimumBid()) {
}
// ...
Here's your code:
$highest_bid = $this->getHighestBid();
if(
($highest_bid < 50 && $highest_bid >= 1 && $bid > ($highest_bid+0.50)) ||
($highest_bid < 500 && $highest_bid >= 50 && $bid > ($highest_bid+1)) ||
($highest_bid < 1000 && $highest_bid >= 500 && $bid > ($highest_bid+5)) ||
($highest_bid < 5000 && $highest_bid >= 1000 && $bid > ($highest_bid+10)) ||
($highest_bid >= 5000 && $bid > ($highest_bid+50))
){
// Condition 1:
// Highest bid should be >= 1 and < 50
// Bid should be > 1.5 and < 50.5
// Condition 2:
// Highest bid should be >= 50 and < 500
// Bid should be > 51 and < 501
// Condition 3:
// Highest bid should be >= 500 and < 1000
// Bid should be > 505 and < 1005
// Condition 4:
// Highest bid should be >= 1000 and < 5000
// Bid should be > 1010 and < 5010
// Condition 5:
// Highest bid should be >= 5000
// Bid should be > 5050
}
There's a major problem here:
there's some numbers which aren't handled.
you should consider doing this business logic in a function.
I changed your code a little bit, so I could execute it in my IDE:
<?php
$hbid = 50;
$bid = 52;
if(
($hbid < 50 && $hbid >= 0 && $bid > $hbid+0.50) ||
($hbid < 500 && $hbid >= 50 && $bid > $hbid+1) ||
($hbid < 1000 && $hbid >= 500 && $bid > $hbid+5) ||
($hbid < 5000 && $hbid >= 1000 && $bid > $hbid+10) ||
($hbid >= 5000 && $bid > $hbid+50)
)
{
echo "ok";
}
else
{
echo 'not ok';
}
?>
When I change the parameters, it always works fine. I guess your problem is somewhere else.

Categories