Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 打印包含一些MySQL记录的数组_Php_Html_Mysql_Html Table_Duplicates - Fatal编程技术网

Php 打印包含一些MySQL记录的数组

Php 打印包含一些MySQL记录的数组,php,html,mysql,html-table,duplicates,Php,Html,Mysql,Html Table,Duplicates,不确定该主题是否适合该问题 我有一个表,其中包含一些数据和年份信息,如下所示 table mydata +----+------------+----------+ | id | year | some_data| +----+------------+----------+ | 1 | 2011 | x | | 2 | 2012 | y | | 2 | 2009 | x | | 2 | 2007

不确定该主题是否适合该问题

我有一个表,其中包含一些数据和年份信息,如下所示

table mydata
+----+------------+----------+
| id | year       | some_data|
+----+------------+----------+
|  1 | 2011       | x        |
|  2 | 2012       | y        |
|  2 | 2009       | x        |
|  2 | 2007       | z        |
|  1 | 2009       | b        |
|  3 | 2011       | a        |
|  2 | 2007       | n        |
|  3 | 2006       | h        |
|  2 | 2007       | t        |
+----+------------+----------+
这是我的问题。 我正试图从mydata表中获取具有年份信息的特定id的数据。然而,我需要显示特定年份的记录,比如过去6年和未来6年的记录

比如,

id = 2

+----+------------+----------+
| id | year       | some_data|
+----+------------+----------+
|  2 | 2007       | z        |
|  2 | 2007       | n        |
|  2 | 2009       | x        |
|  2 | 2012       | y        |
+----+------------+----------+
我需要在HTML表结构中显示这样的数据:

+------------+----------+
| year       | some_data|
+------------+----------+
| 2006       |          |
| 2007       | z        |
|            | n        |
|            | t        |
| 2008       |          |
| 2009       | x        |
| 2010       |          |
| 2011       |          |
| 2012       | y        |
| 2013       |          |
| 2014       |          |
| 2015       |          |
| 2016       |          |
| 2017       |          |
| 2018       |          |
+------------+----------+
我不知道哪一年有重复记录,哪一年没有。因此,我总是尝试使用多个查询或大量for循环来解决问题。而且没有按照我的要求工作


还有想法

从数据库中获取排序后的数据,然后在希望输出的年份中循环,用PHP构建输出表

例如,使用PDO对象
$dbh

$qry = $dbh->prepare('
  SELECT   year, some_data
  FROM     mydata
  WHERE    year BETWEEN :start AND :end
  ORDER BY year
');

$qry->bindParam(':start', $range_start);
$qry->bindParam(':end'  , $range_end  );

$current_year = intval(date('Y'));
$range_start  = $current_year - 6;
$range_end    = $current_year + 6;

if ($qry->execute()) {

  $row = $qry->fetch();

  echo '<table><tr><th scope="col">year</th><th scope="col">some_data</th></tr>';
  for ($y = $range_start; $y <= $range_end; $y++) {
    echo "<tr><th scope=\"row\">$y</th><td><ul>";
    while ($row && $row['year'] == $y) {
      echo '<li>' . htmlentities($row['some_data']) . '</li>';
      $row = $qry->fetch();
    }
    echo "</ul></td></tr>";
  }
  echo '</table>';

}
$qry=$dbh->prepare('
选择年份、一些数据
从mydata
其中:开始和:结束之间的年份
按年订购
');
$qry->bindParam(':start',$range\u start);
$qry->bindParam(':end',$range\u end);
$current_year=intval(日期('Y');
$range\u start=$current\u year-6;
$range\u end=$current\u year+6;
如果($qry->execute()){
$row=$qry->fetch();
回显“年度数据”;
对于($y=$range_start;$y fetch());
}
回声“”;
}
回声';
}
$row=$qry->fetch();
在while语句中!不知道为什么我没有考虑它。这节省了我的时间。谢谢!