【问题标题】:Putting a table in a 2d array and display it将表格放入二维数组并显示它
【发布时间】:2012-02-03 13:37:59
【问题描述】:

我有两个问题,请帮助我。

  1. 在 PHP/MySQL 中,我有一个很长的查询,我用 7 行编写它。现在我 想要运行它并将该查询的结果(表)作为 2d 变量中的数组。我该怎么做?
  2. 在我将查询结果作为二维数组放入 a 变量后,现在 如何以表格的形式显示我的查询结果 html页面?

谢谢

**** 示例 **** **

        $database_host = "localhost";
    $database_username = "root";
    $database_password = ""; // FOR ME!!!   

    $connection = mysql_connect("$database_host", "$database_username", "$database_password") or die("Could't connect to database!");
    if(!mysql_select_db("football_db", $connection)) 
        die("Could't select database!");

    // Sanitizing leagueName input
    $leagueName = strtolower(strval($_POST[ 'leagueName' ])); 
    $leagueName = stripslashes( $leagueName ); 

    // Sanitizing seasonName input
    $seasonName = strtolower(strval($_POST[ 'seasonName' ])); 
    $seasonName = stripslashes( $seasonName ); 



    $query = "SELECT cl.club_name , cr.played , cr.matches_won AS W , cr.matches_lose AS L , 
        (cr.played - ( cr.matches_won + cr.matches_lose) ) AS D , 
        cr.goals_for AS GF , cr.goals_against AS GA , (cr.goals_for - cr.goals_against) AS GD ,
        (cr.matches_won*3 + (cr.played - ( cr.matches_won + cr.matches_lose) ) ) AS PTS
    FROM ClubsRecords AS cr , Clubs AS cl , Seasons AS se , leagues AS le
    WHERE le.league_type='$leagueName' AND  se.season_name ='$seasonName' AND 
        se.league_id = le.league_id AND cr.club_id = cl.club_id AND cr.season_id = se.season_id 
    ORDER BY PTS DESC;";

【问题讨论】:

  • 是的,我用谷歌搜索过,但我不知道如何将查询结果放入二维数组并显示出来。
  • 无论如何,如果我们要帮助您,我们需要数据示例,这个问题太笼统了。
  • @amiramir 你可以使用json_encode() 轻松解决这个问题。
  • 我把我的问题放在帖子里。
  • @amiramir 请添加来自print_r( mysql_fetch_Assoc( $query)) 的输出,如果 Afir 的回答不好,请添加所需数据的示例。

标签: php mysql database phpmyadmin


【解决方案1】:
$result = mysql_query($query);
$rows = array();

while($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}

echo json_encode($rows);

我认为这可以帮助你。

【讨论】:

  • 感谢您的大力帮助,但我想将该二维数组显示为 html 页面中的表格。使用 "json_encode" ,它将查询结果显示为一个集合而不是表。
【解决方案2】:
/* Question 1: */

    $res = mysql_query($query);
    $rows = array();
    while ($row = mysql_fetch_assoc($res)) {
        $rows[] = $row;
    }

    // $rows is your 2d-array

/* Question 2: */

    echo "<table>\n";

    // print the rownames
    $keys = array_keys($rows);
    echo "<tr>\n";
    foreach($keys as $key) {
        echo "    <td>$key</td>\n";
    }
    echo "</tr>\n";

// print the values
echo "<tr>\n";
foreach($rows as $row) {
    print('<tr>');
    foreach($row as $key => $value){
        echo "    <td>$value</td>\n";
        }
    print('</tr>');
}
echo "</tr>\n";
echo "</table>\n";

【讨论】:

  • 您的第二个答案没有将二维数组显示为表格。
【解决方案3】:

在不知道你在寻找什么样的二维数组的情况下,带着一个明显的答案走出困境:

$twoDimArray = array();
$result = mysql_query($query);

if ($result !== false)
{
    while ($row = mysql_fetch_assoc($result))
    {
        $twoDimArray[] = $row;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-04
    • 2017-08-09
    • 2023-01-26
    • 1970-01-01
    • 2013-04-24
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多