multisort for marks and gpa - php

I am trying to generate a student tabulation sheet. How can I display students details with ranking according to Marks & GPA(Descending). currently I can display by either Marks or GPA with the following code:
arsort($myGpa);
foreach($myGpa as $x=>$myValue)
{
?>
<tr>
<td><?php echo $sn++; ?></td>
<td><?php echo $myName[$x]; ?></td>
<td><?php echo $myCadet[$x]; ?></td>
<td><?php echo $myTotal[$x]; ?></td>
<td><?php echo $myGrade[$x]; ?></td>
<td><?php echo number_format($myValue,2); ?> </td>
<td><?php //$position++;
$myposition = $position++;
if($myposition == '1') { echo '1st'; }
else if($myposition == '2') { echo '2nd'; }
else if($myposition == '3') { echo '3rd'; }
else echo $myposition.'th';
?> </td>
</tr>
and another thing is: if the marks & gpa are same then how to show the position ?
thanks in advance.

actually.. I want to sort the students according to marks & gpa. currently my output is as follows:
Total Marks - Grade - GPA - Position
813 - A - 4.50 - 1
819 - A - 4.44 - 2
779 - A - 4.31 - 3
826 - A - 4.31 - 4
but as you see 2 students got same gpa(4.31) but different marks. I want the marks to be sorted when the gpa is same. that means if 2 students have same gpa then the position will be calculated according to marks.
currently I am using the following code(sort by GPA only)
$myName[] = $student_name['name'];
$myCadet[] = $stud['cadet_no'];
$myTotal[] = $all_total;
$myGrade[] = $final_grade;
$myGpa[] = $final_gpa;
arsort($myGpa);
foreach($myGpa as $x=>$myValue)
{
?>
<tr>
<td><?php echo $sn++; ?></td>
<td><?php echo $myName[$x]; ?></td>
<td><?php echo $myCadet[$x]; ?></td>
<td><?php echo $myTotal[$x]; ?></td>
<td><?php echo $myGrade[$x]; ?></td>
<td><?php echo number_format($myValue,2); ?> </td>
<td><?php //$position++;
$myposition = $position++;
if($myposition == '1') { echo '1st'; }
else if($myposition == '2') { echo '2nd'; }
else if($myposition == '3') { echo '3rd'; }
else echo $myposition.'th';
?> </td>
</tr>
}

Related

Trying to get property of non-object using OCI connection

I'm new in PHP with OCI connection. I want to retrieve some data from database and insert it into a table form. But it keep show an error
Trying to get property 'attribute' of non-object
I have try to use oci_fetch object / oci_fetch_array, but it still the same. I also have followed some tutorial but it doesn't help.
This is for my mini project for this semester.
Here my source code:
$sql="SELECT borrow.book_id, book_title, borrow.stud_id, stud_name, book_bdate, return_date, due_date
FROM book
JOIN borrow
ON book.book_id = borrow.book_id
JOIN student
ON borrow.stud_id = student.stud_id
where borrow.stud_id = '$stud_id'
ORDER BY 5 DESC ";
$query=oci_parse($link,$sql) or die ("error here!");
oci_execute($query);
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row->stud_id; ?></td>
<td><?php echo $row->book_id; ?></td>
<td><?php echo $row->book_title; ?></td>
<td><?php echo $row->book_bdate; ?></td>
<td><?php echo $row->due_date; ?></td>
<td><?php echo $row->return_date; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row->book_id; ?>'>Update</a></center>
</td>
<tr>
<?php
}
}
oci_close($link);
?>
Oracle returns field names as uppercase by default so you need to use uppercase indexes like so:
Here the solution where I got.
Btw thank you everyone for helping me.
Correct Your while loop You are fetching data as array:
oci_fetch_array($query, OCI_ASSOC) // return associated array
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row['stud_id']; ?></td>
<td><?php echo $row['book_id']; ?></td>
<td><?php echo $row['book_title']; ?></td>
<td><?php echo $row['book_bdate']; ?></td>
<td><?php echo $row['due_date']; ?></td>
<td><?php echo $row['return_date']; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row['book_id']; ?>'>Update</a></center>
</td>
<tr>
<?php
}

php find a specify word in a specific column from stored proc sql server

