Displayind data in a codeigniter view using a table - php

Hi am having problem in sorting the correct <td> to display subject results for students. i have 11 subjects offered in the school and senior students take 8 subjects because 4 are electives at this level. so when fetching results for the senior students and displaying when the student does not take that subject it still echoes the result in the wrong field.my code below does not distinguish e.g if the <td> is physics or biology.
i want it to echo a (-) where a student does not take the subject.thanks in advance
<table border='1'>
<tr><th>Position</th><th>Students</th><th>English</th><th>Kiswahili</th><th>Maths</th><th>Biology</th><th>Physics</th><th>Chemistry</th><th>History</th><th>Geography</th><th>CRE</th><th>Agriculture</th><th>Business</th><th>Total Marks</th><th>Mean Grade</th><th>Aggregate Points</th><th>Action</th>
</tr>
<?php
//loop to display the names of students
$i = 1;
foreach ($overallresults as $result) {
echo "<tr>";
echo "<td>";
echo $i++;
echo "</td>";
$admNo = $result->admNo;
$total_marks = $result->total_marks;
$agp = $result->aggregate_points;
$mean_grade = $result->mean_grade;
$agp = $result->aggregate_points;
$result_id = $result->result_id;
$fname = "";
foreach ($students as $student) {
// print_r($student);
$admNo1 = $student->admNo;
if ($admNo == $admNo1) {
// print_r($student);
$fname = $student->firstName;
$mname = $student->middleName;
$lname = $student->lastName;
//}
// }
//echo "<tr>";
echo "<td>";
echo $fname . " " . $mname . " " . $lname;
echo "</td>";
}
}
foreach ($subjectresults as $subresult) {
// print_r($result);
$score = "0";
$admNo3 = $subresult->admNo;
$subCode = $subresult->subCode;
$score = $subresult->score;
if ($admNo == $admNo3) {
if ($subCode == '232') {
$score = $score;
}
if ($subCode == '101') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '102') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '121') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '231') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '232') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '233') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '311') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '312') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '313') {
echo "<td>";
echo $score;
echo "</td>";
}
if ($subCode == '443') {
echo "<td>";
if (!$score) {
echo 0;
} else {
echo $score;
}
echo "</td>";
}
if ($subCode == '565') {
echo "<td>";
echo $score;
echo "</td>";
}
}
}
?>
<?php
if (isset($term)) {
$term = $term;
}
if (isset($form)) {
$form = $form;
}
if (isset($year)) {
$year = $year;
}
if (isset($examCategory)) {
$examCategory = $examCategory;
}
//if ($admNo == $admNo1) {
// print_r($student);
//}
// }
echo "<td>";
echo $total_marks;
echo "</td>";
echo "<td>";
echo $mean_grade;
echo "</td>";
echo "<td>";
echo $agp;
echo "</td>";
echo "<td>";
echo "</td>";
//}
}
?>
</table>
<?php
}
?>
</div>
the above code works but displays the subject done by students in the wrong table data field.the subjects are identified using subject codes like english is 101 while kiswahili is 102,maths is 121

I can see that the subjects are static. I would rather you have a data structure for Results, with proper relation's based on student id, course id. then you can loop all the Results and fetch the relevant subjects names and student names. eg;
<?php
$results = array();
$subjects = array(101=>"English", 102=>"Kiswahili",103=>"Physics",104=>"Chemistry");
$students = array("tom","dick","ally");
$result1 = array(
'studentId'=>1,
'score'=>array(
101=>20,
102=>30,
103=>30,
104=>45
),);
$result2 = array(
'studentId'=>2,
'score'=>array(
101=>34,
102=>54,
103=>77
),);
$results[] = $result1;
$results[] = $result2;
echo "<table border='1'>";
echo "<tr>";
echo "<th>#</th><th>Student</th>";
for($i = 101; $i < 105; $i++){
echo "<th>".$subjects[$i]."</th>";
}
echo "</tr>";
$count = 1;
foreach($results as $result){
echo "<tr>";
echo "<td>".$count."</td>";
echo "<td>".$students[$result['studentId']]."</td>";
foreach($subjects as $key => $value){
$marks = $result['score'][$key]!=null?$result['score'][$key]:'-';
echo "<td>".$marks."</td>";
}
echo "</tr>";
$count++;
}
echo "</table>";
?>
Ofcourse you will have to write Helper functions for fetching student names and calculating the mean scores.

Related

SQL query html table format

