2

Please help me. How to select student result from databese using multiple select. But not work.......

My Student Database like - std_profile

id StudentId  stdName     stdRoll    stdClass
1    P700       ABC            1          A
2    P701       DEF            2          A
3    P702       GHI            3          A
4    P703       JKL            4          A
5    P704       MNO            5          A

std_subject

id     Subject     SubjectId
1      Bengali          1
2      English          2
3      Mathematics      3

std_result

id   StudentId    SubjectId    TermOneWritten    TermOneOral     TermTwoWritten    TermTwoOral
1       P700          1              29               10               58               18
2       P700          2              25               9                51               20
3       P700          3              20               10               60               19
4       P701          1              26               9                57               18
5       P701          2              25               10               50               17
6       P701          3              23               8                56               20

I want result output like this -

Student Id - P700, Student Name - ABC, Roll - 1, Class - A
Subject    TermOneWritten    TermOneOral     TermTwoWritten    TermTwoOral
Bengali          29               10               58               18
English          25               9                51               20
Mathematics      20               10               60               19
Total                    103                               226

Student Id - P702, Student Name - DEF, Roll - 2, Class - A
Subject    TermOneWritten    TermOneOral     TermTwoWritten    TermTwoOral
Bengali          26               9                57               18
English          25               10               50               17
Mathematics      23               8                56               20
Total                    101                               218

But when i select multiple student then display result with wrong place like ( Both Information display in same place ) like -

Student Id - P700, Student Name - ABC, Roll - 1, Class - A
Subject    TermOneWritten    TermOneOral     TermTwoWritten    TermTwoOral
Bengali          29               10               58               18
English          25               9                51               20
Mathematics      20               10               60               19
Bengali          26               9                57               18
English          25               10               50               17
Mathematics      23               8                56               20
Total                    103                               226
Total                    101                               218

Student Id - P702, Student Name - DEF, Roll - 2, Class - A
Subject    TermOneWritten    TermOneOral     TermTwoWritten    TermTwoOral
Bengali          29               10               58               18
English          25               9                51               20
Mathematics      20               10               60               19
Bengali          26               9                57               18
English          25               10               50               17
Mathematics      23               8                56               20
Total                    103                               226
Total                    101                               218

My PHP + HTML Code -

<?php error_reporting(0); $studentIds = str_replace(',', "','",$_GET["StudentId"]); ?>
<?php
    $StudentId=$_GET['StudentId'];
    $sql = "SELECT std_result.StudentId,std_profiles.stdName,std_profiles.stdRoll,std_profiles.stdClass FROM std_result JOIN std_profiles on std_profiles.StudentId=std_result.StudentId WHERE std_result.StudentId IN ('" . implode("','", $studentIds) . "') GROUP BY std_result.StudentId";
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(':studentIds',$studentIds,PDO::PARAM_STR);
    $stmt->execute();
    $results=$stmt->fetchAll(PDO::FETCH_OBJ);
    $cnt=1;
    if($stmt->rowCount() > 0)
    { foreach($results as $row) {   ?>
    <div class="row">
        <div class="col-lg-12">
            <div class="stdinfo">
                ID : <?php echo htmlentities($row->StudentId);?> |
                Name : <?php echo htmlentities($row->stdName);?> |
                Roll : <?php echo htmlentities($row->stdRoll);?> |
                Class : <?php echo htmlentities($row->stdClass);?> |
            </div>
        </div>
    </div>

<table id="dt_basic" class="table table-striped table-bordered table-hover" width="100%">
    <thead>
        <tr>
            <th>Subject</th><th>TermOneWritten</th><th>TermOneOral</th><th>TermTwoWritten</th><th>TermTwoOral</th><th>Total MO</th>
        </tr>
    </thead>
</table>

<?php  
     $sql1 = "SELECT std_result.id,std_result.SubjectId,std_subject.Subject,std_result.TermOneWritten,std_result.TermOneOral,std_result.TermTwoWritten,std_result.TermTwoOral FROM std_result INNER JOIN std_subject ON std_subject.SubjectId=std_result.SubjectId WHERE std_result.StudentId IN ('" . implode("','", $studentIds) ."') ORDER BY id, StudentId ASC";  
        $result = mysqli_query($mysqli, $sql1);  
        while($row=mysqli_fetch_array($result)) {
?>
<table id="dt_basic" class="table table-striped table-bordered table-hover" width="100%">
    <tbody>
        <tr>
            <td><?php echo $row['Subject'] ;?></td>

            <td><?php echo $row['TermOneWritten'] ;?></td>
            <td><?php echo $row['TermOneOral'] ;?></td>

            <td><?php echo $row['TermTwoWritten'] ;?></td>
            <td><?php echo $row['TermTwoOral'] ;?></td>

            <td><?php echo $row["TermOneWritten"]+$row["TermOneOral"]+$row["TermTwoWritten"]+$row["TermTwoOral"];?></td>
        </tr>
    </tbody>
</table>
<?php } ?>

<?php  
    $sql2 = "SELECT SUM(std_result.TermOneWritten+std_result.TermOneOral) AS A, SUM(std_result.TermTwoWritten+std_result.TermTwoOral) AS B  FROM  std_result  WHERE std_result.StudentId IN ('" . implode("','", $studentIds) ."') GROUP BY std_result.StudentId";  
    $result = mysqli_query($mysqli, $sql2);  
    while($row=mysqli_fetch_array($result)) {
 ?>
  <table id="dt_basic" class="table table-striped table-bordered table-hover" width="100%">
       <tfoot>
           <tr>
               <td align="center" style="width: 17.1%">Total</td>
               <td align="center" style="width: 17%"><?php echo $row["A"];?> / <?php echo $row["B"];?> [ ]</td>            
           </tr>
       </tfoot>
  </table>


<?php } } } ?>

Please Help Me....

2

You're getting all the students at once and just displaying the table, without any subselection.

Your second SQL Statement here:

<?php  
     $sql1 = "SELECT std_result.id,std_result.SubjectId,std_subject.Subject,std_result.TermOneWritten,std_result.TermOneOral,std_result.TermTwoWritten,std_result.TermTwoOral FROM std_result INNER JOIN std_subject ON std_subject.SubjectId=std_result.SubjectId WHERE std_result.StudentId IN ('" . implode("','", $studentIds) ."') ORDER BY id, StudentId ASC";  
        $result = mysqli_query($mysqli, $sql1);  
        while($row=mysqli_fetch_array($result)) {
?>

is pulling data for all students in $studentIds and displaying them, rather than displaying a single student.

Your're creating the individual students records correctly because you're getting one row for each student from that query, but when you go to generate your table you're not referencing the student the ForEach loop is on and just returning all of them.

Instead of having WHERE std_result.StudentId IN ('" . implode("','", $studentIds) ."') ... in the sql statement I quoted, store the student id in a variable during the loop and use it instead: WHERE std_result.StudentId = '"$studentId"' ... (don't quote me on the PHP syntax, I could be wrong)

Hope that helps.

  • std_result.StudentId = '"$studentId"' this work one by one, but when select multiple item then not work – Abdul Azim Aug 23 '18 at 4:47

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.