我正在建立一个系统,主要是巩固学习,但将用于实践。
我将试着口头解释我所关注的E-R图的部分:
每个学员可以有许多统一格式的
每个Uniform ID都是表Uniform中的一个新条目,因此cadets
(table)可能看起来像:
id | name | ... | uniformID
1 | Example | ... | 1,2,3
uniform
表格:id | notes | cadet
1 | Need new blahh | 1
2 | Some stuff needed | 1
3 | Whatever you like | 1
仔细想想,看起来我不需要数据库中的第三列。
我试图在uniformID中遍历每个id,代码:
<?php
$cadet = $_GET['id']; // set from URL
$query = mysql_query("SELECT `uniformID` FROM `cadets`
WHERE id = '$cadet' LIMIT 1")
or die(mysql_error()); // get uniform needed as string
// store it
while ($row = mysql_fetch_array($query)) {
$uniformArray = $row['uniformID'];
}
echo $uniformArray . " ";
$exploded = explode(",", $uniformArray); // convert into an array
// for each key in the array perform a new query
foreach ($exploded as $key => $value) {
$query(count($exploded));
$query[$key] = mysql_query("SELECT * FROM `uniform` WHERE `id` = '$value'");
}
?>
正如我所说,这主要是为了进行整合,但我发现了一个错误,sql说:
致命错误:函数名必须是第82行C:\ wamp\www\intranet\uniform.php中的字符串
第82行是:
$query[$key] = mysql_query("SELECT * FROM `uniform` WHERE `id` = '$value'");
我不确定它能不能用,所以我试了一下,现在卡住了!
编辑:
感谢所有为此做出贡献的人!这就是工作代码:
foreach ($exploded as $key => $value) {
//$query(count($exploded));
$query = mysql_query("SELECT * FROM `uniform` WHERE `id` = '$value'");
while ($row = mysql_fetch_array($query)) {
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['note'] . "</td>
</tr>";
}
}
添加while并通过在foreach中嵌套它来执行迭代
最佳答案
除了你的桌子
cadets(id, ...)
uniforms(id, ...)
使用描述
cadets
实体和uniforms
实体之间关系的交叉乘积表cadets_x_uniforms(cadet_id, uniform_id)
对于每个关系(在本例中,cadet x具有统一的y),将一个具有相应id的记录放入cross-product表中。
... 嗯,我想在这种情况下一个有效的例子会更好。。。
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);
// fetch the uniforms of a specific cadet
$stmt = $pdo->prepare('
SELECT
c.name,u.id,u.labelid
FROM
so_cadets as c
LEFT JOIN
so_cadet_uniform as cxu
ON
c.id=cxu.cadet_id
LEFT JOIN
so_uniforms as u
ON
cxu.uniform_id=u.id
WHERE
c.name=?
');
$stmt->execute( array('cadetB') );
$stmt->setFetchMode(PDO::FETCH_ASSOC);
echo "uniforms of cadetB\n";
foreach( $stmt as $row ){
echo join(', ', $row), "\n";
}
// fetch cadets without uniforms
$query = '
SELECT
c.name
FROM
so_cadets as c
WHERE
NOT EXISTS(SELECT uniform_id FROM so_cadet_uniform as cxu WHERE c.id=cxu.cadet_id)
';
echo "cadets without uniforms\n";
foreach( $pdo->query($query, PDO::FETCH_ASSOC) as $row ){
echo join(', ', $row), "\n";
}
function setup($pdo) {
$pdo->exec('
CREATE TEMPORARY TABLE so_cadets (
id int auto_increment,
name varchar(32),
primary key(id)
)
');
$pdo->exec('
CREATE TEMPORARY TABLE so_uniforms (
id int auto_increment,
labelid varchar(32),
primary key(id),
unique key(labelid)
)
');
$pdo->exec('
CREATE TEMPORARY TABLE so_cadet_uniform (
cadet_id int,
uniform_id int,
primary key(cadet_id,uniform_id)
)
');
$stmt = $pdo->prepare('INSERT INTO so_cadets (name) VALUES (?)');
foreach( range('A','F') as $c ) {
$stmt->execute( array('cadet'.$c));
}
$stmt = $pdo->prepare('INSERT INTO so_uniforms (labelid) VALUES (?)');
foreach( range('K','Z') as $c ) {
$stmt->execute( array('label'.$c));
}
$stmt = $pdo->prepare('INSERT INTO so_cadet_uniform (cadet_id,uniform_id) VALUES (?,?)');
$cadetHasUniforms = array(
1=>array(1,2,3), // <- cadetA
2=>array(7,9), // <- cadetB
3=>array(8,5,4), // <- cadetC
// 4=>array() <- cadetD, no entry, no uniforms
5=>array(10,13,15) // <- cadetE
// <- cadetE, no entry, no uniforms
);
foreach( $cadetHasUniforms as $cadetId=>$uniformIds ) {
foreach( $uniformIds as $uid ) {
$stmt->execute(array($cadetId, $uid));
}
}
}
印刷品
uniforms of cadetB
cadetB, 7, labelQ
cadetB, 9, labelS
cadets without uniforms
cadetD
cadetF
关于php - PHP遍历数组以从数据库中获取每个键的值(第三种形式),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13286991/