0

My code join 4 table using INNER JOIN. I can't customize loop while using inner join. How to control while clause so unwanted loop wouldn't happen.

My code output (student and school name is looping in every subject field)

enter image description here

I want to display like this where student and school loops at once.

enter image description here

MY PHP Code

<table id="customers">
    <?php
        $query=$con->prepare("SELECT subjectcomb.subjectid, subjectcomb.schoolid, student.student, school.school, subject.name 
        FROM ((( subjectcomb
        INNER JOIN school ON subjectcomb.schoolid=school.id)
        INNER JOIN student ON subjectcomb.schoolid=student.schoolID)
        INNER JOIN subject ON subjectcomb.subjectid=subject.id)");
        $query->execute();
        while ($row = $query->fetch(PDO::FETCH_ASSOC)){?>
            <tr>
                <td>
                    <?php echo $row['school']."->".$row['student']?>
                </td>
            </tr>
            <tr>
                <td>
                    <?php echo $row['name']?>
                    <input type="text" name="mark"></input>
                </td>           
            </tr>
        <?php } ?>
</table>
4
  • Adding an if judgement before the first <tr> and judge whether you have already output this schoool->student could solve this issue. But I guess you want to solve it just with SQL command? Commented Feb 2, 2018 at 3:08
  • Did you try using group by student.student, school.school in your query? Commented Feb 2, 2018 at 3:32
  • @Phil How to apply if clause in this case, please give example. Commented Feb 2, 2018 at 3:35
  • @AnandaLC, Lucas Borges just gave you an example. That's also what I mean. Commented Feb 2, 2018 at 3:49

2 Answers 2

1

Try adding an ORDER BY to your query:

$query=$con->prepare("SELECT subjectcomb.subjectid, subjectcomb.schoolid, student.student, school.school, subject.name 
        FROM ((( subjectcomb
        INNER JOIN school ON subjectcomb.schoolid=school.id)
        INNER JOIN student ON subjectcomb.schoolid=student.schoolID)
        INNER JOIN subject ON subjectcomb.subjectid=subject.id)
        ORDER BY school.school, student.student");

then in the while loop use this check:

<?php
$last_student = ""
while ($row = $query->fetch(PDO::FETCH_ASSOC)){
    if ($last_student != $row['student']) {
?>
    <tr>
        <td>
            <?php echo $row['school']."->".$row['student']?>
        </td>
    </tr>
    <?php } ?>
    <tr>
        <td>
            <?php echo $row['name']?>
            <input type="text" name="mark"></input>
        </td>           
    </tr>
<?php $last_student = $row['student'] } ?>
Sign up to request clarification or add additional context in comments.

Comments

0

I think what Phil meant was something like the following. You keep track of the previous school and student. If the current school and student are the same as the previous, you skip printing the school/student row, but if they not the same, you print them and also update the previous school's and previous student's values.

$prev_school = '';
$prev_student = '';
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
  if ($row['school'] != $prev_school && $row['student'] != $prev_student) { ?>
    <tr>
      <td>
        <?php echo $row['school']."->".$row['student'] ?>
      </td>
    </tr>
    <?php
    $prev_school = $row['school'];
    $prev_student = $row['student'];
  }
  ?>
  <tr>
    <td>
      <?php echo $row['name']?>
      <input type="text" name="mark"></input>
    </td>           
  </tr>
  <?php 
}

1 Comment

Thanks for your great answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.