How to echo the left column table to "Daily" "Monthly" and "Yearly"? It currently displays repeatedly because the results of SQL queries on the right side are 3 items.
$ID = get_current_user_id();
global $wpdb;
if(isset($_GET["view_data"])){
$SubmissionID = $_GET["view_data"];
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}myTable WHERE user_id = {$ID} AND `key` IN ('name1', 'name2', 'name3')", OBJECT );
if(isset($_GET["form_name"])) echo "<h4>YOUR REVIEW ON {$_GET["form_name"]}</h4>";
echo "<table>";
foreach ($results as $result) {
echo "<tr>";
echo "<td>";
echo "<p>test1</p>";
echo "</td>";
echo "<td>";
echo $result->value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "<h4><a href='?view_all'>Back to List</a></h4>";
}
The current output of my code:
test1
dynamic_data_1
test1
dynamic_data_2
test1
dynamic_data_3
The output I need:
Daily
dynamic_data_1
Monthly
dynamic_data_2
Yearly
dynamic_data_3
add a counter:
$ID = get_current_user_id();
global $wpdb;
if(isset($_GET["view_data"])){
$SubmissionID = $_GET["view_data"];
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}myTable WHERE user_id = {$ID} AND `key` IN ('name1', 'name2', 'name3')", OBJECT );
if(isset($_GET["form_name"])) echo "<h4>YOUR REVIEW ON {$_GET["form_name"]}</h4>";
$count = 0;
echo "<table>";
foreach ($results as $result) {
$count++;
$title = 'DEFAULT Title';
if( $count == 1 ){
$title = 'Daily';
}
elseif( $count == 2 ){
$title = 'Monthly';
}
elseif( $count == 3 ){
$title = 'Yearly';
}
echo "<tr>";
echo "<td>";
echo "<p>" . $title ."</p>";
echo "</td>";
echo "<td>";
echo $result->value;
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "<h4><a href='?view_all'>Back to List</a></h4>";
}

Decrypt data from MySQL database

I have the below code to show all data from a MySQL database in a HTMl database:
<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
This code works fine and the data is displayed in the table correctly, my problem is that most of the data in the DB is encrypted (simply)!
For example, here is how someones first name is stored 5cGs+mhdNN/SnHdqEbMk6zlKRu8uW7pR1zRjQHV9DGQ=
Is there a way to decrypt the data before displaying it in the table?
I usually decrpyt the date in the following way:
$name_firstD = simple_decrypt($name_first , "secured");
You need to have an array of columns which are encrypted.
<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
$encrypted_columns = array('password','code', 'first_name');
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>" . (in_array($key,$encrypted_columns))? simple_decrypt($value , "secured") : $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
put the name of your encrypted column inside $encrypted_columns array.

paging is not working for jquery datatable

