简体   繁体   中英

How can i use values returned from a PHP function in querying and displaying data from a table?

Am attempting to display test results of multiple students in a class. To do this, am using a function find_student_by_year($year) to get a list of all the students which includes the student id. The id is then used in an sql statement to query the result for the specific student id. My code isn't throwing errors but no data is displayed even though the table actually has data. Unfortunately, my research has shown nothing similar so no link is added to my post. See code am working with below

Function

function find_student_by_year($year) {
global $connection;

$query = "select * from students where entry_year = {$year} order by s_fname asc";
$found_students = mysqli_query($connection, $query);
confirm_query($found_students);
return $found_students;
}

PHP and HTML (mixed) code

<tbody>

<?php 
    $staff_id = 27;
    $subject_id = 2;

    //Actually getting this value from $_GET but am hard coding it here
    $year = 4;

    if (isset($year)) { 
        $student_year = find_student_by_year($year); 
    } else {
        $student_year = null;
    } 

    if (isset($student_year)) {
        while ($year_group = mysqli_fetch_assoc($student_year)) { 
?>

<tr>
    <td><input name="student_id[]" value="<?php echo htmlentities($year_group["student_id"]) ?>" readonly></td>
    <td><?php echo $year_group["s_fname"] . " " . $year_group["s_mname"] . " " . $year_group["s_lname"]; ?></td>

    <?php
        $result = mysqli_query($connection, "select * from results where student_id = {$year_group['student_id']} and subject_id = {$subject_id}");
        while($result_record = mysqli_fetch_assoc($result)) {
    ?>

    <td style="width: 15%"><input type="text" name="test1[]" class="form-control" style="text-align: center" 
        value="<?php if ($result_record['test1'] !== ' ') { echo $result_record['test1']; } else {echo ' ';} ?>">
    </td>
    <td style="width: 15%"><input type="text" name="test2[]" class="form-control" style="text-align: center" 
        value="<?php if ($result_record['test2'] !== ' ') { echo $result_record['test2']; } else {echo ' ';} ?>">
    </td>
    <td style="width: 15%"><input type="text" name="test3[]" class="form-control" style="text-align: center" 
        value="<?php if ($result_record['test3'] !== ' ') { echo $result_record['test3']; } else {echo ' ';} ?>">
    </td>
    <td style="width: 15%"><input type="text" class="form-control" style="text-align: center" 
        value="<?php echo htmlentities($result_record['final']); ?>" readonly>
    </td>
</tr>
<?php } } } ?>

</tbody>

See database table and display screenshot

数据库截图

HTML显示

What happen if you try this code instead:

function find_student_by_year($year) {
   global $connection;
    $query = "select * from students where entry_year = '$year' order by 
    s_fname asc";

   $found_students = mysqli_query($connection, $query);

   confirm_query($found_students);    
   return $found_students;

}

Created a view using a join statement on students and records table using the student_id column. Then queried the view and displayed each student's result using the student_id. See code extract below

<?php
$subject_id = 2;
if (isset($_GET["year"])) {
    $result = mysqli_query($connection, "select * from fullresult where year = {$_GET["year"]} and subject_id = {$subject_id}");
    while($student_result = mysqli_fetch_assoc($result)) {
?>
<tr>
<td style="display: none"><input name="student_id[]" value="<?php echo htmlentities($student_result["student_id"]) ?>"></td>
<td><?php echo $student_result["firstname"] . " " . $student_result["middlename"] . " " . $student_result["lastname"]; ?></td>
<td style="width: 15%; text-align: center">
    <?php 
        if ($student_result['test1'] !== ' ') {
            echo $student_result['test1']; } else {
                echo '<input type="text" name="test1[]" class="form-control" style="text-align: center" value="">';
            }
    ?>
</td>
<td style="width: 15%; text-align: center">
    <?php 
        if ($student_result['test2'] !== ' ') {
            echo $student_result['test2']; } else {
                echo '<input type="text" name="test2[]" class="form-control" style="text-align: center" value="">';
            }
    ?>
</td>
<td style="width: 15%; text-align: center">
    <?php 
        if ($student_result['test3'] !== ' ') {
            echo $student_result['test3']; } else {
                echo '<input type="text" name="test3[]" class="form-control" style="text-align: center" value="">';
            }
    ?>
</td>
<td style="width: 15%; text-align: center"><?php echo htmlentities($student_result['total']); ?></td>
</tr>
<?php 
  } 
} 
?>

学生表结构

记录表结构

结果查看表结构

前端显示屏

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM