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 ";
} ?>
I have placed results of two functions in two different columns of a table. In the third column I want to multiply the two results. Multiplication shows zero. First result is 6 and second is 3 - addition shows 63 (like string addition).
<tr>
<td>Economics</td><td><?php echo $e; ?></td><!--Economics marks-->
<td><?php $gra = sg($e); ?></td><!--Economics grade using function sg()-->
<td><?php $grap = sgp($e); ?></td><!--Economics grade points using sgp() function.-->
<td><?php echo $ec; ?></td><!--Economics credits-->
<td><?php $ce = ce3($e); ?></td><!--Economics credits earned using function ce3()-->
<td><?php echo $ce * $grap; ?></td><!--multiplication of two function results - this should be 18 - 3 x 6 but shows 3.-->
</tr>
Function to calculate earned credits:-
function ce3($marks) {
if ($marks == "A" || $marks == "CC") {
$marks = "---";
echo $marks;
} else {
$marks = 3;
echo $marks;
}
}
Function to calculate grade points:-
function sgp ($marks) {
if ($marks == "A" || $marks =="CC") {
$marks = "---";
echo $marks;
} elseif ($marks < 40) {
$marks = 0;
echo $marks;
} elseif ($marks >= 40 && $marks < 45) {
$marks = 4;
echo $marks;
} elseif ($marks >= 45 && $marks < 50) {
$marks = 5;
echo $marks;
} elseif ($marks >= 50 && $marks < 55) {
$marks = 6;
echo $marks;
} elseif ($marks >= 55 && $marks < 60) {
$marks = 7;
echo $marks;
} elseif ($marks >= 60 && $marks < 70) {
$marks = 8;
echo $marks;
} elseif ($marks >= 70 && $marks < 80) {
$marks = 9;
echo $marks;
} elseif ($marks >= 80) {
$marks = 10;
echo $marks;
}
}
you're missing return statements in your function...
function ce3($marks) {
if ($marks == "A" || $marks == "CC") {
$marks = "---";
echo $marks;
}
else {$marks = 3;
echo $marks;
}
return $marks;
}
your sgp() function also.
<?php
$hp = 0;
while($hp < 50) {
$flip = rand(0,2);
if ($flip == 1) {
echo "<p>X-Ray</p>";
$hp += 15;
} elseif ($flip == 2) {
echo "<p>Special Move</p>";
$hp += 10;
} else {
echo "<p>Punch</p>";
$hp += 5;
}
echo "<p>Total so far: $hp</p>";
echo "</br>";
}
?>
This is a PHP code. When I run it, it works fine. However, when I change it to this code below it doesn't.
<?php
$hp = 50;
while($hp > 1) {
$flip = rand(0,2);
if ($flip == 1) {
echo "<p>X-Ray</p>";
$hp -= 15;
} elseif ($flip == 2) {
echo "<p>Special Move</p>";
$hp -= 10;
} else {
echo "<p>Punch</p>";
$hp -= 5;
}
echo "<p>Total so far: $hp</p>";
echo "</br>";
}
?>
Please help. tHE CHANGES I MADE ARE THE HIGHLIGHTED ONES.
You never created $hp properly in the second version:
50;
doesn't do anything. It just tells php "here, have a 50", and php goes "gee, thanks, ok, whatever" and moves onwards. Then you have
while($hp > 1) {
Since $hp is undefined, it's null, and the code parses/executes as:
while($hp > 1) {
while(null > 1) {
while(0 > 1) {
FALSE -> exit loop
You never created $hp properly in the second version:
50;
If you do change it to $hp = 50;
Right so i have a promotion button setup. Every promotion someone receives it gives them earning points to their profile, But every rank has different points depending on how high it is or low it is. I'm having a hard time trying to figure out how to get the points to scale with their ranks. My database column for it is known as: rank and the ranks are known as numbers in the database in which when it shows externally they all have a name from a function. So ie; Rank 1, 2, 3, 4 would equal say Noob, Member, Elite, God. Anyway if you can help me find out where my error is that would be great, kinda new to php. The issue I'm having is that the points are not being given to the person. They get promoted fine but no points.
promote.php
<?php include_once('../classes/check.class.php'); ?>
<?php include('../config/db.php'); ?>
<?php include('functionsprom.php'); ?>
<?php if( protectThis("1, 2, 3") ) : ?>
<?php
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
$date = date('d M y');
$prom = promote;
$result = mysql_query("UPDATE players SET date='$date', rank = rank + 1, exp = exp + '$prom' WHERE id=$id", $db1)
or die(mysql_error());
require_once("../config/logdb.php");
include('../logs.php');
write_mysql_log($promote, $db);
header("Location: ../home.php");
}
else
{
header("Location: home.php");
}
?>
<?php endif; ?>
functionsprom.php
<?php
include('functionsrank.php');
mysql_select_db('pts_player');
$rank = "SELECT rank FROM players WHERE id = $id";
function promote($rank) {
if($rank == 1):
$prom = 0;
elseif($rank == 2):
$prom = 0;
elseif($rank == 3):
$prom = 0;
elseif($rank == 4 ):
$prom = 10;
elseif($rank == 5):
$prom = 20;
elseif($rank == 6):
$prom = 30;
elseif($rank == 7):
$prom = 45;
elseif($rank == 8):
$prom = 45;
elseif($rank == 9):
$prom = 55;
elseif($rank == 10):
$prom = 60;
elseif($rank == 11):
$prom = 75;
elseif($rank == 12):
$prom = 75;
elseif($rank == 13):
$prom = 0;
elseif($rank == 14):
$prom = 0;
elseif($rank == 15):
$prom = 100;
elseif($rank == 16):
$prom = 200;
elseif($rank == 17):
$prom = 400;
elseif($rank == 18):
$prom = 600;
elseif($rank == 19):
$prom = 800;
elseif($rank == 20):
$prom = 900;
elseif($rank == 21):
$prom = 1000;
elseif($rank == 22):
$prom = 1200;
elseif($rank == 23):
$prom = 1400;
elseif($rank == 24):
$prom = 0;
endif;
return $rank;
}
?>
So I'm looking how to make a checkboard but with a spiral in it instead of the default checkerboard made like this:
$checkerboard=array();
for($row=0;$row<10;$row++){
if($row%2==0){
for($col=0;$col<10;$col++){
if($col%2==0){
$checkerboard[$row][$col]="white";
}else{
$checkerboard[$row][$col]="black";
}
}
}else{
for($col=0;$col<10;$col++){
if($col%2==0){
$checkerboard[$row][$col]="black";
}else{
$checkerboard[$row][$col]="white";
}
}
}
}
I also tried it with 2 diagonals like this:
$diagonal=array();
for($row=0;$row<10;$row++){
for($col=0;$col<10;$col++){
if($row==$col){
$diagonal[$row][$col]='black';
}else{
$diagonal[$row][$col]='white';
}
if($row+$col==9){
$diagonal[$row][$col]='black';
}
}
}
And then echo'd simply like this:
echo "<table>";
for($row=0;$row<count($checkerboard);$row++){
echo "<tr>";
for($col=0;$col<count($checkerboard);$col++){
echo "<td width='50px' height='50px' bgcolor='".$checkerboard[$row][$col]."'></td>";
}
echo "</tr>";
}
I'd like to keep the code simple because I've not been coding php for a very long time and it has to work with an array.
I tried this here:
$spiral=array();
for($row=0;$row<10;$row++){
for($col=0;$col<10;$col++){
$spiral[$row][$col]='white';
if($row==0 or $row==9 or $col==0 or $col==9){
$spiral[$row][$col]='black';
}if($row==1 and $col==0){
$spiral[$row][$col]='white';
}if($row==2 and $col<8){
$spiral[$row][$col]='black';
}if($row>1 and $row<8 and $col==7){
$spiral[$row][$col]='black';
}if($row==7 and $col>1 and $col<8){
$spiral[$row][$col]='black';
}if($row>3 and $row<7 and $col==2){
$spiral[$row][$col]='black';
}if($row==4 and $col>2 and $col<6){
$spiral[$row][$col]='black';
}if($row==5 and $col==5){
$spiral[$row][$col]='black';
}
}
}
But if the checkerboard becomes bigger it will be very hard to change. It there a way to make it easier?
Try this out:
I create an empty board then start drawing horizontal & vertical lines, starting from the edges each time. The code might need some tweaking but it's a good start
$checkerboard=array();
$size = 12;
for ($row=0; $row<$size; $row++) {
for ($col=0; $col<$size; $col++) {
$checkerboard[$row][$col]="red";
}
}
//horizontal
$pair = 0 ;
while ($pair < (int) $size / 2) {
//drawing top half rows
$row = 2 * $pair;
$end = min($row, $size - $row);
$start = $end - 2;
for ($col = $start; ($col < $size - $end) && ($row < $size / 2); $col++){
$checkerboard[$row][$col]="black";
}
//drawing bottom half rows
$far_row = $size - 1 - 2 * $pair;
$end = min($far_row, $size - $far_row) + 1 - 2;
$start = $end ;
for ($col = $start; ($col < $size - $end) && ($far_row > $size / 2 ); $col++){
$checkerboard[$far_row][$col]="black";
}
$pair++;
}
$pair = 0;
//vertical
while ($pair < (int) $size / 2) {
//drawing left half columns
$col = 2 * $pair;
$end = min($col, $size - $col);
$start = $end +2 ;
for ($row = $start; ($row < $size - $end) && ($col < $size / 2); $row++){
$checkerboard[$row][$col]="black";
}
//drawing right half columns
$far_columns = $size - 1 - 2 * $pair;
$end = min($far_columns, $size - $far_columns) - 1;
$start = $end ;
for ($row = $start; ($row < $size - $end) && ($far_columns >= ($size / 2 ) ); $row++){
$checkerboard[$row][$far_columns]="black";
}
$pair++;
}
echo "<table>";
for($row=0;$row< $size;$row++){
echo "<tr>";
for($col=0; $col< $size; $col++){
echo "<td width='50px' height='50px' bgcolor='".$checkerboard[$row][$col]."'></td>";
}
echo "</tr>";
}
You can just change the $size variable for different dimensions
Here is my attempt at the problem:
function buildSpiral($gridSize)
{
/**
* Origin is at the top left handcorner
*/
$x = 0;
$y = 0;
$xMin = 0;
$xMax = $gridSize-1;
$yMin = 2;
$yMax = $gridSize-1;
$pattern = [];
$size = $gridSize;
$collision = function($p, $limit) {
return (bool) ($p == $limit);
};
// increment x
$shadeRight = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x++, $xMax));
if ($x >= $xMax) {
$x=$xMax;
}
$xMax-=2;
};
// increment y
$shadeDown = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision) {
while ($y < $yMax && $yMin > $yMax) {
$pattern[++$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($y++, $yMax));
if ($y >= $yMax) {
$y = $yMax;
}
$yMax-=2;
};
// decrement x
$shadeLeft = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision, $gridSize) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x--, $xMin));
if ($x < $xMin) {
$x=$xMin;
}
$xMin+=2;
};
// decrement y
$shadeUp = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision, $gridSize) {
while ($y > $yMin && $yMin > $yMax) {
$pattern[--$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while(!$collision(--$y, $yMin));
if ($y < $yMin) {
$y = $yMin;
}
$yMin+=2;
};
while ($size > 0) {
$shadeRight($x, $y);
$shadeDown($x, $y);
$shadeLeft($x, $y);
$shadeUp($x, $y);
$size-=2;
}
return $pattern;
}
for ($i = 1; $i <= 25; $i++) {
$checkboard = buildSpiral($i);
echo "<h1>$i</h1>";
echo "<table style='margin-bottom: 2em;'>";
for($row=0;$row<count($checkboard);$row++){
echo "<tr>";
for($col=0;$col<count($checkboard);$col++){
if (!isset($checkboard[$row][$col])) {
echo "<td width='50px' height='50px' bgcolor=\"red\"></td>";
} else {
echo "<td width='50px' height='50px' bgcolor=\"black\"></td>";
}
}
echo "</tr>";
}
echo "</table>";
}
I stop each shading direction when a limit is hit.
Update Let's say I want the spirals to start from the top right corner, then we just need to set the new origin and call the shaders in the way we want the spiral to go like so:
function buildSpiral($gridSize)
{
/**
* Origin is at the top left handcorner
*/
$x = $gridSize-1;
$y = 0;
$xMin = 0;
$xMax = $gridSize-1;
$yMin = 2;
$yMax = $gridSize-1;
$pattern = [];
$size = $gridSize;
$collision = function($p, $limit) {
return (bool) ($p == $limit);
};
// increment x
$shadeRight = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x++, $xMax));
if ($x >= $xMax) {
$x=$xMax;
}
$xMax-=2;
};
// increment y
$shadeDown = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision) {
while ($y < $yMax && $yMin > $yMax) {
$pattern[++$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($y++, $yMax));
if ($y >= $yMax) {
$y = $yMax;
}
$yMax-=2;
};
// decrement x
$shadeLeft = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision, $gridSize) {
if ($xMin > $xMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while (!$collision($x--, $xMin));
if ($x < $xMin) {
$x=$xMin;
}
$xMin+=2;
};
// decrement y
$shadeUp = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision, $gridSize) {
while ($y > $yMin && $yMin > $yMax) {
$pattern[--$y][$x] = 1;
}
if ($yMin > $yMax) {
return;
}
do {
$pattern[$y][$x] = 1;
} while(!$collision(--$y, $yMin));
if ($y < $yMin) {
$y = $yMin;
}
$yMin+=2;
};
while ($size > 0) {
$shadeLeft($x, $y);
$shadeDown($x, $y);
$shadeRight($x, $y);
$shadeUp($x, $y);
$size-=2;
}
return $pattern;
}
for ($i = 1; $i <= 25; $i++) {
$checkboard = buildSpiral($i);
echo "<h1>$i</h1>";
echo "<table style='margin-bottom: 2em;'>";
for($row=0;$row<count($checkboard);$row++){
echo "<tr>";
for($col=0;$col<count($checkboard);$col++){
if (!isset($checkboard[$row][$col])) {
echo "<td width='50px' height='50px' bgcolor=\"red\"></td>";
} else {
echo "<td width='50px' height='50px' bgcolor=\"black\"></td>";
}
}
echo "</tr>";
}
echo "</table>";
}