Change td content according to database - php

I have two table for assignments. TABLE1 has the data of the assignments posted by a faculty and TABLE2 has data of those students who submitted the assignments. Both table have one similar column called 'assignment name'.
My TABLE2 has a status column which is boolean data. By default i have set it to 0 and if a student submit his assignment that changes to 1.
In my HTML page (student side) I'm displaying the data from TABLE1 in a table. It shows all assignments name and I have given a submit status column there which will show if the assignment work is submitted by the logged in student. If yes it will show a check icon if not then a times icon.
This is what i have tried:
<?php
session_start();
include'connection.php';
$user=$_SESSION['details1'];
$query =mysqli_query($con,"SELECT course FROM student WHERE username='$user'");
while($row=mysqli_fetch_array($query))
{
$cr=$row['course'];
}
$query1 =mysqli_query($con,"SELECT semester FROM student WHERE username='$user'");
while($row1=mysqli_fetch_array($query1))
{
$sems=$row1['semester'];
}
$res=mysqli_query($con,"SELECT status FROM submitted_assignments WHERE s_name='$user'");
while($row2=mysqli_fetch_array($res))
{
$stat=$row2['status'];
}
?>
<table class="student" id= "student">
<form action="" method="post">
<thead>
<tr class="table100-head">
<th class="column0">Sl. No.</th>
<th class="column2">Assignment Name</th>
<th class="column2">Subject</th>
<th class="column1">Date</th>
<th class="column0">View</th>
<th class="column0">Status</th>
</tr>
</thead>
<tbody>
<?php
$shcat=mysqli_query($con,"SELECT * FROM assignment INNER JOIN subject ON assignment.as_subject=subject.s_id WHERE s_course='$cr' AND a_semester='$sems'");
$cnt=1;
while($row=mysqli_fetch_array($shcat)){
$stat = 0;
$res=mysqli_query($con,"SELECT status FROM submitted_assignments WHERE s_name='$user' AND a_name=".$row['a_id']);
while($row2=mysqli_fetch_array($res))
{
$stat=$row2['status'];
}
?>
<tr>
<td class="column0"><?php echo $cnt; ?></td>
<td class="column1"><?php echo $row['a_name'];?></td>
<td class="column2"><?php echo $row['s_name'];?></td>
<td class="column2"><?php echo $row['date'];?></td>
<td class="column0"><a target = "_blank" href="../faculty/assignment/<?php echo $row['image']?>"><i class="fa fa-eye"></i></a></td>
<?php
if ($stat == 0){ ?>
<td class="column0"><i class="fa fa-times" title="Not Submitted"></i></td>
<?php }
else {?>
<td class="column0"><i class="fa fa-check" title="Submitted"></i></td>
<?php }?>
</tr>
<?php $cnt=$cnt+1;
} ?>
</tbody>
</form>
</table>
From what i have tried it changes all the rows to check icon even if only one row's assignment is submitted.
Any help is much appreciated. Thank you.
Edit Its working now.

Your code to set $stat
$res=mysqli_query($con,"SELECT status FROM submitted_assignments WHERE s_name='$user'");
while($row2=mysqli_fetch_array($res))
{
$stat=$row2['status'];
}
is just going to override the value of $stat leaving the last rows value in its place.
The simple way to fix this will be to move this sql into the PHP for loop and check for the specific assignment+user combination. Without your database scheme im not sure the files used but something like:
<?php
$shcat=mysqli_query($con,"SELECT * FROM assignment INNER JOIN subject ON assignment.as_subject=subject.s_id WHERE s_course='$cr' and a_semester='$sems'");
$cnt=1;
while($row=mysqli_fetch_array($shcat)){
$stat = 0;
$res=mysqli_query($con,"SELECT status FROM submitted_assignments WHERE s_name='$user' AND a_name=".$row['a_name']);
while($row2=mysqli_fetch_array($res))
{
$stat=$row2['status'];
}
?>
<tr>
<td class="column0"><?php echo $cnt; ?></td>
<td class="column1"><?php echo $row['a_name'];?></td>
<td class="column2"><?php echo $row['s_name'];?></td>
<td class="column2"><?php echo $row['date'];?></td>
<td class="column0"><a target = "_blank" href="../faculty/assignment/<?php echo $row['image']?>"><i class="fa fa-eye"></i></a></td>
<?php
if ($stat == 0){ ?>
<td class="column0"><i class="fa fa-times" title="Not Submitted"></i></td>
<?php }
else {?>
<td class="column0"><i class="fa fa-check" title="Submitted"></i></td>
<?php }?>
</tr>
<?php $cnt=$cnt+1;
} ?>

Related

How do I make a button within an HTML table, to update info in my database

