Skip to content
Advertisement

Change td content according to database

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.

Advertisement

Answer

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; 
                    } ?>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement