我有一个舞蹈比赛网站,每个用户都可以登录并添加舞蹈时刻, 在我的html表中,所有用户的所有时刻我都拥有所有数据,但我希望在html列中添加"由记录的用户ID"添加的每个时刻的舞者数量。
我有这个:
$array_container= array();
while($fila=mysqli_fetch_assoc($sql)){
$format_org=str_replace(" ","_",$fila["organization"]);
$format_eval=str_replace(" ","_",$fila["EvaluationType"]);
$format_test=str_replace(" ","_",$fila["test_name"]);
$CapaEnviar=$format_org.$format_eval;
$array_container[] = array($CapaEnviar => $fila['test_name']);
}
echo json_encode($array_container,true);
phpMyAdmin时刻表:有id,俱乐部名称,类别,纪律,部分,以及:
但是这个过程会计算所有用户的所有舞者名字。 这个过程的例子:你总共有200个舞者!
我希望这个过程为我计算所有舞者的名字,每个时刻都添加在表单中,而不是所有整个用户的时刻,例如:如果用户john有两个时刻添加:时刻1:5舞者 - 时刻2 :每个用户都有10位舞者,等等。
答案 0 :(得分:2)
让我试着让你以正确的方式(这似乎是一个很长的帖子,但我认为值得初学者阅读它!)。
评论中告诉您规范化您的数据库,如果我是您,如果您希望您的项目长时间正常运行......我会这样做。< / p>
有许多MySQL规范化教程,如果你感兴趣的话,你可以将它自己谷歌...我只是想用你的特定例子来帮助你,我相信你会理解它。
基本上,您必须创建不同的表来存储“不同的概念”,然后在查询数据库时加入它。
在这种情况下,我会创建这些表:
categories
,dance_clubs
,users
和dancers
存储“基本”数据。
moments
和moment_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做一些查询。
我们将使用预备语句而不是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
。更改了功能以使脚本适应新的表和名称。