I'm kind of new in php and i'm searching a way to get some words from a specific column that i get from a sql server stored proc. These 'words' will me to use them as style for my css
Example :
Column HTML_CODE has sometime 'bold', 'green' or something else. Sometime they are in the same column, not always in the same order. I don't know how to use them in my loop. See what i has until now :
$result = sqlsrv_query($conn, $sql);
while ($row = sqlsrv_fetch_array($result))
{
//print_r( $row ); // debug code
if ($row['HTML_CODE'] == 'BOLD(), Green()'){
$couleur='green';
$font= 'bold';
}
else {
$couleur='black';
$font= 'normal';
}
?>
<tbody>
<tr>
<?php echo "<tr style=\"font-weight:$font; color:$couleur;\">"; ?>
<td><?php echo ($row['Nom']); ?></td>
<td><?php echo ($row['Quantite']); ?></td>
<td><?php echo ($row['Montant']); if (is_numeric($row['Montant'])) {
echo ' $';
}?></td>
<td><?php echo ($row['#GL']); ?></td>
<td><?php echo ($row['Debit']); if (is_numeric($row['Debit'])) {
echo ' $';
} ?></td>
<td><?php echo ($row['Credit']); if (is_numeric($row['Credit'])) {
echo ' $';
}?></td>
</tr>
</tbody>
if( strstr($row['HTML_CODE'], "BOLD()")){ $font= 'bold'; }

How to display data in table based on the year in PHP

I want to display based on the year with repeated first_ ids and actual ids. but actual_id is unique.
<?php while($Row=oci_fetch_assoc($Result)){
$ResultArray[$j]['First_ID']=$Row['First_ID'];
$ResultArray[$j]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j]['Title']=$Row['Title'];
$ResultArray[$j]['START_DATE']=$Row['START_DATE'];
$startDate=explode(' ',$Row['START_DATE']);
$ResultArray[$j]['YEAR']=$startDate[0];
if(($startDate[0])==($startyear)){
$ResultArray[$j][$startyear]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear]['Content']=$Row['Content'];
}
if(($startDate[0])==($startyear+1)){
$ResultArray[$j][$startyear+$i]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear+$i]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear+$i]['Content']=$Row['Content'];
}
if(($startDate[0])==($startyear+1)){
$ResultArray[$j][$startyear+$i]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear+$i]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear+$i]['Content']=$Row['Content'];
}
$j++;
}?>
//to display in table
it display year with asc order first and then next year etc.
<table class="tab">
<tr><th>S.No</th>
<th>first_ id</th><th>Title</th>
<?phpfor($i=0;$i<4;$i++){?><th colspan="3"><?php echo $startyear+$i; ?> </th>
<?php }?>
<tr><th></th><th></th><th></th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th>
<th>Actual_ID</th><th>Other_ID</th> <th>Content</th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th></tr>
<?php
foreach($ResultArray as $ResultArray1){
//print_r($abstarctResultArray1);
?><tr><td><?php echo $count; $count++;?></td>
<td><?php echo $ResultArray1['First_ID'];?></td>
<td><?php echo $ResultArray1['Title'];?></td>
<?php for($i=0;$i<4;$i++){
$startDate=explode(' ',$ResultArray1['START_DATE']);
if(($startDate[0]==$startyear)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
<?php }
else if(($startDate[0]==$startyear+1)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
</tr>
<?php
}
else if(($startDate[0]==$startyear+2)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
</tr>
<?php
}
}
}
?>
I have tried with different ideas, but I can't. Please help.

How to showing report that group by archieve with PHP & MySQL?

