2017-08-09 9 views
0

以下のクエリから配列$code$per_sectionの値を繰り返したいとします。どのようなループメソッドを使用する必要がありますか?それともこれは正しいことですか?クエリの配列の値を反復する方法は?

//total students 
    $count_query=mysql_query("select total_students from subject where teacherid = '$get_id'"); 
    while($count_row=mysql_fetch_array($count_query)){ 
    $total += $count_row['total_students']; 
    } 

    $query = mysql_query("Select code,total_students from subject where teacherid='$get_id'"); 
     while($result=mysql_fetch_assoc($query))){ 
     $section = ($result['total_students']/ $total)*30; 
     $per_section[] = round($section, 0, PHP_ROUND_HALF_UP); 
     $code[] = $result['code']; 
     } 

    //perform this statement for a number of times depending on the number of array of $code.. 
     $user_query=mysql_query("select * from result where subject_id ='$code' and faculty_id = '$get_id'LIMIT $per_section ")or die(mysql_error());     
     while($row=mysql_fetch_assoc($user_query)){ 
      ... 
     } 

サンプルデータ: 件名テーブル

code total_students teacherid 

IT230   45   11-0009 
IT213   44   11-0009 
IT214   40   11-0009 

結果テーブル

subject__id faculty_id 

IT230   11-0009 
IT213   11-0009 
IT214   11-0009 

期待される結果: 私は、結果表に一致する学生のみ30レコードを表示します。

その教師の総学生は45、44であるので、= 129 40これはIT230のみ必要学生=(129分の45)* 30 = 10.46または私は11の結果を必要$per_section

あります。

IT213のみ必要な学生=(44/129)* 30 = 10.23または私は10の結果が必要です。

IT214のみ必要な学生=(40/129)* 30 = 9.3または私は結果の9だけが必要です。

私はIT230でレコードを検索し、そのうちの11個だけを選択したいと考えています。 IT213は10、IT214は9です。私は3つのレコードしか持っていないので、これらの3つのレコードだけが表示されます。そして、あなたは、単一のループ内のすべての情報を得ることができます

SELECT s.total_students, s.code, r.* 
FROM students AS s 
LEFT JOIN result AS r ON r.subject_id = s.code 
WHERE s.teacher_id = '$get_id' AND r.faculty_id = '$get_id' 
ORDER BY s.code 

::2つのテーブルを結合し

+1

mysql_queryはphp5.6で廃止され、php7ではサポートされていません。代わりにPDOを使用してください。http://php.net/manual/en/book.pdo.php – brianforan

+0

準備文も使用してください。http://php.net/manual/en /pdo.prepared-statements.php SQLインジェクションを防ぐために – brianforan

+0

ループしないで、2つのテーブルを結合します。 – Barmar

答えて

0

いずれかを使用したクエリ

while ($result = mysqli_fetch_assoc($query)) { 
    if (!isset($per_section[$result['code']]) { 
     $per_section[$result['code']] = round($result['total_student']/$total*30, 0, PHP_ROUND_HALF_UP); 
     $code[] = $result['code']; 
    } 
    if ($result['subject_id'] !== null) { 
     // do stuff with columns from result table here 
    } 
} 
+0

先生、私はそれを含むのを忘れてしまったので私の問題コードを更新しました。ありがとうございました。 – Eric

+0

最初のループは機能しません。クエリによって返された '$ row ['total_students']はありません。 – Barmar

+0

$ result ['total_students']に更新されました。まだ幸運なことだ – Eric

0

あなたの質問にコメントで述べたように、それはありますPDOJOINの2つのテーブルを介して準備されたステートメントを使用する方がはるかに優れています。

$dbh = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'); 

$stmt = $dbh->prepare(' 
    SELECT result.* 
    FROM result 
    JOIN student ON result.subject_id = student.code 
    WHERE student.id = :id 
'); 

$stmt->execute(['id' => $id]); 
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); 
+0

近い将来にそれらのことを練習しますが、その前に、私はそれを含むのを忘れてしまったので、問題コードを更新しました。 – Eric

関連する問題