i am using jquery table to show table on a web page.The table is coming up properly but paging for the table and showing number of records and search options are not working for the table generated.All the records are populating at the load of the page.This is my code
PHP code
$qry = " SELECT AssetId,";
$qry .= $data;
$qry .= " from Completedetails";
mysql_select_db($database_finalkms, $finalkms);
$query_getcolumns = $qry;
$getcolumns = mysql_query($query_getcolumns, $finalkms) or die(mysql_error());
$row_getcolumns = mysql_fetch_assoc($getcolumns);
$totalRows_getcolumns = mysql_num_rows($getcolumns);
if (($getcolumns)||(mysql_errno == 0))
{
echo "<table width='50%' id='sample_2'><thead><tr>";
if (mysql_num_rows($getcolumns)>0)
{
//loop thru the field names to print the correct headers
$i = 0;
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
//display the data
while ($rows = mysql_fetch_array($getcolumns,MYSQL_ASSOC))
{
echo "<tbody><tr >";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</tbody></table>";
}else{
echo "Error in running query :". mysql_error();
}
?>
Java Script
<script>
jQuery(document).ready(function() {
// initiate layout and plugins
$("#sample_2").dataTable({"bPaginate": true});
});
</script>
<input name="hdnfld" id="hdnfld" type="hidden" value="<?php echo $qry;?>"/>
Please help me in this regard.
Try the following modified code of yours, if still not working then give the live URl where ur html output is displyed.
if (($getcolumns)||(mysql_errno == 0))
{
// Displying the headers
$i = 0;
echo "<table width='50%' id='sample_2'><thead><tr>";
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
// Data Section
echo "<tbody>";
if (mysql_num_rows($getcolumns)>0)
{
while ($rows = mysql_fetch_array($getcolumns, MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
echo "</tr>";
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</tbody></table>";
}else{
echo "Error in running query :". mysql_error();
}

PHP - Adding a separator in a Loop

I asked this question yesterday but no one replied after I tried to get further help. I've been trying to figure out the problem in the code I was given in my previous post but it's not giving me the results I want. Right now what it does is it writes a separator after each row but what I really want is if the name of the manager from the previous row is different then write a separator and keep going on with loop.
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($rows['Manager'] !== $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
}
You're mixing $row and $rows, you would be okay doing this:
$team = "";
while ($row = mysqli_fetch_array($results)) {
if($team && ($row['Manager'] != $team)){ // skip first separator
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
$team = $row['Manager'];
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
Am I missing something, or can you simply display the data, then assign it to the variable further in the loop?
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($row['Manager'] !== $team){
echo "<tr><td colspan=\"3\">".$team."</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
$team = $row[0]['Manager'];
}
}
What is $rowS['Manager'] ? $rowS is unknown. use $row['Manager'] like:
if($row['Manager'] !== $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($rows['Manager'] !== $team) {
$team = $row['Manager'];
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
}
Just move the value allocation $team = $rows['Manager']; in the if condition.
while ($row = mysqli_fetch_array($results)) {
$team = "";
foreach($row as $rows){
if($rows['Manager'] != $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
$team = $rows['Manager'];
}
echo "<tr>";
echo "<td>".$rows['Manager']."</td>";
echo "<td>".$rows['Employee_ID']."</td>";
echo "<td>".$rows['Name']."</td>";
echo "</tr>";
}
}

Php undefine variable issue

I've a html form which is insert data to mysql database and then get those data with following php code (From supplier_jv table)
<?php
include("include/address2.php");
include("include/menu.php");
$uname_ad = $_SESSION['uname_ad'];
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM supplier_jv WHERE jv_id = '$id'");
$num = mysql_num_rows($sql);
if($num == 0)
{
echo "<p><font color=red>Accounts is emtpy</font></p>";
}
else
{
$re_name = mysql_fetch_array($sql);
echo "<center><h2>";
echo "<strong>Accounts of </strong>";
echo $re_name['jv_name'];
echo "</h2></center>";
echo "<center>";
echo "<table>";
echo "<table border='0' cellpadding='5' cellspacing='5' width='1000'>";
echo "<tr/>";
echo "<td><strong>Date</strong></td>";
echo "<td><strong>Particular</strong></td>";
echo "<td><strong>Folio(C)</strong></td>";
echo "<td><strong>Folio(J)</strong></td>";
echo "<td><strong>Debit</strong></td>";
echo "<td><strong>Credit</strong></td>";
echo "<td><strong>Balance</strong></td>";
echo "</tr>";
while($re= mysql_fetch_array($sql))
{
$day = $re['day'];
$month $re['month'];
$year = $re['year'];
$parti = $re['particulars'];
$folio = $re['folio'];
$folio2 = $re['folio2'];
$debit = $re['debit'];
$credit = $re['credit'];
$balance = $re['balance'];
$b = $debit - $credit;
$total_debit = mysql_query("SELECT SUM(debit) FROM supplier_jv");
$re_t = mysql_fetch_array($total_debit);
$t_d = $re_t['SUM(debit)'];
$total_credit = mysql_query("SELECT SUM(credit) FROM supplier_jv");
$re_t2 = mysql_fetch_array($total_credit);
$t_c = $re_t2['SUM(credit)'];
$b = $t_d - $t_c;
echo "<tr>";
echo "<td>$day/$month/$year</td>";
echo "<td>$parti</td>";
echo "<td>$folio</td>";
echo "<td>$folio2</td>";
echo "<td>";
echo number_format($debit);
echo "</td>";
echo "<td>";
echo number_format($credit);
echo "</td>";
echo "<td></td>";
echo "</tr>";
}
echo "<tr bgcolor='#f3f3f3'>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td><strong>Total Balance-</strong></td>;
echo "<td><strong>";
echo number_format($b);
echo "</strong></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
}
?>
Well, After insert data then it's show:
Notice: Undefined variable: b in E:\xampp\htdocs\Accounts\admin\content
\supplier_account.php on line 108
But if i insert data in second time then it's OK!!.
Any idea or solution.
Thanks
shibbir.
You need to use isset to see if it is set, not null and also avoid the Notice: Undefined variable error:
if (isset($b))
{
// your code here
}
Where you have:
$b = $t_d - $t_c;
Make sure that there is some value coming up for $t_d and $t_c

Categories