dynamically generate HTML table from MySQL - php

I have three mysql tables subjects, examinations, examinfo
TABLE - SUBJECTS
subjectid
subjectname
subjectExamid
TABLE - EXAMINFO
examid
exam
TABLE EXAMINATIONS
fname
lname
studentid
score
subjectid
ON subject.subjectExamid = exam.examid
ON examination.subjectid = subject.subjectid
Now i would like to generate HTML table indicating scores students get per paper against subject
STRUCTURE TABLE OUTPUT
Student details against each subject score
EDIT CODE SAMPLE
<?php
$examinid = 3;
$subjects = mysqli_query(
$con,"
SELECT * FROM subjects
WHERE examid = '$examinid'
ORDER BY shortname ASC
");
$content = mysqli_query(
$con,"
SELECT DISTINCT exam.idcandidate, exam.sex, exam.fname, exam.lname
FROM examinations
AS exam
INNER JOIN examinfo
AS info
ON exam.id_subject = info.idsubject
WHERE info.idexam = '$examinid'
");
?>
<div id="table_1">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="table1tr">#</td>
<td class="table1tr">Candidate</td>
<td class="table1tr">ID</td>
<td class="table1tr">Sex</td>
<?php
// output subjects
while($subRow = mysqli_fetch_array($subjects)){
$arbv = strtoupper($subRow['shortname']);
$subjectname = ucwords(strtolower($subRow['subjectname']." - ".$subRow['subjectid'].""));
?>
<td class="table1tr" title="<?php echo $subjectname; ?>">
<?php echo $arbv; ?>
</td>
<?php
}
?>
<td class="table1tr">Exam</td>
</tr>
<?php
while($stdnt = mysqli_fetch_array($content)){
$fullname = ucwords(strtolower("$stdnt[lname] $stdnt[fname]"));
$studentid = str_replace(array('/', 'M', 'W', 'S', 'F', '-'), "",$stdnt['idcandidate']);
if($sex = $stdnt['sex'] == Male){
$sex = M;
}else{ $sex = F; }
$id_subject = $stdnt['id_subject'];
$x++;
$zebra_1 = ($x%2)? 'TableZebra_1': 'TableZebra_2';
?>
<tr>
<td class="<?Php echo $zebra_1; ?>"><?php echo $count++; ?></td>
<td class="<?Php echo $zebra_1; ?>"><?Php echo $fullname; ?></td>
<td class="<?Php echo $zebra_1; ?>"><?php echo $studentid; ?></td>
<td class="<?Php echo $zebra_1; ?>"><?php echo $sex; ?></td>
<td class="<?Php echo $zebra_1; ?>">
<!-- Problem is here how to output the subject grades $grade -->
<!--
My first unsuccessful approach
SELECT score
FROM examinations AS test
INNER JOIN examinfo AS testinfo ON testinfo.idsubject = test.id_subject
WHERE testinfo.idexam
IN (
SELECT idexam
FROM examinfo
WHERE idexam = $examinid
)
AND test.id_subject = $id_subject AND test.idcandidate = '$studentid'
Then output results - But this falls it shows one student subjects in one cell
-->
</td>
<td class="<?Php echo $zebra_1; ?>">Exam</td>
</tr>
<?php
} // loop content
?>
</table>
</div>

If your solution is not concat(); you may first
follow the simple steps
1 loop $contents // to get info such as studentid
2 inside the loop of $contents loop $subjects // to get all subjects including subjectids
3 inside $subject loop, loop examinations table where studentid = '$studentid' AND subjectid = '$subjectid'
if step three return null echo empty cell otherwise echo cell with score
I have no time to test this, but you may follow the steps and it will work otherwise try google search

Related

not able to get the data and display on web using php

$totDays = cal_days_in_month(CAL_GREGORIAN,$_REQUEST['month'],$_REQUEST['year']);
$attData = $commonObj->getAttendanceData($_REQUEST['month'],$_REQUEST['year']);
`
Student Name
<?php foreach($attData as $attk=>$attv){
$punchin = $commonObj->getTimeOfDate($attData[$attk]['punchin']);
$punchout = $commonObj->getTimeOfDate($attData[$attk]['punchout']);
$nam=$attv['name'];
?>
<tr>
<th class="danger">
<?php echo $nam;?>
</th>
<?php for($i=1;$i<=$totDays;$i++){?>
<?php if($commonObj->getDayOfDate($attData[$attk]['punch_time']) == $i){
echo "<td class='success' id='att_$i'>".$punchin.'-'.$punchout;?>
<table class="table table-responsive"style="display: none; position:relative;min-width:100px;max-width:200px; margin-top: -40px;" id="<?php echo "det_att_".$i;?>">
<tr>
<td>Time:</td>
<td><?php echo "p"?>
</td></tr>
<tr>
<td>UID:</td>
<td><?php echo $attData[$attk]['rfid_uid'];?></td>
</tr>
</table>
<?php
}else {echo "<td class='info'>#na";}?>
</td>
<?php }?>
</tr>
<?php }?>
</tr>
</table>`
this is the code for displaying on the web for viewing attendance ..
and here is the code for database part for getting attendance...
public function getAttendanceData($month,$year){
if(!is_numeric($month) || $month>12 || $month<1){
$this->setErrorMsg("Please Select valid month!");
header("location:viewAttendance.php");
exit;
}
$retData = array();
$fullDate = $year."-".$month;
//print_r($_SESSION);
if($_SESSION['user_type']==1){
$sql = "
SELECT t1.*
, t2.rfid_uid
, punch_time as punchin
, t2.name
FROM tbl_attendance as t1
right
join tbl_users as t2
on t1.rfid_uid = t2.rfid_uid
WHERE YEAR(punch_time) = $year
AND MONTH(punch_time) = $month
and t2.rfid_uid ='".$_SESSION['rfid_uid']."'
";
$retData = $this->getData($sql);
}else if($_SESSION['user_type'] == 2){
$sql = "SELECT t1.*,t2.rfid_uid,punch_time as punchin,t2.name FROM tbl_attendance as t1 right join tbl_users as t2 on t1.rfid_uid=t2.rfid_uid WHERE YEAR(punch_time) = $year AND MONTH(punch_time) = $month group by t2.rfid_uid";
$retData = $this->getData($sql);
}
return $retData;
}
web viewing imagedatabase table for attendanceuser
the attendance is recorded in the database but is not shown on the web..please help

How to fetch and show these data in php mysql?

I have question table and subject table and question table contain subject wise questions for online examination.
I need to fetch subject wise questions with subject name as header and show all the questions in subject wise serial number such as example: Maths: Q1, Q2, Q3
English: Q1, Q2, Q3 and so on.
How to achieve it in php and mysql. The question table and subject table are given below.
Question sample data are given below
<?php
require_once 'config.php';
//$con = mysqli_connect("localhost","root","","database_name");
$query1 = "SELECT q.q_id,q.setq_no, q.qtext_eng, s.sub_id, s.sub_name
FROM question q
INNER JOIN subject s ON s.sub_id = q.sub_id
INNER JOIN questionset qs ON qs.qset_id = q.qset_id
WHERE qs.qset_id =2 ORDER BY s.sub_id";
?>
<table class="table table-bordered">
<thead>
<tr>
<th>Q.No</th>
<th>Q Set number</th>
<th>Q text eng</th>
</tr>
<?php
$result1 = mysqli_query($link,$query1);
while($row1 = mysqli_fetch_array($result1))
{
$subID = $row1['sub_id'];
$subName = $row1['sub_name'];
?>
<h2><?php echo "$subName" ?></h2>
<?php
error_reporting(0);
$sno++;
$qSet = $row1['setq_no'];
$qEng = $row1['qtext_eng'];
?>
<tr>
<td><?php echo $sno; ?></td>
<td><?php echo $qSet; ?></td>
<td><?php echo $qEng; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
I am including some of your columns from question table here , you can add the rest same way
<?php
$con = mysqli_connect("localhost","root","","database_name");
$query1 = "SELECT q.q_id,q.setq_no, q.qtext_eng, s.sub_id, s.sub_name
FROM question q
INNER JOIN subject s ON s.sub_id = q.sub_id
INNER JOIN questionset qs ON qs.qset_id = q.qset_id
WHERE qs.qset_id =2 ORDER BY s.sub_id";
$presubID = 0;
<table class="table table-bordered">
while($row1 = mysqli_fetch_array($result1))
{
$subID = $row1['sub_id'];
if($subID != $presubID){
$subName = $row1['sub_name'];
<h2><?php echo "$subName" ?></h2>
$sno=0;
<thead>
<tr>
<th>Q.No</th>
<th>Q Set</th>
<th>Q text eng</th>
</tr>
</thead>
}
$presubID = $subID;
$sno++;
$qSet = $row1['setq_no'];
$qEng = $row1['qtext_eng'];
<tr>
<td><?php echo $sno; ?></td>
<td><?php echo $qSet; ?></td>
<td><?php echo $qEng; ?></td>
</tr>
<?php
}
?>
</table>

Query to find member name and score

I have two tables one for members having fields as id , name
Second table as results with fields as rid , mid (member id foreign key), pid , score
I need to show name of member and their score where pid=$id
My query is :
$sel = mysql_query("SELECT m.name,r.score FROM member m, score r WHERE m.id=r.mid and r.pid='$id'");
<?php $i=0; while( $sql = mysql_fetch_assoc( $sel )) { ?>
<tr>
<td align="center"><?php $i++; echo $i; ?> </td>
<td align="center"><?php echo ucfirst( $sql['name'] ); ?></td>
<td align="center"><?php echo $sql['score']; ?></td>
</tr>
<?php } ?>
But no records are getting output.
There are matching records but it is not entering in while loop.
Use as:
$sel = mysql_query("SELECT m.name as name, r.score as score FROM member m, score r WHERE m.id=r.mid and r.pid='$id'");
Or so, if you do not want to use as:
<?php echo ucfirst( $sql['m.name'] ); ?>

Generate an id in the td of a row, from the Mysql DB

I have a table where I need to fill an ID in a td using the value retrieved from a Mysql Table. I tried like this:
<?php
$ww = db_query("SELECT id, item1 FROM table1 WHERE table1.id = '".$record->main_data."'" );
?>
<td id="id_field1" class="<?php echo $ww->id;?>" >
<?php
foreach($ww as $ee)
{ echo $ee->item1;}
?>
</td>
I know the use of:
<td id="id_field1" class="<?php echo $ww->id;?>" >
is wrong, but I am not getting any other idea can you help me to generate an id for this td from the id retrieved from the Query?
EDIT
After the use of:
$ww = db_query("SELECT id, item1, FROM table1 WHERE table1.id = '" . $record->main_data . "'");
foreach ($ww as $row) {
?>
<td id="id_field<?php echo $row->id; ?>" class="<?php echo $row->id; ?>" >
<?php echo $row->item1; ?>
</td>
<?php
}
I am getting two issues--
1 ) Getting a warning from the DATATABLES plugin for fixedheader which says
Requesetd unknown parameter '11' for row 0
2 ) For empty td , the next <td> gets moved , instead of first <td> and I am getting the second <td> values of class and id inside the first... In this case, the table row is shifted to left with empty in the last column td
EDIT2 : this worked with no issues
I tried like this - is it ok to do like this: please comment on it-
<?php $ww = db_query("SELECT id FROM table1 WHERE table1.id = '".$record->main_data."'" );?>
<td id="id_field1" class="cart_wonid<?php foreach($ww as $ee) { echo $ee->id;}?>">
<?php
$ww = db_query("SELECT item1 FROM table1 WHERE table1.id = '".$record->main_data."'" );
foreach($ww as $ee){
echo $ee->main_data;
}
?>
</td>
EDIT
OP using drupal.
Put your td into the loop.
$ww = db_query("SELECT id, item1, FROM table1 WHERE table1.id = '" . $record->main_data . "'");
foreach ($ww as $row) {
?>
<td id="id_field<?php echo $row->id; ?>" class="<?php echo $row->id; ?>" >
<?php echo $row->item1; ?>
</td>
<?php
}

grouping class names together in my CMS php

I am done with a CMS school system which i created from scratch for practice in php. My question is for example I have Accounting 101, Computer Science 101, however there must multiple times for Accounting 101. For example: Ticket 1035, 1036 are both Accounting 101 and they should appear in the same table, but in my code it shows them in different classes. Here is my code.
if(isset($_GET['id']))
{
$category = $_GET['id'];
$sql = "SELECT * FROM classes WHERE category_id = " . $category;
$query2 = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($query2))
{
?>
<center><h3><?php echo $row->class_name . '-' . $row->units; ?> </h3></center>
<table border ="0" wdith="100%">
<tr>
<td>
<strong>Description: </strong>
<?php echo $row->class_description; ?>
</tr>
</td>
</table>
<br/>
<table border="1" width="44%">
<tr>
<td width="60"><b>Ticket</b> </td>
<td width="123"><b>Days</b></td>
<td width="120"><b>Hours</b></td>
<td width="64"><b>Room</b></td>
<td><b>Instructor</b></td>
</tr>
<tr>
<td width="60"> <?php echo $row->ticket; ?> </td>
<td width="123"><?php echo $row->days; ?></td>
<td width="120"><?php echo $row->start_hours . $row->time_format . '-' . $row->end_hours . $row->time_format2 ; ?> </td>
<td width="64"> <?php echo $row->room_number; ?></td>
<td><?php echo $row->instructor_name; ?></td>
</tr>
}//end while
}//end if
Its showing Accounting 101 with different tickets in different tables, but it should be all in 1 table. Thanks.
You need a double loop if you're trying to get records inside of records. For example:
while ($row1 = mysql_fetch_object($query1))
{
echo $row1->ParentName;
$query2 = 'select * from `mytable` where `myForeignKey` = ' . $row1->ParentId;
while ($row2 = mysql_fetch_object($query2))
{
echo $row2->ChildName;
}
}
You could also do a left join. Let me know if you need a sample of that.
Edit:
The left join would be done like this:
$sql = "select * from `classes` as a where category_id = '{$category}' left join `tickets` as b on a.id = b.class_id"
Ref. http://www.w3schools.com/sql/sql_join_left.asp

Categories