使用 Wordpress 并使用其功能在两个自定义表上查询数据库。我正在尝试将数据输出为“树/分组”格式。例子:
Team Name One
- Player Name One
- Player Name Two
- Player Name Three
- Etc...
Team Name Two
- Player Name one
- Player Name Two
- Player Name Three
- Etc...
我不确定要以这种方式显示结果需要什么。这是我有一个连接表的查询......
global $wpdb;
$user_id = '1';
$teamlist = $wpdb->get_results( $wpdb->prepare(
"
SELECT
fv_teams.team_id AS team_teamid,
fv_players.player_id,
fv_teams.team_name AS teamname,
fv_players.player_fname AS fname,
fv_players.player_lname AS lname,
fv_players.player_team AS team,
fv_players.player_position AS position
FROM
fv_teams
INNER JOIN fv_players ON fv_teams.team_id = fv_players.team_id
WHERE
fv_teams.user_id = %d
",
$user_id
), OBJECT );
我当然可以通过以下方式获得结果...
foreach ( $teamlist as $teamrow )
{
echo 'Team: ' . $teamrow->teamname . '<br />';
echo 'Player ID: ' . $teamrow->fname . '<br />';
}
但是团队名称将在结果中重复为
Team Name One
- Player One
Team Name One
- Player Two
Team Name One
- Player Three
- Etc...
我相当确定我需要在其中添加一个嵌套循环,但我不确定所需的编码。
任何想法或方向都会对一个刚刚失去大部分头发的人非常有帮助。; )