I want to recapitulate report that grouped by month. But the results of sum is incorrect. It's just sum data in one month and show in all rows.
<?php
$query = mysql_query("SELECT * FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php
$Masuk= mysql_query("SELECT SUM(barang_in) AS masuk FROM barang GROUP BY arsip");
if($Masuk){
$data = mysql_fetch_assoc($Masuk);
echo $data['masuk'];
}
?></td>
<td><?php
$keluar= mysql_query("SELECT SUM(bijih_out+htm_out+pth_out) AS keluar FROM barang GROUP BY arsip");
if($keluar){
$data = mysql_fetch_assoc($keluar);
echo $data['keluar'];
}?></td>
<td><?php
$efisiensi= mysql_query("SELECT SUM((bijih_out+htm_out+pth_out)-barang_in) AS efisiensi FROM barang GROUP BY arsip");
if($efisiensi){
$data = mysql_fetch_assoc($efisiensi);
echo $data['efisiensi'];
}
?></td>
<td><?php
$persen= mysql_query("SELECT SUM(barang_in/(bijih_out+htm_out+pth_out))*1 AS persen FROM barang GROUP BY arsip");
$simbol = "%";
if($persen){
$data = mysql_fetch_assoc($persen);
echo number_format($data['persen'],2); echo $simbol;
}
?></td>
<?php
$i++;
}
?>
so that's my code. what's wrong with that? There are some columns for displaying income items, outcome items, efficiency ( difference of outcome and income items ) and percent of efficiency.
finally I found out the right code. Thankyou for #RandomSeed who helps me. This is my final code and it works!
<?php
$query = mysql_query("SELECT arsip, SUM(barang_in) AS income, SUM(bijih_out+htm_out+pth_out) AS outcome, SUM((bijih_out+htm_out+pth_out
)-barang_in) AS efficiency, SUM((barang_in / ( bijih_out + htm_out + pth_out))*100) AS percent FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php echo $data['income']; ?></td>
<td><?php echo $data['outcome']; ?></td>
<td><?php echo $data['efficiency']; ?></td>
<td><?php $simbol = "%"; echo number_format($data['percent'],0); echo $simbol; ?></td>
<?php
$i++;
}
?>

Set proper conditional statement in WHILE LOOP

After submitting a mysqli_query to select open/unfulfilled orders from a cart database the results is output to the screen with a standard WHILE LOOP.
while($row = mysqli_fetch_array($oresult)) { include('orders-open.php');}
orders-open.php is simply the internal part of a TABLE:
<tr style="color:#FFF;">
<td><?php echo $row['Buyer']; ?></td>
<td><?php echo $row['Cart_Date']; ?></td>
<td><?php echo $row['Item_Number']; ?></td>
<td><?php echo $row['Item_Title']; ?></td>
<td><?php echo $row['Item_FPrice']; ?></td>
<td><?php echo $row['Item_Qty']; ?></td>
</tr>
So here is my question, I want to apply a simple HTML "<HR>" tag between the records at the point where the value in $row['Buyer'] changes, so by example:
John Doe 9/11/13 123456 Item 1 $5.99 5
John Doe 9/11/13 123654 Item 2 $8.99 3
John Doe 9/9/13 321456 Item 3 $4.99 2
(HR - Horizontal Rule Tag here)
Mike Doe 9/7/13 123555 Item 1 $9.99 2
Mike Doe 9/7/13 123777 Item 2 $2.99 6
What would be the best way to write the conditional statement inside the WHILE LOOP to compare the $row[Buyer'] result to the previous $row['Buyer'] result?
$first_run = TRUE;
$previous_buyer = NULL;
while($row = mysqli_fetch_array($oresult)) {
if($first_run) {
$first_run = FALSE;
$previous_buyer = $row['Buyer'];
}
include('orders-open.php');
}
And then in your include file:
<?php if($previous_buyer != $row['Buyer']) {
echo '<HR width="100%">';
$previous_buyer = $row['Buyer'];
} ?>
<tr style="color:#FFF;">
<td><?php echo $row['Buyer']; ?></td>
<td><?php echo $row['Cart_Date']; ?></td>
<td><?php echo $row['Item_Number']; ?></td>
<td><?php echo $row['Item_Title']; ?></td>
<td><?php echo $row['Item_FPrice']; ?></td>
<td><?php echo $row['Item_Qty']; ?></td>
</tr>
$prev_buyer = '';
while($row = mysqli_fetch_array($oresult))
{
if($prev_buyer !== $row['Buyer'])
{
//Do Something
}
include('orders-open.php');
$prev_buyer = $row['Buyer']
}
Just as Elon said above - change this:
while($row = mysqli_fetch_array($oresult)) { include('orders-open.php');}
to
$old_buyer = null;
while($row = mysqli_fetch_array($oresult)) {
if ($row['Buyer'] != $old_buyer) {
echo '<hr>';
}
$old_buyer = $row['Buyer'];
include('orders-open.php');
}
You could do it like this:
$buyer = null ; //Cache buyer in a variable, because $row is reset in the loop.
while($row = mysqli_fetch_array($oresult)) {
include('orders-open.php');
if ($buyer !== $row["Buyer"]){
echo ($buyer !== null) ? "<hr/>" : "" ;
$buyer = $row["Buyer"] ;
}
}

Categories