Student Position ranking in mysql - php

I have a project on student ranking. The sample school does three terminal exam in a year. I created three different tables for each terminal exam, (i.e. firstermsar for first, secondtermsar for second, thirdtermsar for third term respectively).
Table structure is like this:
id studentid matca1 matca2 matexam engca1 engca2 engexam
1 2 15 14 40 12 10 60
2 1 10 5 56 9 13 35
3 4 11 9 45 14 17 40
4 5 14 1 50 20 0 60
Students with id 4 and 5 are in class1 while students 2 and 1 are in in class2. I have a seperate table for classes and also seperate table for student profile info.
Now, I already output the result slip but without ranks which looks like this
StudentId: 2 Full Name: Fawaz James Class: Pry 2 Overall Position: -
1stAss 2ndAss ExamScore Grade Position Rmks
Mark Obtainable 20 20 60
English 12 10 60 A - Excellent
Mathematics 15 14 40 B - V. Good
Though am a starter, and my code is a mess, its provided underneath. Moreover I need help with calculating the overall position on class basis and subject position on class basis. Please help me as my project depends on this for approval and am in my deadline week before I got introduced to stack Overflow.
$query = "SELECT * FROM firsttermsars
WHERE studentID=$stuID AND year=\"$_SESSION[year]\""; #27
$result = mysql_query($query)
or die ("Couldn't execute query.");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<h2> First Term Stint Report Sheet for $namet during the SESSION $year</h2>";
echo "<table class='table1'>";
echo "<tr>";
echo "<td></td>";
echo "<td><b>1st Assessment</b></td><td><b>2nd Assessment</b></td>
<td><b>Mid Term<br />Exam</b></td><td><b>Mid Term<br />Total Score</b></td><td><b>Grade<br />Point</b></td></tr>";
echo "<tr> <td><b>Mark Obtainable</b></td><td><b>30</b></td><td><b>20</b></td><td><b>50</b></td><td><b>100</b></td><td><b>||=||</b></td></tr><tr>";
$tetal = $row['thirdengtest']+$row['thirdengexam']+$row['eng1'];
if ($tetal >="1") {
$q2 = "1";
$sq2 = "100";
echo "<td>English Language: </td>";
echo "<td>{$row['eng1']}</td><td>{$row['thirdengtest']}</td><td>{$row['thirdengexam']}</td> <td>";
$total1 = $row['thirdengtest']+$row['thirdengexam']+$row['eng1'];
echo $total1."</td><td>";
if ($total1>="75")
echo "A1";
elseif ($total1>="70")
echo "B2";
elseif ($total1>="65")
echo "B3";
elseif ($total1>="60")
echo "C4";
elseif ($total1>="55")
echo "C5";
elseif ($total1>="50")
echo "C6";
elseif ($total1>="45")
echo "D7";
elseif ($total1>="40")
echo "E8";
elseif ($total1>="1")
echo "F9";
elseif ($total1=="0")
echo "-";
else
echo "F";
echo "</td></tr>";
}
$tetal = $row['thirdmathtest']+$row['thirdmathexam']+$row['math1'];
if ($tetal >="1") {
$q3 = "1";
$sq3 = "100";
echo "<tr>";
echo "<td>Mathematics </td>";
echo "<td>{$row['math1']}</td><td>{$row['thirdmathtest']}</td> <td>{$row['thirdmathexam']}</td> <td>";
$total2 = $row['thirdmathtest']+$row['thirdmathexam']+$row['math1'];
echo $total2."</td><td>";
if ($total2>="75") echo "A1"; elseif ($total2>="70") echo "B2"; elseif ($total2>="65") echo "B3"; elseif ($total2>="60") echo "C4"; elseif ($total2>="55") echo "C5"; elseif ($total2>="50") echo "C6"; elseif ($total2>="45") echo "D7"; elseif ($total2>="40") echo "E8"; elseif ($total2>="1") echo "F9"; elseif ($total2=="0") echo "-"; else echo "F";
echo "</td></tr>";
}
?>
I hope you can help me like this, and hope is not complicated. I wanted to add the picture of the output but its complaining i don't have the required reputation

Firstly, in the database, make a new column for total('Total') and ('Grade'), and when you are submitting the marks details to your database, add the marks and grade and then submit the total to the database, instead of calculating them while retrieving the information.
Second, when you are selecting students from the database, use the SQL command:
SELECT * FROM firsttermsars ORDER BY Total DESC
More info on ORDER BY
This will automatically return the list of students in descending order of their total marks.
Similarly, for subject ranking, you can have a separate table, where ranking is done on subject marks
Then, while retrieving the data,
<table>
<?php
while ($i<$num) //num is number of rows
{
?>
<tr><td><?php echo mysql_result($result,$i,"Name"); ?></td>
//... Similarly for all clumns
<?php
}
?>
</table>
This should make the table with all students ranked according to their total marks. Similarly you can make a table for ranking by subject marks.
This is how i would solve the problem. Hope I helped!