I am totally new to coding. That being said I hope you can help me with this issue I am having.
I use PHP and Mysqli to populate information within a html table. I have added a button in one of the columns. I want to click the button and update the information on that row of the table, in the database.
The users earn a commission, so by clicking the button, I want to move the amount in the "pay" column to the "paid" column and update the database.
When I click the button, I get the "success" message that says "Payment Processed Successfully", but nothing changes in the database!
Thanks ahead of time for any help you can provide.
Here's my code:
<html>
<body>
<div class="form">
<h2>View Records</h2>
<table width='100%'>
<thead>
<tr>
<th><strong>user ID</strong></th>
<th><strong>Fist Name</strong></th>
<th><strong>Last Name</strong></th>
<th><strong>pay commission</strong></th>
<th><strong>Paid</strong></th>
<th><strong>Pay Commission</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
<?php
$count=1;
include "db.php";
$sql = "SELECT * FROM users ";
$result = $conn-> query($sql);
if($result -> num_rows > 0){
while ($row = $result-> fetch_assoc()) { ?>
<td align="center"><?php echo $row["id"]; ?></td>
<td align="center"><?php echo $row["first_name"]; ?></td>
<td align="center"><?php echo $row["last_name"]; ?></td>
<td align="center"><?php echo $row["pay"]; ?></td>
<td align="center"><?php echo $row["paid"]; ?></td>
<td align="center">
<div class="input-group">
<form class="reset-form" action="members.php" method="post" >
<div class="input-group">
<button type="submit" class="button_1" name="pay-commission">Pay Commission</button></div></form>
</td>
<td align="center">
Delete
</td>
</tr>
<?php
}
// process commission*******
if(isset($_POST["pay-commission"])){
$id=$row['id'];
$pay=$row['pay'];
$paid=$row['last_paid'];
//date and time of transaction
$trn_date = date("Y-m-d H:i:s");
require('db.php');
$ins_query="update users last_paid='$pay' trm_date='$trn_date' where id= '$id'";
mysqli_query($conn,$ins_query);
if($ins_query){
echo "<p class= 'success'> Payment Processed Successfully <p>";
}else{
echo"<p class= 'error'>something went wrong!!</p>";
}
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
got it working except it updates all users with one click on any button in the "pay commission" column. may not be a bad thing to process all commissions with one click.
Maybe I move the button below the table, to update all users with one click. all of this code is on a page called members.php
<html>
<head></head> <header></header>
<body>
//table structure
<div class="form">
<h2>View Records</h2>
<table width='100%'>
<thead>
<tr>
<th><strong>user ID</strong></th>
<th><strong>Fist Name</strong></th>
<th><strong>Last Name</strong></th>
<th><strong>pay commission</strong></th>
<th><strong>Paid</strong></th>
<th><strong>Pay Commission</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
//populate table with user data
<?php
$count=1;
include "db.php";
$sql = "SELECT * FROM users ";
$result = $conn-> query($sql);
if($result -> num_rows > 0){
while ($row = $result-> fetch_assoc()) { ?>
<td align="center"><?php echo $row["id"]; ?></td>
<td align="center"><?php echo $row["first_name"]; ?></td>
<td align="center"><?php echo $row["last_name"]; ?></td>
<td align="center"><?php echo $row["pay"]; ?></td>
<td align="center"><?php echo $row["last_paid"]; ?></td>
<td align="center">
<div class="input-group">
<form action="members.php" method="post" >
<div class="input-group">
<button type="submit" class="button_1" name="pay-commission">Pay commission</button></div></form>
</td>
<td align="center">
Delete
</td>
</tr>
<?php
// process commission*******
if(isset($_POST["pay-commission"])){
$id=$row['id'];
$pay=$row['pay'];
$paid=$row['last_paid'];
//set the date and time of transaction
$trn_date = date('Y-m-d');
$conn->query("UPDATE users SET last_paid='$pay' WHERE id= '$id'");
$conn->query("UPDATE users SET pay=0.00 WHERE id= '$id'");
$conn->query("UPDATE users SET trn_date='$trn_date' WHERE id= '$id'");
if($conn){
//refresh the page to see changes
header ('Location: members.php');
echo '<p class="success"> A Payment of $$pay Processed Successfully for member ID $id</p>';
}else{
echo '<p class="error">Something went wrong! </p>';
}
}
}
}
$conn->close();
?>
</tbody>
</table>
</div>
</body>
</html>

How to select a name using foreign key, and present it on the table

I´m creating a school project and in this part I need to present the name of the person associated with this "n_processo" (id).
I have my tablets divided on the database like the printscreen, and I don´t know how to present the name of the person using the foreign key.
Turma Table:
I´m using a functions file, that have the function DBRead12 (that is selecting my "turma" table).
I have the user_especial table that have the names and the stranger key, like on this print.
User_Especial Table:
function DBRead12()
{
$sql="SELECT * FROM turma";
$result=DBExecute($sql);
while($res=mysqli_fetch_assoc($result))
{
$data[]=$res;
}
return $data;
}
<button class="collapsible">Consulta de Turmas</button>
<div class="content">
<br>
<div class="container" align="left">
<div class="well" align="left" style="width:70%">
<?php $admin = DBRead12()?>
<table id="example" class="table table-striped table-bordered" style="width:90%">
<thead>
<tr>
<th>Designação</th>
<th>Tipo</th>
<th>Diretor de Turma</th>
<th>Ano Letivo</th>
</tr>
</thead>
<tbody>
<?php foreach($admin as $cl)
{?>
<tr>
<td align=center><?php echo ($cl['designacao']) ?></td>
<td align=center><?php echo ($cl['tipo']) ?></td>
<td align=center><?php echo ($cl['diretor_turma']) ?></td>
<td align=center><?php echo ($cl['ano_letivo']) ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
In your query, you would use a join statement in order to get data from both tables at once
Example:
$sql="SELECT user_especial.nome, turma.*
FROM turma
LEFT JOIN user_especial ON user_especial.n_processo = turma.director_turma";
$sql="SELECT user_especial.nome as nome, turma.*
FROM turma
LEFT JOIN user_especial ON user_especial.n_processo = turma.director_turma";
This will give you all data from Turma Table and nome from User_Especial Table as nome.
$sql="SELECT user_especial.nome as nome, turma.*
FROM turma
LEFT JOIN user_especial ON user_especial.n_processo = turma.director_turma";
Also you must cut $cl['diretor_turma'] and add $cl['nome'].
So, your tbody will be:
<td align=center><?php echo ($cl['designacao']) ?></td>
<td align=center><?php echo ($cl['tipo']) ?></td>
<td align=center><?php echo ($cl['nome']) ?></td>
<td align=center><?php echo ($cl['ano_letivo']) ?>
</td>

PHP Notification Page - Table not displaying Results

I've created a notification script and I have a page called notificationCenter.php where the admin user can view ALL the notifications. It will display which users have read which notifications, when it was posted and who by. Everything works fine with it, the only issue is when I go onto the notificationCenter.php page to view all notifications, the table doesn't display the results. However I run an if statement to check if there are any results and I don't get any errors. Here's my code, I'm sure I've done something stupid but I can't see what it is, I'd really appreciate any help!.
//Get User ID
$id = $_SESSION['user_id'];
//Select * Notifications
$notQuery = $serviceConn->query("SELECT * FROM db759709251.notifications WHERE `not_viewedby` NOT LIKE '%$id%' ");
<h4>Nofication Center</h4>
<?php if($notQuery->rowCount()) { ?>
<table>
<thead>
<tr>
<th scope="col">Status</th>
<th scope="col">User</th>
<th scope="col">Notification</th>
<th scope="col">Date</th>
<th scope="col">Viewed By</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php
while ($row = $notQuery->fetch()) {
$notid = $row['not_id'];
$notUser = $row['not_user'];
$notMsg = $row['not_msg'];
$notStatus = $row['not_status'];
$notDate = $row['not_date'];
$notViewedby = $row['not_viewedby'];
?>
<tr>
<td style="background-color: <?php echo $statusColour; ?>" data-label="Status"><?php echo $notStatus; ?></td>
<td data-label="User"><?php echo $notUser; ?></td>
<td data-label="Notification"><?php echo $notMsg; ?></td>
<td data-label="Date"><?php echo $notDate; ?></td>
<td data-label="Viewed By"><?php echo $notViewedby; ?></td>
<td data-label="">
<form action="" method="POST">
<input type="hidden" name="notificationID" value="<?php echo $notid; ?>" >
<input type="hidden" name="notificationBy" value="<?php echo $notViewedby; ?>">
<input type="submit" name="read" value="Dismiss!">
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php } else { ?>
<p>You currently have no notifications, please check back later.</p>
<?php } ?>
Now obviously there is a lot of HTML in the middle for styling and layouts, but I didn't think it was important enough as it shouldn't have any effect on the output of the table. But as you can see the if statement SHOULD print that there are no notifications and to check back later, and it doesn't so in theory there should be records to display.

Why is my delete operation not performing?

In my project, I want a delete operation for a particular id when I click on the delete button, but it isn't performing. Here's my code for displaying the table:
<table class="listing" cellpadding="0" cellspacing="0">
<tr>
<th class="first"><center>Id</center></th>
<th>Category Name</th>
<th>Status</th>
<th>Edit</th>
<th class="last">Delete</th>
</tr>
<?php
include('config.php');
$sql="select * from category_tbl";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result)) {
if($row['cat_status'] == 0) {
$im='<img src="../images/red.jpg" height="28" width="28">';
}
else{
$im='<img src="../images/green.jpg" height="30" width="30">';
}
if (isset($_REQUEST['false'])) {
$updt=mysql_query("update category_tbl set cat_status=1 where cat_id='".$_REQUEST['false']."'");
header('location:category.php');
}
if (isset($_REQUEST['true'])) {
$updt=mysql_query("update category_tbl set cat_status=0 where cat_id='".$_REQUEST['true']."'");
header('location:category.php');
}
?>
<tr>
<td><strong><?php echo $row['cat_id'];?></strong></td>
<td><strong><?php echo $row['cat_name'];?></strong></td>
<td><?php echo $im;?></td>
<td><img src="../images/edit.jpg" height="30" width="60" value=<?php echo $row['cat_id'];?>></td>
<td><img src="../images/delete1.jpg" height="30" width="60" value=<?php echo $row['cat_id'];?>></td>
</tr>
<?php
}
?>
</table>
The edit operation is working, but delete is not.
This is my code for delete:
<?php
include("config.php");
$id=$_REQUEST['id'];
$sql=mysql_query("DELETE FROM category_tbl WHERE cat_id='".$id."'");
if ($sql) {
header("location:category.php");
}
?>
After execution it stays on delete_cat.php?id=(passed id)....
i think you need to remove your foreign key from sub table or second option is you need to alter your query and set cascade like this.
ON DELETE CASCADE
for more help visit this link

PHP - product comparison table displaying only one column

I am trying to compare products and display results in a table. I have a form page and a TABLE page, the problem is that when i select in the FORM page from the two drop-downs the same product(value) it will only display in the table one column and returns an multidimensional array containing one array not two arrays identical. When i select two different values from the from and submit, both selections are displayed. How can I avoid having one empty column if the user select the same product /ID ?
I was thinking maybe load the second drop down and subtracting from the list the value of the first drop down selection? I am not sure how to do it Or maybe there is an easier way like a form validation. Can someone show me how? Thanks
FORM.html
<?php
$products_dropdown = "select * from product_dp" ;
$statement = $dbh->prepare($products_dropdown);
$statement->execute();
$result_dp = $statement->fetchAll();?>
<form action="table.html" method="post">
<fieldset class="custimg">
<legend>Product Comparison</legend>
<div class="row">
<div class="large-block-grid-4"><br /></div>
<div class="large-block-grid-4">
<select name="id1" id="id1" class="small button secondary dropdown radius">`
<?php foreach ($result_dp as $row){
echo "<option value='".$row['id']."'>".$row['sku']."</option>";}?>
</select>
</div>
<div class="large-block-grid-4">
<select name="id2" id="id2" class="small button secondary dropdown radius">
<?php foreach ($result_dp as $row){
echo "<option value='".$row['id']."'>".$row['sku']."</option>"; }?>
</select>
<input type="submit" value="Compare" class="tiny button secondary" />
</div>
</div>
Table.html
$id1 = $_POST[id1];
$id2 = $_POST[id2];
$statement = $dbh->prepare("select * from products_specs where id IN ($id1,$id2)");
$statement->execute(array(':id1' => $id1, ':id2'=> $id2));
$statement->setFetchMode(PDO::FETCH_ASSOC);
$subrows = $statement->fetchAll();
print_r($subrows);
$yes = '<img src="images/icons/yes.png" width="16" height="16" />';
$no = '<img src="images/icons/no.png" width="16" height="16" />';?>
<table >
<thead>
<tr>
<th width="100"></th>
<th width="275"><?php echo $subrows[0][image]; ?></th>
<th width="275"><?php echo $subrows[1][image]; ?></th>
</tr>
<tr text-align="center">
<th width="100"></th>
<th class="centered-cell1"><font color="#990000"><?php echo "ID"." ".$subrows[0][id]; ?></th>
<th class="centered-cell1"><font color="#990000"><?php echo "ID"." ".$subrows[1][id]; ?></th>
</tr>
</thead>
<tbody>
<tr>
<td > Capacity </td>
<td align="center"><?php echo $subrows[0][capacity]; ?> </td>
<td align="center"><?php echo $subrows[1][capacity]; ?> </td>
</tr>
<tr>
<td > Memory </td>
<td align="center"><?php echo $subrows[0][mem]; ?> </td>
<td align="center"><?php echo $subrows[1][mem]; ?> </td>
</tr>
ect....
Very simple solution. After executing query use code:
if (!isset($subrows[1]))
$subrows[1] = $subrows[0];
Let users compare a product with itself if they want it.

Categories