2016-05-04 9 views
1

クラス内の複数の生徒のテスト結果を表示しようとしています。これを行うには、関数find_student_by_year($ year)を使用して、学生IDを含むすべての生徒のリストを取得しています。 IDは、特定の学生IDの結果を照会するためにSQLステートメントで使用されます。私のコードはエラーを投げていませんが、テーブルに実際にデータがあってもデータは表示されません。残念ながら、私の研究は何も似ていないので、私の投稿にリンクは追加されていません。コードはDatabase screenshotPHP関数から返された値をクエリとテーブルのデータ表示に使用するにはどうすればよいですか?

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やHTML(混合)コード

<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> 

参照データベースのテーブルやディスプレイスクリーンショット機能

以下

HTML display

で働いています参照してください。
+0

'$結果= mysqli_query($接続、「year_group STUDENT_ID = {$結果SELECT * FROM以下のコード抽出を見ます["student_id"]}とsubject_id = {$ subject_id} "); ' - " $ year_gro '$ year_group [student_id ']' –

+0

また、ネストしたwhileループの中括弧はありません: 'while($ result_record = mysqli_fetch_assoc($ result))' ** {* student_id " * –

+0

そのようなループでクエリを実行するのは一般的に貧弱な設計です。 2つのテーブルを結合する1つのクエリを使用します。 – Barmar

答えて

0

あなたの代わりにこのコードをしようとした場合、何が起こる:

STUDENT_ID列を使用して、学生やレコードをテーブルの上に結合文を使用してビューを作成し
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; 

} 
+0

コードは学生IDと名前を意図して生成します。表示画像を見る – Mena

0

を。次に、ビューを照会し、student_idを使用して各生徒の結果を表示しました。

<?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 
    } 
} 
?> 

Students table structure

Records table structure

Results view table structure

Front end display

関連する問題