-1

I currently have this code set up:

$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    $data_exist = true;
    while($row = mysqli_fetch_assoc($result)) {
      $id = $row["id"];
      $teacher_set = $row["teacher_set"];
      $class = $row["class"];
      $name = $row["name"];
      $description = $row["description"];

    }
}

And then:

<?php if ($data_exist){?>
            <p><?php  echo $id ?></p>
            <p><?php echo $teacher_set?></p>
            <p><?php echo $name?></p>
            <p><?php echo $description?></p>
          <?php
          }?>

However, the issue is if there is multiple results in the database it only outputs one of them, how can I prevent this from happening and output two?

I want to make it so every row has their own section, like this: http://prntscr.com/hcgtqn so if there is only one result, one one will show etc.

Tom
  • 1
  • 4
  • 1
    Possible duplicate of [How to select multiple rows from mysql with one query and use them in php](https://stackoverflow.com/questions/6481806/how-to-select-multiple-rows-from-mysql-with-one-query-and-use-them-in-php) – Martin Nov 18 '17 at 16:36

2 Answers2

1

You have to echo data in a loop. Right now you are reassigning values in while($row = mysqli_fetch_assoc($result)) iterations and printing just the last one.

SirKometa
  • 1,857
  • 3
  • 16
  • 26
  • How do I echo in a loop? – Tom Nov 18 '17 at 16:16
  • It all depends on what you want to achieve. where you do assignment ` $id = $row["id"];` instead maybe you should add it to array and then loop through that array and echo elements instead of `

    `. Simplest way would be writing `echo $row["id"];` instead `$id = $row["id"];`
    – SirKometa Nov 18 '17 at 16:23
1

You need to print each time you read a row from the database. about the styles, you can represent it in many ways. In the code below I present it in a table.

<table>
  <thead>
    <tr>
      <th>id</th>
      <th>teacher set</th>
      <th>name</th>
      <th>description</th>
    </tr>
  </thead>
  <tbody>
<?php
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_array($result)) {
      $id = $row["id"];
      $teacher_set = $row["teacher_set"];
      $class = $row["class"];
      $name = $row["name"];
      $description = $row["description"];
      // you need to print the output now otherwise you will miss the row!
      // now printing
      echo "
      <tr>
      <td>".$id."</td>
      <td>".$teacher_set."</td>
      <td>".$name."</td>
      <td>".$description."</td>
      </tr>";
    }
}
else // no records in the database
{
    echo "not found!";
}

?>
</tbody>
</table>
</body>
</html>
  • I don't want to print them there though... I use them multiple places in the document. – Tom Nov 19 '17 at 10:22