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

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

问题描述:

所以我的问题是,在MySqL表中我有值:“minas”和“subject”可能会加倍,但评论“值会有所不同。我可以在表格中显示所有具有相同“主题”和“minas”值的行,而不仅仅是最后一行吗?无法在html表中显示多个mysql查询结果

此外,如果您对我的编写代码的方式有任何进一步的意见,指正

如果您在这里有问题的理解有一些照片:
http://imgur.com/gallery/Oa9Jc

这是我的代码(PHP和HTML合并)

<?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> 
+0

在基本级别上,您可以追加到您的'$ comment'变量,而不是每次覆盖它。例如: '$ comment。= $ row [“comment”];' – Andy

+0

您可能想在它们之间放置某种分隔符也 – Andy

+0

您可以初始化循环外部的空数组,并在循环中可以保存所有项在里面 。 –

所以,这是你的情况下,

你遍历数据库结果,然后每一次定义结果同一个变量。

所以不是单一变量存储值并追加它的价值在每一个迭代,你需要将其存储在一个这样的数组:

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

然后在你的餐桌,遍历您的意见阵列并建立您的HTML这样的:

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; 
} 
+0

为什么要做第一个循环? 'fetch_all()'可以在没有循环的情况下完成。 – Xorifelse

+0

+1已完成。必须对html连接进行一些更改:$ template。='​​'。 $评论。 ''; (所以它在表格区域垂直出现)而不仅仅是$ template。= $ comment; – KpS

可以初始化你的循环之外,在你的循环中,您可以保存它里面的所有物品,例如空数组。

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 
    } 
} 

并在您的视图中,可以通过数组循环和回声值。

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

但我认为最好的答案是Xorifelse的答案。

+0

现在我们需要遍历值[再次](http://stackoverflow.com/questions/42820615/cant-display-multiple-mysql-query-results-in-a-html-table/42820909#comment72753733_42820881) 。此外,你没有添加日期和主题。 – Xorifelse

+0

@Xorifelse是的,你是对的。 –

直接incorperating循环到HTML可以像这样做:

<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> 

这或多或少是一个工作示例,但如果有实际结果,您可能只想打印表格。这意味着您应该只打印if声明中的html表格。

+0

谢谢,我试过ypu告诉我,它的工作原理,但我也想显示结果之一。在我的情况下,它们是水平列出的。我该怎么办? – KpS

+0

哎呀,我忘了在循环中添加'tr'元素,进行了更新。 – Xorifelse