【问题标题】:Php/Mysql count dancers from each moment added issuePhp/Mysql 计算每个时刻的舞者添加问题
【发布时间】:2016-09-01 19:55:45
【问题描述】:

我有一个舞蹈比赛网站,每个用户都可以登录并添加舞蹈时刻, 在包含所有用户的所有时刻的 html 表中,我拥有所有数据,但我希望在 html 列中添加“记录的用户 ID 添加的每个时刻的舞者数量”。

我有这个:

$c = mysql_query("SELECT * FROM moments");
$dancers = 0;
while($rows = mysql_fetch_array($c)){
    for($i = 1; $i <= 24; $i++){
        $dan_id = 'dancer'.$i;
        if($rows[$dan_id] != "" || $rows[$dan_id] != null )
            $dancers++;
    }   
}
echo "<th class="tg-amwm">NR of dancers</th>";
echo "<td class='tg-yw4l'>$dancers</td>";

phpMyAdmin 时刻表:有 id、clubname、category、discipline、section 和这个:

但这个过程是计算所有用户时刻的所有舞者姓名。 此过程的示例:您总共有 200 名舞者!

我希望该过程为我计算在表单中添加的每个时刻的所有舞者姓名,而不是所有用户时刻的总数,如下所示:如果用户 john 添加了两个时刻:时刻 1:5 位舞者 - 时刻 2 : 10 位舞者,以此类推。

【问题讨论】:

  • stop using mysql_* functionsThese extensions 已在 PHP 7 中删除。了解PDOMySQLiprepared 语句并考虑使用 PDO,it's really pretty easy
  • 我是新手,谢谢杰的建议
  • 考虑规范化您的数据库...在列中以逗号分隔的值列表是不好的设计,为什么需要存储计数和名称列表?你不能数一数列表中的名字吗?
  • 请根据用户的专业水平调整您的 cmets。 @Fido 对于 PHP 和 mySql 来说显然是个新手。
  • 你的 WHILE() 循环中有一个 FOR() 循环。 FOR() 循环的用途是什么?

标签: php mysql


【解决方案1】:

让我试着把你放在正确的位置(这似乎是一个很长的帖子,但我认为值得初学者阅读它!)。

cmets 告诉你规范化你的数据库,如果我是你,如果你想让你的项目长期运行良好......我会这样做。

有很多 MySQL 规范化教程,如果您有兴趣,可以自行在 Google 上搜索...

基本上,你必须创建不同的表来存储“不同的概念”,然后在查询数据库时加入它。

在这种情况下,我会创建这些表:

categoriesdance_clubsusersdancers 存储“基本”数据。

momentsmoment_dancers 存储外键以创建数据之间的关系。

让我们看一下内容以更好地理解它。

mysql> select * from categories;
+----+---------------+
| id | name          |
+----+---------------+
|  1 | Hip-hop/dance |
+----+---------------+

mysql> select * from dance_clubs;
+----+---------------+
| id | name          |
+----+---------------+
|  1 | dance academy |
+----+---------------+

mysql> select * from users;
+----+-------+
| id | name  |
+----+-------+
|  1 | alex  |
+----+-------+

mysql> select * from dancers;
+----+-------+
| id | name  |
+----+-------+
|  1 | alex  |
|  2 | dan   |
|  3 | mihai |
+----+-------+

mysql> select * from moments;
+----+--------------+---------------+-------------------+
| id | main_user_id | dance_club_id | dance_category_id |
+----+--------------+---------------+-------------------+
|  1 |            1 |             1 |                 1 |
+----+--------------+---------------+-------------------+
          (user alex)  (dance acad..)     (Hip-hop/dance)

mysql> select * from moment_dancers;
+----+-----------+-----------+
| id | moment_id | dancer_id |
+----+-----------+-----------+
|  1 |         1 |         1 | (moment 1, dancer alex)
|  2 |         1 |         2 | (moment 1, dancer dan)
|  3 |         1 |         3 | (moment 1, dancer mihai)
+----+-----------+-----------+

好的!现在我们想从 PHP 中进行一些查询。

我们将使用 prepared statements 代替他们在 cmets 中所说的 mysql_* 查询。

准备好的语句的概念起初可能有点难以理解。只需仔细阅读代码并再次查找一些教程;)

列出舞者的简单示例(只是为了理解它):

// Your connection settings
$connData = ["localhost", "user", "pass", "dancers"];

