I am getting $final value from database and displaying in a table according to user. Now I have a value i.e., this is I was showing in the frontend
Userid Total Letter
36 45
5 67
78 90
42 82
12 57
Back End Code
$sql = mysql_query('SELECT userid, total FROM history');
echo '<table><th>Userid</th>
<th>Total</th>
<th>Grade</th>
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>'.$row['userid'].'</td>
<td>'.$row['$total'].'</td>
$letter = mysql_query('SELECT score,letter FROM letter');
while($row = mysql_fetch_array($letter))
{
$score= $row['score'];
$letterp= $row['letter'];
switch($row['$total'])
{
case $row['$total'] == $score;
echo '<td>'.$letterp.'</td>';
break;
case $row['$total'] >= $score;
echo '<td>'.$letterp.'</td>';
break;
}
</tr>
}
I have a letter table
ID SCORE letter
1 100 A+
2 90 A-
3 80 B+
4 73 B-
5 65 C
6 55 D
7 45 E
8 0 F
Switch case is a sample I wrote here. I need to give condition like if userid scores 45 then I need to show letter E and if userid scores 67 then I need to show B-(b'coz the scorefield defines 65 to 54 is B-) . This is the exact requirement.
Something like this should do:
SELECT
CASE score
WHEN = 100 THEN 'A+'
WHEN >= 90 and < 100 THEN 'A-'
WHEN >= 80 and < 89 THEN 'B+'
WHEN >= 73 and < 80 THEN 'B-'
// and so one...
ELSE 'F'
END as myGrade,
studentID
from
table1
Edit: Sorry, I thought you were trying to do it in the database (where I would have probably done it if I just wanted the grade, not the score. Here is the PHP code anyhow:
$myScore=90; // for example
function getMyScore($myScore)
{
if($myScore==100)
{
return 'A+';
}
elseif ($myScore >= 90 && $myScore <100)
{
return 'A-';
}
elseif ($myScore >= 80 && $myScore <90)
{
return 'B+';
}
// .....
else
{
return 'F';
}
}
$myGrade=getMyGrade($myScore);
echo $myGrade; // output: A-
Based on the function I wrote:
function getMyScore($myScore)
{
if($myScore==100)
{
return 'A+';
}
elseif ($myScore >= 90 && $myScore <100)
{
return 'A-';
}
elseif ($myScore >= 80 && $myScore <90)
{
return 'B+';
}
// .....
else
{
return 'F';
}
}
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>'.$row['userid'].'</td><td>'.$row['$total'].'</td>';
// In your code:
$letter = mysql_query('SELECT score,letter FROM letter');
while($row = mysql_fetch_array($letter))
{
$score= $row['score'];
echo '<td>'.getMyGrade($row['letter']).'</td>';
}
echo'</tr>';
}
Related
I've faced problem that I cant resolve in my simple code
foreach(file('num.txt') as $num) {
foreach(file('char.txt') as $char) {
echo $char.$num. "<br>";
if($char=="C"&&$num==3){
echo 'Found it<br>';
break;
}
}
echo '----------------------------<br>';
}
num.txt contains numbers like this
1
2
3
4
5 per line
char contains character Like A B C D per line
.
and as result
it dose not remove the C3 in text step after break
but i want it like this way without the founded C3
A1
B1
C1
D1
--------------
A2
B2
C2
D2
--------------
A3
B3
C3
Found it
D3
--------------
A4
B4
D4
--------------
A5
B5
D5
--------------
so C is no longer in the loop and continue if add B5 and so on
Please help i need this assignment
If you had regular array, you could remove "C" from it, but since you are getting data implicitly from file, this is the solution. Not the most elegant, but works:
// add flag for found value
$found = false;
foreach(file('num.txt') as $num) {
foreach(file('char.txt') as $char) {
// condition to show an entry
if(!$found || $char != "C") {
echo $char.$num. "<br>";
if($char == "C" && $num == 3) {
echo 'Found it<br>';
// you do not need 'break', remove it and set flag instead
$found = true;
}
}
}
echo '----------------------------<br>';
}
I think your code will be like this:
foreach(file('num.txt') as $num) {
foreach(file('char.txt') as $char) {
if($char=="C"&&$num==3){
echo 'Found it<br>';
break;
}
else{
echo $char.$num. "<br>";
}
}
echo '----------------------------<br>';
}
Hope this help.
<?php
$arr1 = ['A','B','C','D'];
$arr2 = [1,2,3,4,5];
for($i = 0; $i < count($arr1); $i++) {
for($j = 0; $j < count($arr2); $j++){
if($arr1[$i] == "C" && $arr2[$j] == 3){
echo "Found It<br/>";
break;
}
echo $arr1[$i]."".$arr2[$j]."<br/>";
}
echo "----------------------<br/>";
}
?>
code:
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if($grade <= 39)
{
echo '<span class="text-danger">Bad</span>';
}
else if($grade >=74)
{
echo '<span class="text-warning">Average</span>';
}
else if($grade >=100)
{
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
Show grade according to:-
0-39 (Bad)
40-74 (Average)
75-100 (Good)
In this question I want to show message bad, average, good according to grade. Suppose if grade is 0-39 then it will show bad similarly if grade is 40-74 then show average like this but the condition I am giving is wrong. So, how can I do it?
Just change greater than to less than.
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if($grade <= 39)
{
echo '<span class="text-danger">Bad</span>';
}
else if($grade <=74) //Change to less than here.
{
echo '<span class="text-warning">Average</span>';
}
else if($grade <=100) //Change to less than here.
{
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
You need to modify the conditions so that no score is missed out of grade.
So, please define 3 ranges of scores using if and `else if'.
Range 1: 0-39: if ($grade <= 39) {
Range 2: 40-74: else if($grade <=74) {
Range 3: 75-100: else if($grade <=100) {
This way, first if checks if the grade is less than or equal to 39.
If yes, grade is Bad.
Else, if score, does not fit in this range, it will go ahead in next if else for the range: 40-74 and same way to 75-100 if it does not fit.
Corrected code:
if ($outoff!=0) {
$grade = ($score/$outoff)*100;
if ($grade <= 39) { // Score range: 0-39
echo '<span class="text-danger">Bad</span>';
}
// If $score is coming to this else if means it is definitely
// greater than 39: that is 40+
// Score range: 40-74 as it is in else if after if of `39`
else if($grade <=74) {
echo '<span class="text-warning">Average</span>';
}
// Score range: 75-100 as it is in else if after 0 - 39 and 40 - 74
else if($grade <=100) {
echo '<span class="text-success">Good</span>';
}
}
You have to make changes to you code as follow:
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if( $grade >= 0 && $grade < 40 ) {
echo '<span class="text-danger">Bad</span>';
}
else if( $grade > 39 && $grade < 75 ) {
echo '<span class="text-warning">Average</span>';
}
else if($grade > 74 && $grade <= 100 ) {
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if( $grade > 0 && $grade <= 39 ) {
echo '<span class="text-danger">Bad</span>';
}
else if( $grade >= 40 && $grade <= 74 ) {
echo '<span class="text-warning">Average</span>';
}
else if($grade >= 75 && $grade <= 100 ) {
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
As mentioned before, I am a beginner when it comes to PHP coding. I have made a text file which has my students information, as per below - this includes their name, marks for subjects, e-mail address and date of registration:
Ann Thompson : 50,90,82,64,75 : ann#amuniversity.com: 2016-02-01
Jeremiah Hanson: 80, 75, 88 : jeremiah#amuniversity.com: 2016-03-02
Billy Jones: 89, 72, 46, 54 : billy#amuniversity.com: 2016-04-12
and I have also made a form which inputs a field which takes in the filename which the user wishes to read from and a field for the filename which the user wishes to write to.
Where do I start to get the output below?
Summary for students:
Ann Thompson enrolled in 2016-02-01, has an average of 48, with a symbol F.
Jeremiah Hanson enrolled in ....
Billy Jones enrolled in ....
It should also show the date at the bottom when it was last modified.
Please kindly help
Please check the following code. I commented on the code wherever I thought needed. Let me know if you don't understand something from it. And obviously, you need to update the checkGrade function as per your grade system, but you get the idea :)
<?php
// Read the input file line by line into "input" array
$filename = "input.txt";
$input = explode("\n", file_get_contents($filename));
// Go through each line
for($i = 0; $i < sizeOf($input); $i++){
// split line by :
$line = explode(":", $input[$i]);
// Initialize variables based on position
$name = $line[0];
$numbers = $line[1];
$email = $line[2];
$registration = $line[3];
// Calculate average
// Remove any spaces first from the numbers
$numbers = str_replace(' ', '', $numbers);
// Split the numbers by ,
$numbersArray = explode(",", $numbers);
$sum = 0;
// Calculate sum
for($j = 0; $j < sizeOf($numbersArray); $j++){
$num = (int)$numbersArray[$j];
$sum += $num;
}
$avg = $sum/sizeof($numbersArray);
// Print it
echo "$name enrolled in $registration, has an average of $avg, with a symbol ". checkGrade($avg) ."<br>";
}
// Last modified date of the input file
echo "<br><br><br>";
if (file_exists($filename)) {
echo "This file was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
function checkGrade($num){
if ($num < 65){
$grad = 'F';
}
else if ($num<= 66 && $num >=65){
$grad = 'D';
}
else if ($num <= 69 && $num >=67){
$grad = 'D+';
}
else if ($num <= 73 && $num >=70){
$grad = 'C-';
}
else if ($num <= 76 && $num >=74){
$grad = 'C';
}
else if ($num<= 79 && $num >=77 ){
$grad = 'C+';
}
else if ($num <= 83 && $num >=80){
$grad = 'B-';
}
else if ($num <= 86 && $num >=84){
$grad = 'B';
}
else if ($num <= 89 && $num >=87){
$grad = 'B+';
}
else if ($num <= 93 && $num >=90){
$grad = 'A-';
}
else if ($num <= 96 && $num >=94){
$grad = 'A';
}
else if ($num >= 97){
$grad = 'A+';
}
return $grad;
}
?>
Here is the output:
Ann Thompson enrolled in 2016-02-01, has an average of 72.2, with a symbol C-
Jeremiah Hanson enrolled in 2016-03-02, has an average of 81, with a symbol B-
Billy Jones enrolled in 2016-04-12, has an average of 65.25, with a symbol D
This file was last modified: May 03 2017 13:12:13.
How to produce the following output? All numbers should be bold except 10, 20, 30 and 40.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
My current code is:
<?php
$i = 1;
while($i <= 40) {
$m = ($i % 1);
if($m == 0) {
echo '<b><u>' . $i . '</b></u>';
}
$i++;
}
?>
Simple one:
<?php
for ($i=1;$i<=40;$i++){
if ($i % 10 == 0){
$result .= $i;
}
else{
$result .= "<b>".$i."</b>";
}
}
echo $result;
?>
Update 1:
If your logic is to be corrected then,
<?php
$i = 1;
while($i <= 40) {
$m = ($i % 10); // have to replace 1 by 10
if($m == 0) {
echo $i;
}
else{
echo '<b><u>' . $i . '</b></u>';
}
$i++;
}
?>
You can merge the if ($i%10 == 0) into single statement as well.
<?php
$i=1;
while($i<=40)
{
if ($i%10 == 0){
echo $i;
}
else{
echo '<b><u>'.$i.'</b></u>';
}
$i++;
}
?>
one small Correction From 1st answer #Fakhruddin Ujjainwala
Undefined variable: result
<?php
$result = "";
for ($i=1;$i<=40;$i++){
if ($i % 10 == 0){
$result .= $i;
}
else{
$result .= "<b>".$i."</b>";
}
}
echo $result;
?>
I have list of values that I want to fetch from my database. List could be long so I want to divide into two columns if it has over 15 items and to three columns if it has over 30. How can I break it in 3 columns. Eg..
01 | 12 | 23
02 | 13 | 24
03 | 14 | 25
04 | 15 | 26
05 | 16 | 27
06 | 17 | 28
07 | 18 | 29
08 | 19 | 30
09 | 20 | 31
10 | 21 |
11 | 22 |
For now i'm using tables and it has huge if-nest at the beginning of every loop
$result = mysql_query("SELECT namecount FROM table WHERE name='$myvar'");
if (!$result)
echo 'MySQL Error: ' . mysql_error();
else
{
while ($row = mysql_fetch_array($result))
{
$namecount= $row['namecount'];
for($i=1;$i<=$namecount;$i++)
{
if($namecount>15)
{
if($i==1)
echo "\n<table><tr><td width=\"200px\">";
if($namecount%2==0)
{
if($i==$namecount/2)
echo "</td>\n<td>";
}
else
{
if($i==($sailiot+3)/2)
echo "</td>\n<td>";
}
}
//Print content here
if($namecount>15)
{
if($namecount%2!=0 && $i==$namecount)
echo "<h3> </h3><p> <br> </p>"; //if last item and count is odd, print empty item
if($i==$namecount)
echo "\n</td></tr></table>\n";
}
}
}
}
This (kinda) works with two columns, but what about three?
I would extract <tr>, </tr> out of the inner loop, this saves additional ifs. For splitting into multiple columns, you can calculate the number of items in a column and introduce a second intra column variable to keep track of:
while ($row = mysql_fetch_array($result)) {
$namecount = $row['namecount'];
// calculate items per column
$columns = 1;
if ($namecount > 15)
$columns = 2;
if ($namecount > 30)
$columns = 3;
$items = $namecount / $columns;
if ($namecount % $columns > 0)
++$items;
echo "\n<table><tr><td width=\"200px\">";
for ($i = 1, $j = 1; $i <= $namecount; $i++, $j++) {
if ($j > $items) {
echo "</td>\n<td>";
$j = 1; // reset for new column
}
// Print content here
}
if ($namecount % 2 != 0) {
// if count is odd, print empty item
echo "<h3> </h3><p> <br> </p>";
}
echo "\n</td></tr></table>\n";
}
you maybe can try this
$result = mysql_query("SELECT namecount FROM table WHERE name='$myvar'");
if (!$result)
echo 'MySQL Error: ' . mysql_error();
else
{
while ($row = mysql_fetch_array($result))
{
$namecount= $row['namecount'];
for($i=1;$i<=$namecount;$i++)
{
if($namecount<=15){
echo "<table><thead><tr><th>".$your_variable_data."</th></tr></thead>";}
else if ($namecount >15 and $namecount <=30){
echo "<thead><tr><th>".$your_variable_data."</th></tr></thead></table>";
}
}
}
its not tested but it should work for 3 columns if you want more just add else if .
here a demo how it will be
http://jsfiddle.net/TCJjj/107/
use css3 multi column property
<style>
.namecount {
-moz-column-count:3; /* Firefox */
-webkit-column-count:3; /* Safari and Chrome */
column-count:3;
}
</style>
<div class="namecount">
Long Text
</div>
I found that CSS3 multi-columns was horribly inconsistent between browsers, and very buggy (elements with float:left, white-space:nowrap, etc. cause it to break).
So I wrote the following brute-force collator that retains key=>value pairs.
// vars
$list = array('a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z');
$columns = 3;
// sort it
$count = count($list);
$base = ceil($count/$columns);
$keys = array_keys($list);
for ($i=0;$i<$columns;$i++) {
for ($j=0;$j<$base;$j++) {
if (!empty($keys)) {
$col[$i][] = array_shift( $keys );
}
}
}
$sorted = array();
for ($j=0;$j<$base;$j++) {
for ($i=0;$i<$columns;$i++) {
if (isset($col[$i][$j])) {
$sorted[$col[$i][$j]] = $list[$col[$i][$j]];
}
}
}
// check output
echo '<div style="float:left;margin-right:20px"><h3>ORIGINAL</h3>';
foreach ($list as $k=>$v) echo $k .' = '. $v.'<br>';
echo '</div>';
echo '<div style="float:left"><h3>SORTED</h3>';
foreach ($sorted as $k=>$v) echo $k .' = '. $v .'<br>';
echo '</div>';