Wordpress - 获取HTML表格中的所有行

时间:2018-03-07 19:00:30

标签: php mysql wordpress foreach

我有一个自定义wordpress表,我试图将数据库中的所有行分隔年份。我的代码现在:

echo "<table width='100%'>";
for ($year = date("Y"); $year >= 2017; --$year) {         
    echo ("<tr><th>$year</th></tr>");       
    echo ("<tr><th>Champions</th>");

    echo ("<th>Score</th>");

    echo ("<th>Date</th></tr>");

  global $wpdb;
        $clubmatch = $wpdb->get_results("SELECT * FROM wp_club ORDER BY gamedate DESC");

    foreach($clubmatch as $row){

      echo "<tr><td>" . $row->names . "</td>";   
        echo "<td>" . $row->score . "</td>";
        echo "<td>" . $row->gamedate . "</td></tr>";
    }
    echo ("<tr><td style='padding: 20px;'></td></tr>");   


} 
echo '</table>';    

这在屏幕上给出了以下内容:

2018
Champions   Score   Date
Sam Jones - Peggy Jones    81.43    2018-03-01
Joseph Parks - Carmen Parks    70.85    2017-12-17

2017
Champions   Score   Date
Sam Jones - Peggy Jones    81.43    2018-03-01
Joseph Parks - Carmen Parks    70.85    2017-12-17

如何让它显示这样?适当年份下的每一行?

2018
Champions   Score   Date
Sam Jones - Peggy Jones    81.43    2018-03-01

2017
Champions   Score   Date
Joseph Parks - Carmen Parks    70.85    2017-12-17

提前致谢!

1 个答案:

答案 0 :(得分:1)

更改SQL查询,以便按字段gamedate的年份过滤结果。所以使用这个:(为了清晰起见,代码是缩进的)

$clubmatch = $wpdb->get_results(
    "SELECT * FROM wp_club " .
    "WHERE YEAR(gamedate) = '$year' " .
    "ORDER BY gamedate DESC"
);

..而不是:

$clubmatch = $wpdb->get_results("SELECT * FROM wp_club ORDER BY gamedate DESC");