Although i am late, but it may help some one.
First of All you should calculate Obtained marks and store in database table.
For simplicity i am showing just 3 column.
Id Name Obtained
1 A 100
2 B 58
3 L 88
4 F 102
5 C 99
Insert the row in table , then run this query to get Position of student.
SELECT id, name, Obtained, FIND_IN_SET( score, (
SELECT GROUP_CONCAT( score
ORDER BY score DESC )
FROM scores )
) AS position
FROM scores
WHERE id = 5
Detail here https://dba.stackexchange.com/questions/13703/get-the-rank-of-a-user-in-a-score-table

Related

php mysqli query select

I have 3 different tables
table 1 keep general scores over the year
table 2 and 3 is event specific
only common field in all 3 is a member ID
I need to select 5 top score results for each member from table1 combine them with 1 specific result from table 2 and 3
I managed to get everything together in a temp table, but cnt get the output the way I need it
example what i have and need.
Original code
$prevQuery = "SELECT distinct member_id FROM scores";
$prevResult = $conn->query($prevQuery);
while($row = $prevResult->fetch_assoc()) {
$scores=("SELECT member_id,event_id,event_date,event_score FROM scores where member_id = ".$id." ORDER BY event_score DESC LIMIT 5");
Query:
select * from temp_table order by mem_id asc
PHP:
you can simple do your expected result in your application like this
<?php
$result = array(array('mem_id'=>1,'location'=>'A','date'=>'20/05/2017','score'=>100),array('mem_id'=>1,'location'=>'B','date'=>'21/05/2017','score'=>103),array('mem_id'=>1,'location'=>'C','date'=>'22/05/2017','score'=>106),array('mem_id'=>1,'location'=>'C','date'=>'23/05/2017','score'=>108),
array('mem_id'=>2,'location'=>'A','date'=>'20/05/2017','score'=>105),array('mem_id'=>2,'location'=>'B','date'=>'21/05/2017','score'=>109),array('mem_id'=>2,'location'=>'C','date'=>'22/05/2017','score'=>111),array('mem_id'=>2,'location'=>'C','date'=>'23/05/2017','score'=>110));
$new_result=array();
foreach($result as $key=>$row)
{
$new_result[$row['mem_id']][]=$row;
}
echo "<table border='1px'>";
echo "<thead><tr><th>S.No</th><th>date</th><th>score1</th><th>date</th><th>score2</th><th>date</th><th>score3</th><th>date</th><th>score4</th></tr>";
echo "<tbody>";
foreach($new_result as $key=>$row)
{
echo "<tr><td>".$key."</td>";
foreach($row as $key1=>$row1)
{
echo "<td>".$row1['date']."</td>";
echo "<td>".$row1['score']."</td>";
}
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
OUTPUT :
S.No date score1 date score2 date score3 date score4
1 20/05/2017 100 21/05/2017 103 22/05/2017 106 23/05/2017 108
2 20/05/2017 105 21/05/2017 109 22/05/2017 111 23/05/2017 110

how to loop through a database table of numbers until you have sum of an input number php/sql

So basically if you have a table in your database which looks like this:
id :: name :: numbers 1 ::: Jack ::::::: 4 2 : Katrina ::::: 23 :: Clyde :::::: 8
I am looking to loop through the numbers column adding the numbers to each other until it reaches a certain number input and then echos out the one row, where the numbers has added up to.
So if your input is 3 it will output the row with Jack, if the input is 5 or 6 it will output Katrina and if the input is from 7-14 it will output the row with clyde.
The thing here I cannot figure out is how i loop through the numbers column adding up the numbers until you reach a specific row, then to echo only that specific row out.
I know how to echo out all rows and creating the condition for the input field but I seem to be stuck at grasping how to go further.
$sql = "SELECT medlemsid, navn, lotterinr, tid FROM medlemmer";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "| id: " . $row["medlemsid"]. " | - Name: " . $row["navn"]. " " ." | Lotterinr " . $row["lotterinr"]. " | Tid: ". $row["tid"]. "<br>";
}
} else {
echo "0 results";
}
if (isset($_POST['numberinput'])) {
$numbers = $_POST['numberinput'];
}
$con->close();
?>
<div id="udlodning-wrapper">
<form name="login-form" class="login-form" method="post">
<div>
<input name="numberinput" type="number" id="numberinput" placeholder="Lotterinummer" />
</div>
<div>
<input type="submit" name="submit" value="Udlodning" id="udlodning-submit" class="button-input"/><br/>
</div>
</form>
</div>
Use self join to find out sum of number of people with id equal or less than current person
select
p1.id,
p1.name,
p1.number,
sum(p2.number) as sum
from people p1
join people p2 on p2.id <= p1.id
group by p1.id, p1.name, p1.number;
SQL above would produce a result like this:
id name number sum
1 jack 4 4
2 katrina 2 6
3 clyde 8 14
You can then iterate through the result, checking your input against sum

Select where two rows have the same column value and echo them