$conn = new mysqli($connData[0], $connData[1], $connData[2], $connData[3]);
$conn->set_charset("utf8");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Here we explain MySQL which will be the query
$stmt = $conn->prepare("select * from dancers");
// Here we explain PHP which variables will store the values of the two columns (row by row)
$stmt->bind_result($dancerId, $dancerName);

// Here we execute the query and store the result
$stmt->execute();
$stmt->store_result();

// Here we store the results of each row in our two PHP variables 
while($stmt->fetch()){
    // Now we can do whatever we want (store in array, echo, etc)
    echo "<p>$dancerId - $dancerName</p>";
}

$stmt->close();
$conn->close();

浏览器中的结果:

好!现在有点难! 列出时刻

// Your connection settings
$connData = ["localhost", "user", "pass", "dancers"];
$conn = new mysqli($connData[0], $connData[1], $connData[2], $connData[3]);
$conn->set_charset("utf8");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query to read the "moments", but we have their main user and dancers in other tables
$stmtMoments = $conn->prepare("
    select
        moments.id,
        (select name from users where users.id = moments.main_user_id) as main_user,
        (select name from dance_clubs where dance_clubs.id = moments.dance_club_id) as dance_club,
        (select name from categories where categories.id = moments.dance_category_id) as dance_category,
        (select count(*) from moment_dancers where moment_dancers.moment_id = moments.id) as number_of_dancers
    from moments
    ");
// Five columns, five variables... you know ;)
$stmtMoments->bind_result($momentId, $momentMainUser, $momentDanceClub, $momentDanceCategory, $momentNumberOfDancers);

// Query to read the dancers of the "moment" with id $momentId
$stmtDancers = $conn->prepare("
    select
        dancers.name as dancer_name
    from
        dancers join moment_dancers on dancers.id = moment_dancers.dancer_id
    where
        moment_dancers.moment_id = ?
    ");

$stmtDancers->bind_param("i", $momentId);
$stmtDancers->bind_result($momentDancerName);

// Executing the "moments" query
$stmtMoments->execute();
$stmtMoments->store_result();

// We will enter once to the while because we have only one "moment" right now
while($stmtMoments->fetch()){

    // Do whatever you want with $momentId, $momentMainUser, $momentDanceClub, $momentDanceCategory, $momentNumberOfDancers
    // For example:

    echo "<h3>Moment $momentId</h3>";
    echo "<p>Main user: $momentMainUser</p>";
    echo "<p>Dance club: $momentDanceClub</p>";
    echo "<p>Category: $momentDanceCategory</p>";
    echo "<p>Number of dancers: $momentNumberOfDancers</p>";
    echo "<p><strong>Dancers</strong>: ";

    // Now, for this moment, we look for its dancers
    $stmtDancers->execute();
    $stmtDancers->store_result();
    while($stmtDancers->fetch()){

        // Do whatever you want with each $momentDancerName
        // For example, echo it:

        echo $momentDancerName . " ";
    }

    echo "</p>";
    echo "<hr>";
}

$stmtUsers->close();
$stmtMoments->close();

$conn->close();

浏览器中的结果:

仅此而已!有什么问题可以问我!

(如果需要,我可以发布 DDL 代码以使用内容数据创建示例数据库)

已编辑:添加了dancers 表。将 moment_users 重命名为 moment_dancers。更改了功能以使脚本适应新的表和名称。

【讨论】:

  • 感谢 nanocv 的辛勤工作,感谢您的回复!一个问题,在我的表 alex dan & mihai 他们不是用户,是由登录用户添加的舞者,当用户注册时,他们在表格中写了一些舞者的名字,从 1 到 24 舞者,我不知道如何计算那些舞者的名字,在 myphpmyadmin 表中,每个舞者都有一列: dancer1 |舞者2 | dancer3等等..
  • @Fido 这对我来说并不难,我喜欢这样做!正如你所看到的,昨天我有一些空闲时间;)对不起,如果我坚持用这种方式来制作这个应用程序,但我向你保证这是个好方法!在每个时刻使用固定数量的列来存储可变数量的舞者,这根本不是一个好习惯(您的数据库中会有很多空单元格)。我已经编辑了我的答案并创建了一个“舞者”表来以正确的方式存储它们(你可以在图中很容易地看到它)。
  • @nanocv - 非常感谢您为解释规范化的原则和好处做出了真正的努力
猜你喜欢
  • 1970-01-01
  • 2012-05-11
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多