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>
Related
I have a page that gives me info from database.
Date - notes - account_type
In account_type I have 3 types of account PS: A - B - C.
$Qdaily_entriesD = mysqli_query($connect, "SELECT * FROM daily_entries ORDER BY account_type DESC");
while ($showRowGeneral = mysqli_fetch_assoc($Qdaily_entriesD))
{
?>
<tr>
<td><?php echo $showRowGeneral['account_type'];?></td>
<td><?php echo $showRowGeneral['riyal'];?></td>
<td><?php echo $showRowGeneral['dollars'];?></td>
</tr>
<?php
}
I want when I print the values it came out with different urls for each account_type.
Like: accounts.php?type=A , B or C
This is what i have tried
<?php
$Qdaily_entriesD = mysqli_query($connect, "SELECT * FROM daily_entries ORDER BY account_type DESC");
while ($showRowGeneral = mysqli_fetch_assoc($Qdaily_entriesD))
{
if ($showRowGeneral['account_type'] == 'b')
{
?>
<tr>
<td><?php echo $showRowGeneral['account_type'];?></td>
<td><?php echo $showRowGeneral['riyal'];?></td>
<td><?php echo $showRowGeneral['dollars'];?></td>
</tr>
<?php
}
}
?>
<tr><td colspan='3'><a href='?type=A'>A</a>, <a href='?type=B'>B</a> or <a href='?type=C'>C</a></td></tr>
but how to achieve this?
Thanks advanced.
Have you tried to use $_GET ?
Read this for more info https://www.w3schools.com/php/php_forms.asp
In your code you want to use it like so,
if ($showRowGeneral['account_type'] == $_GET['type'])
If you want to have a link for each line :
$Qdaily_entriesD = mysqli_query($connect, "SELECT * FROM daily_entries ORDER BY account_type DESC");
while ($showRowGeneral = mysqli_fetch_assoc($Qdaily_entriesD))
{
?>
<tr>
<td><?echo $showRowGeneral['account_type'];?></td>
<td><?echo $showRowGeneral['riyal'];?></td>
<td><?echo $showRowGeneral['dollars'];?></td>
<td>your text</td>
</tr>
<?
}
I create a table in which I create just one <tr> and 11 <td>. The values of first 9 <td> come from one while loop and last 2 values of <td> come from second while loop.
Both While loops are executing in <tr>. Now, I get the correct values, but it repeats the <td> as the total number of records e.g I have total 5 records, it gives me 25 records and the first record executes 5 times continuously and rest like that.
My question is to execute the record once, not to repeat the duplicate records.
This is my code:
<table id="myTable" class="table table-bordered table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Status</th>
<th>Reg on</th>
<th>Upload</th>
<th>Total Upload</th>
<th>Sale</th>
<th>Total Sale</th>
<th>Purchase</th>
<th>Total Purchase</th>
</tr>
</thead>
<tbody>
<?php
include("connection.php");
$query ="SELECT s.*, s.student_id, s.student_email, u.student_email, b.payer_email,
COUNT(u.student_email) AS 'uploadCount',
SUM(u.price) AS 'uploadTotal',
COUNT(b.payer_email) AS 'buyCount',
SUM(b.payment_amount) AS 'buyTotal'
FROM students s
LEFT JOIN academic_work u ON u.student_email = s.student_email
LEFT JOIN orders b ON b.payer_email = s.student_email
GROUP BY s.student_id
ORDER BY s.student_id DESC
";
$run = mysqli_query($con, $query) or die(mysqli_error($con));
?>
<tr>
<?php
while ($row=mysqli_fetch_array($run)) {
$student_id=$row['0'];
$student_first_name=$row[1];
$student_last_name=$row[2];
$student_email=$row[3];
$student_password=$row[4];
$student_status=$row[5];
$student_time=$row[6];
$uploadCount = $row['uploadCount'];
$buyCount = $row['buyCount'];
$uploadTotal = $row['uploadTotal'];
$buyTotal = $row['buyTotal'];
$query1 ="SELECT ss.*, ss.student_id, ss.student_email, p.email,
COUNT(p.email) AS 'purchaseCount',
SUM(p.payment_amount) AS 'purchaseTotal'
FROM students ss
LEFT JOIN orders p ON p.email = ss.student_email
GROUP BY ss.student_id
";
$run1 = mysqli_query($con, $query1) or die(mysqli_error($con));
while ($row1=mysqli_fetch_array($run1)) {
$purchaseCount = $row1['purchaseCount'];
$purchaseTotal = $row1['purchaseTotal'];
?>
<td><?php echo $student_first_name; ?></td>
<td><?php echo $student_last_name; ?></td>
<td><?php echo $student_email; ?></td>
<td><?php echo $student_status; ?></td>
<td><?php echo $student_time; ?></td>
<td><?php if($uploadCount == 0) { echo 0; } else { echo $uploadCount; }?></td>
<td><?php if($uploadTotal == 0) { echo 0; } else { echo $uploadTotal; }?></td>
<td><?php if($buyCount == 0) { echo 0; } else { echo $buyCount; }?></td>
<td><?php if($buyTotal == 0) { echo 0; } else { echo $buyTotal; }?></td>
<td><?php if($purchaseCount == 0) { echo 0; } else { echo $purchaseCount; }?></td>
<td><?php if($purchaseTotal == 0) { echo 0; } else { echo $purchaseTotal; }?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
Thanks in advance!
Your $query1 loops through all rows of the table on each iteration. Use a where clause and then it will return the 1 row you want. Use the id of $row to target the correct row. You should parameterize this so you aren't open to a SQL injection (https://security.stackexchange.com/questions/146595/second-order-sql-injection-protection).
This also could probably be done in 1 query, looping a query is usually an incorrect implementation.
I want to arrange the table head with my table body, so the data fill have a same type, how can i doing it??
The black square is the correct one
This is my code
<table>
$syntax = 'SELECT customer,type, count(type) as num FROM view_detail GROUP BY type ORDER BY customer';
$rows = $config->prepare($syntax);
$rows->execute();
$result = $rows->fetchAll(PDO::FETCH_OBJ);
?>
</tr>
<td>No</td>
<td>Customer</td>
<?php for($x=0;$x<count($result);$x++){ ?>
<td><?php echo $result[$x]->type; ?></td>
<?php } ?>
<td>Total</td>
<?php $no = 1;
$query = 'SELECT customer,type, count(type) as tipe FROM view_detail GROUP BY customer ORDER BY customer';
$baris = $config->prepare($query);
$baris->execute();
$hasil1 = $baris->fetchAll(PDO::FETCH_OBJ);
for($i=0;$i<count($hasil1);$i++){
$sql = 'SELECT * FROM view_detail WHERE customer = "'.$hasil1[$i]->customer.'" ORDER BY customer';
$row = $config->prepare($sql);
$row->execute();
$hasil = $row->fetchAll(PDO::FETCH_OBJ);
?>
<tr>
<td><?php echo $no; $no++;?></td>
<td><?php echo $hasil[$i]->customer; ?></td>
<?php
for($y=0;$y<count($result);$y++){
if($y < count($hasil)){
?>
<td><?php echo $hasil[$y]->type; ?></td>
<?php }else{ ?>
<td>0</td>
<?php } } }?>
</tr>
</table>
Thanks
I want to make it like this
Edit:
Dynatrade -> MFN200, N50Z, and N70
Super Bloom -> N70, MFN100, MFN120, and MFN150
I am wanting to match my user_id column from my announcements table to the id column in my users table. I then want to get the username from the users table where the id's match.
I initially had the following query
if ($announcements_stmt = $con->prepare("SELECT * FROM announcements"))
I am getting the following error with my current code..
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in
Which I know what this means, but do I need to add in every column table from my users table for this to work or is there another way to do this? If I do need to add all of the columns as variables in my bind_result, does it matter which order I put them in? Announcements first or users or vise versa?
if ($announcements_stmt = $con->prepare("SELECT * FROM announcements
INNER JOIN users
ON announcements.user_id = users.id")) {
$announcements_stmt->execute();
$announcements_stmt->bind_result($announcements_id,
$announcements_user_id, $announcements_messages, $announcements_date);
if (!$announcements_stmt) {
throw new Exception($con->error);
}
$announcements_stmt->store_result();
$announcements_result = array();
?>
Current Announcements
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
</tr>
<?php
while ($row = $announcements_stmt->fetch()) {
?>
<tr>
<td><?php echo $announcements_id; ?></td>
<td><?php echo $announcements_username; ?></td>
<td><?php echo $announcements_messages; ?></td>
<td><?php echo $announcements_date; ?></td>
</tr>
<?php
}
?>
}
update..
if ($announcements_stmt = $con->prepare("SELECT announcements.id, announcements.user_id, announcements.messages, announcements.date, users.username FROM announcements
INNER JOIN users
ON announcements.user_id = users.id")) {
$announcements_stmt->execute();
$announcements_stmt->bind_result($announcements_id,
$announcements_user_id, $announcements_messages, $announcements_date, $announcements_username);
if (!$announcements_stmt) {
throw new Exception($con->error);
}
$announcements_stmt->store_result();
$announcements_result = array();
?>
Current Announcements
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
</tr>
<?php
while ($row = $announcements_stmt->fetch()) {
?>
<tr>
<td><?php echo $announcements_id; ?></td>
<td><?php echo $announcements_username; ?></td>
<td><?php echo $announcements_messages; ?></td>
<td><?php echo $announcements_date; ?></td>
</tr>
<?php
}
?>
}
</table>
<?php
}
}
The warning indicates when you are binding the result fields into variables, the number of variables does not match the number of fields in the result set:
$announcements_stmt->bind_result($announcements_id, $announcements_user_id, $announcements_messages, $announcements_date, $announcements_username);
The easy way around this is to always specify the fields in the SELECT statement (just an example):
SELECT t1.id, t1.user_id, t1.messages, t1.date, t2.username
Instead of:
SELECT *
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