In the table "ogitems", I have a column called "itemname", "month", and "price".
There duplicate items, arranged by month.
I want to check "itemname" to see if any rows have the same value for itemname, and if yes, echo the "itemname" along with the last two months and the pricing for these two months.
EXAMPLE:
"itemname" = CHEESE CHEDDAR
CHEESE CHEDDAR is found in months 11 AND 12 (so there is two records/rows for CHEESE CHEDDAR). 11 has "price" of 51 while 12 has "price" of 54
I want to echo that information into an anchor tag...
<a href="pricetrend.php?date=<?php echo "" . $row['month'] . "" ?>&price1=<?php echo $price1; ?>&price2=<?php echo $price2; ?>&item=<?php echo "" . $row['itemname'] . ""?>;">
Basically, I am trying to grab the two most recent prices for one item and echo them into this anchor tag. Thanks in advance for any help!
I am assuming that your month is a date field, this should work
SQL Fiddle link: http://sqlfiddle.com/#!2/d63a0/4
SELECT * FROM (
SELECT
o.itemname,
o.month,
( SELECT price FROM ogitems i WHERE i.itemname = o.itemname AND i.month<o.month ORDER BY MONTH DESC LIMIT 1) AS last_month_price,
o.price AS current_month_price
FROM
ogitems o
ORDER BY
o.month DESC
) AS items
GROUP BY
items.itemname
HAVING
items.last_month_price IS NOT NULL
select * from ogitems where itemname='CHEESE CHEDDAR' order by month desc limit 2
This sql statement should fix your problem
<?php
$result = mysql_query("select * from ogitems where itemname='CHEESE CHEDDAR' order by month desc limit 2")
while ($row = mysql_fetch_assoc($result)) {
echo $row["itemname"];
echo $row["month"];
echo $row["price"];
}
?>

PHP Ajax MySql Pagination

I am using AJAX Pagination for displaying my records in application from MySQL using PHP.
I have done that successfully but just need one solution:
I need to display a Serial No while displaying records like:
----------------------------
S.No Name Country
----------------------------
1 Sandeep India
2 Rahul Japan
3 Riya China
4 Sohan India
...
50 James USA
If I have set to show 20 results per page while paginating the first page shows serial no. 1 to 20 , 2nd page shows serial no 1 to 20 and 3rd page shows serial no 1 to 10.
But I want to show serial no. 1 - 20 in first page , 21- 40 in second page , 41- 50 in 3rd page.
How can I do this in PHP?
Here is my code:
$ssql = $ssql_select.$ssql_where." order by reg_date desc LIMIT $Page_Start , $Per_Page";
$rs=mysql_query($ssql, $cn) or die("MySQL error: ".mysql_error());
$ctr = 1;
while ($row = mysql_fetch_object($rs)) {
echo "<td align=center>".$ctr."</td>";
echo "<td align=center>".$row->name."</td>";
echo "<td align=center>".$row->country."</td>";
$ctr++;
}
Try
...
while ($row = mysql_fetch_object($rs)) {
echo "<td align=center>" . $ctr + $Page_Start . "</td>";
echo "<td align=center>".$row->name."</td>";
echo "<td align=center>".$row->country."</td>";
$ctr++;
}
...

php mysql calculate each percentage

how do i calculate percentage in mysql for each user?
$q = $db->query("SELECT * FROM report LEFT JOIN user ON user.user_id= report.id_user WHERE date='$today' GROUP BY id_user");
$q1 = $db->query("SELECT SUM(r_amount) AS total FROM report WHERE date='$today' AND id_user=id_user");
$r1 = $q1->fetch_assoc();
$totalCharge = $r1['totalCharge'];
$sixtyPercent = $totalCharge * 0.60;
$fortyPercent = $totalCharge * 0.40;
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
echo '<tr>';
echo '<td>'.$r['user_name].'</td>';
echo '<td align="center">'.$fortyPercent.'</td>';
echo '<td align="center">'.$sixtyPercent.'</td>';
echo '<td align="center"><strong></strong></td>';
echo '</tr>';
endwhile;
current result:
name 60% cut 40% cut total
user 1 60 40 100
user 2 60 40 100
user 3 60 40 100
expecting result:
name 60% cut 40% cut total
user 1 60 40 100
user 2 24 16 40
user 3 48 32 80
You aren't getting the expected results because your $totalCharge variable will never change (nor will it ever match the proper user_id).
Most likely what you'll want to do is write a SQL statement which combines the two statements you currently have, then simply loop through the results of that statement and do all of the calculations within that loop.
The SQL statement might look something like:
SELECT SUM(r_amount) AS totalCharge, report.*, user.*
FROM report
JOIN total ON total.id_user = report.id_user
LEFT JOIN user ON user.user_id= report.id_user
WHERE date='$today' GROUP BY user_id
You'll likely have to tweak it a little to get the expected results.
Then your loop will look something like:
while($r = $q->fetch_array(MYSQLI_ASSOC)) {
$totalCharge = $r['totalCharge'];
$sixtyPercent = $totalCharge * 0.60;
$fortyPercent = $totalCharge * 0.40;
echo '<tr>';
echo '<td>'.$r['user_name'].'</td>'; // corrected syntax error here
echo '<td align="center">'.$sixtyPercent.'</td>';
echo '<td align="center">'.$fortyPercent.'</td>';
echo '<td align="center"><strong></strong></td>';
echo '</tr>';
}
Hope this guides you to the solution.

Categories