从数据库使用PHP动态打印表的问题

时间:2022-01-13 19:23:35

I am working on a project were I am taking input from the user for a range of dates and then printing out the results between those two dates. Currently, I am having no problems getting the data from the database and having them print. The issue is with the formatting and structure of the table.

我正在研究一个项目,我正在从用户那里获取一系列日期的输入,然后在这两个日期之间打印出结果。目前,我从数据库获取数据并打印它没有任何问题。问题在于表格的格式和结构。

Currently the table is printing the titles of the table each time a result is being printed and I am having issues changing that.

目前,每次打印结果时,表格都会打印表格的标题,而我正在改变这些问题。

The code provided is what I have been using.

提供的代码就是我一直在使用的代码。

This is for a class so please try to explain what needs to be done instead of simply giving me code!

这是一个类,所以请尝试解释需要做什么,而不是简单地给我代码!

<form method="post" action="index.php">
Date Range From:
<input type="text" id="dateFrom" name="dateFrom" value="">
To:
<input type="text" id="dateTo" name="dateTo" value="">
<input type="submit" id="submit">
</form>

<?php
include('../inclass/db_connect.php');

if(isset($_POST['dateFrom'])){$dateFrom =   $_POST['dateFrom'];}else{$dateFrom=0;};
if(isset($_POST['dateTo'])){$dateTo = $_POST['dateTo'];}else{$dateTo=0;};

$result =$pdo->prepare("SELECT matchDate, player1, player2, result, eco FROM    matches WHERE matchDate BETWEEN :a AND :b LIMIT 250");
$result->bindParam(':a', $dateFrom);
$result->bindParam(':b', $dateTo);
$result->execute();

for($i=0; $row = $result->fetch(); $i++){
?>
<table border="1">
    <tr>
        <th>Match Date</th>
        <th>Player 1</th>
        <th>Player 2</th>
        <th>Result</th>
        <th>ECO Code</th>
    </tr>
    <tr>
        <td><?php echo $row['matchDate']; ?></td>
        <td><?php echo $row['player1']; ?></td>
        <td><?php echo $row['player2']; ?></td>
        <td><?php echo $row['result']; ?></td>
        <td><?php echo $row['eco']; ?></td>
    </tr>
</table>
<?php
}
?>

http://cps276.net/zgambrell/04/index.php

http://cps276.net/zgambrell/04/index.php

2 个解决方案

#1


0  

You should move the table and table header out of the loop or you would end up adding them for each row returned by the query. The code should roughly look like the following :

您应该将表和表头移出循环,否则最终会为查询返回的每一行添加它们。代码应大致如下所示:

<table>
  <!-- table header --> 
  <tr>
    <th>..</th>
    ..
  </tr>

  <!-- Records -->
  <?php
  for (?;?;?) {
  ?>
  <tr>
    <td><?php echo $row['?'] ?></td>
    ..
  </tr>
  <?php } ?>
<!-- End of table --> 
</table>

#2


0  

Table html code is within the ForEach loop. That's why it is printing for each record. Place the table colum header outside the for loop.

表html代码在ForEach循环中。这就是它为每条记录打印的原因。将表colum标头放在for循环之外。

    <table border="1">
        <tr>
            <th>Match Date</th>
            <th>Player 1</th>
            <th>Player 2</th>
            <th>Result</th>
            <th>ECO Code</th>
        </tr>

    for($i=0; $row = $result->fetch(); $i++){
    ?>
        <tr>
            <td><?php echo $row['matchDate']; ?></td>
            <td><?php echo $row['player1']; ?></td>
            <td><?php echo $row['player2']; ?></td>
            <td><?php echo $row['result']; ?></td>
            <td><?php echo $row['eco']; ?></td>
        </tr>

    <?php
    }
    ?>
</table>

#1


0  

You should move the table and table header out of the loop or you would end up adding them for each row returned by the query. The code should roughly look like the following :

您应该将表和表头移出循环,否则最终会为查询返回的每一行添加它们。代码应大致如下所示:

<table>
  <!-- table header --> 
  <tr>
    <th>..</th>
    ..
  </tr>

  <!-- Records -->
  <?php
  for (?;?;?) {
  ?>
  <tr>
    <td><?php echo $row['?'] ?></td>
    ..
  </tr>
  <?php } ?>
<!-- End of table --> 
</table>

#2


0  

Table html code is within the ForEach loop. That's why it is printing for each record. Place the table colum header outside the for loop.

表html代码在ForEach循环中。这就是它为每条记录打印的原因。将表colum标头放在for循环之外。

    <table border="1">
        <tr>
            <th>Match Date</th>
            <th>Player 1</th>
            <th>Player 2</th>
            <th>Result</th>
            <th>ECO Code</th>
        </tr>

    for($i=0; $row = $result->fetch(); $i++){
    ?>
        <tr>
            <td><?php echo $row['matchDate']; ?></td>
            <td><?php echo $row['player1']; ?></td>
            <td><?php echo $row['player2']; ?></td>
            <td><?php echo $row['result']; ?></td>
            <td><?php echo $row['eco']; ?></td>
        </tr>

    <?php
    }
    ?>
</table>