dtudj42064
2017-03-15 21:02
浏览 62
已采纳

无法在html表中显示多个mysql查询结果

So my problem is that in the MySqL table that i have the values: "minas" and "subject" might be doubled but the comment" value will be different. How can i display in the table all the rows that have the same "subject" and "minas" values and not the last one only?

Also, if you have any further comments on my way of writing the code feel free to correct me

In case your having trouble understanding here are some photos:
http://imgur.com/gallery/Oa9Jc

This is my Code (PHP and html combined)

<?php

include 'dbconn.php';

 if(isset($_POST['Submit'])) {

   $subject = trim(mysqli_real_escape_string($conn, $_POST['mathima']));
   $month = trim(mysqli_real_escape_string($conn, $_POST['minas']));

   $sql = "SELECT subject, month, comment FROM mathimata WHERE subject = '$subject' AND month = '$month'";

   $result = $conn->query($sql);

   $noresult = "Δεν έχει καταχωρηθεί το συγκεκριμένο μάθημα";

   if ($result->num_rows > 0) {
         // output data of each
         while($row = $result->fetch_assoc()) {
             $comment =  $row["comment"];
              }
            }
          }
  ?>
<!DOCTYPE html>
<html lang="el">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

    <link rel="icon" href="../../favicon.ico">

    <title>E•Pagkrition</title>

    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <!-- Custom styles for this template -->
    <link href="cover.css" rel="stylesheet" >

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="../assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
  </head>

    <body>
      <div class="container">
        <h2>Αποτελέσματα αναζήτησης</h2>
        <p>Εδώ είναι το μάθημα που επέλεξες</p>
        <table class="table">
          <thead>
            <tr>
              <th>Μάθημα</th>
              <th>Μήνας</th>
              <th>Περιγραφή</th>
            </tr>
          </thead>
          <tbody>
        <tr>
          <td><?php echo "$subject"; ?></td>
          <td><?php echo "$month"; ?></td>
          <td><?php if (isset($comment)) {
            echo $comment;
          } else {
            echo $noresult;
          }?></td>
        </tr>
      </tbody>
    </table>
  </div>
</body>
</html>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dqudtskm49788 2017-03-15 21:20
    已采纳

    so, this is your scenario,

    you are looping over the database results , then define the result to the same variable every time.

    so instead of storing values in a single variable and append it's value in every iterate, you will need to store it in an array like this :

    if ($result->num_rows > 0) {
        // output data of each
        $comments = [];
        while($row = $result->fetch_assoc()) {
            $comments[] =  $row["comment"];
        }
    }
    

    then in your table , iterate over your comments array and build your html like this :

    foreach ($comments as $comment) {
        $template = '';
        $template .= '<tr>';
        $template .= '<td>' . $subject . '</td>';
        $template .= '<td>' . $month . '</td></td>';
        if (isset($comment)) {
            $template .= $comment;
        } else {
            $template .= $noresult;
        }
        $template .= '</td></tr>';
        echo $template;
    }
    
    已采纳该答案
    打赏 评论
  • du5739 2017-03-15 21:19

    You can initialize an empty array outside your loop and in your loop you can save all the items inside it for example.

    if ($result->num_rows > 0) {
      // initialize your array here 
            $comments = [];
            // output data of each
            while($row = $result->fetch_assoc()) {
            $comments[] =  $row["comment"]; // save your values inside the array
        }
     }
    

    and in your view you can loop through the array and echo the values.

    foreach($comments as $comment) {
     // echo your values here
      echo $comment
    }
    

    but what i think the best answer is Xorifelse's answer.

    打赏 评论
  • duanbei6427 2017-03-15 21:22

    Directly incorperating the loop into the html can be done like so:

    <tbody>
      <?php
        // you should execute your query here.
        if ($result->num_rows > 0){
          while($row = $result->fetch_assoc()){
            echo '<tr>';
            echo '<td>', $row['subject'], '</td>';
            echo '<td>', $row['month'],   '</td>';
            echo '<td>', $row['comment'], '</td>';
            echo '</tr>';
          }
        }
     ?>
    </tbody>
    

    This is more or less a working example, but you might only want to print the table if there are actual results. Meaning that you should only print the html table inside the if statement.

    打赏 评论

相关推荐 更多相似问题