我试图用PHP&我无法理解的MySQL。
我有一个采用以下结构的表(它们是两个表,但在SQL查询中加入时将作为一个表):
id | brand | model
1 | brand1 | model1
2 | brand1 | model2
3 | brand1 | model3
4 | brand2 | modelA
我已经能够让PHP输出以下内容:
brand1 model1
brand1 model2
brand1 model3
brand2 modelA
我真正希望能够做的是将其输出为:
Brand1
Model1
Model2
Model3
Brand2
ModelA
有人有任何想法吗?
这是我目前的代码:
<?php
$result = mysql_query("SELECT cameras.cameraid, cameras.model, brands.brand FROM cameras JOIN brands ON cameras.brandid=brands.brandid WHERE cameras.categoryid='$cat' ORDER BY brands.brand ASC, cameras.level ASC")or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo "<li><a href='camera.php?id=". $row['cameraid'] . "'>" . $row['brand'] . " " . $row['model'] . "</a></li>";
}
?>
答案 0 :(得分:3)
您正在寻找的是具有嵌套foreach
- 循环的二维数组。这将允许您拥有一个看起来像这样的数组
Array (
[brand1] => Array (
[1] => model1
[2] => model2
[3] => model3
)
[brand2] => Array (
[4] => modelA
[5] => modelB
[6] => modelC
)
)
当我们稍后将模型作为每个品牌的列表回应时,我们将此用于我们的优势。第一个数组将包含所有品牌,而每个品牌都是自己的数组 - 创建二维数组,其中包含所有模型(以及产品ID作为该数组中元素的键)。
将每个元素放入while($row = mysql_fetch_array($result))
循环中的数组中,并使用嵌套的foreach
循环来回显它们。
while ($row = mysql_fetch_array($result)) {
// Here we fetch all the results, and put it into a two-dimentional array
// with keys as brandname and the ID of the product
$brands[$row['brand']][$row['cameraid']] = $row['model'];
}
// Then we echo them all out in a list!
foreach ($brands as $key=>$brand) {
// The $key is the name of the brand
echo $key."<ul>";
foreach ($brand as $id=>$model) {
// The $id is the ID of the camera in the table
// The $model is the model-name itself
echo "<li><a href='camera.php?id=".$id."'>".$model."</a></li>";
}
echo "</ul>";
}
此外,为了使用mysql_*
功能,已弃用且不推荐使用mysqli_*
功能,您应考虑转到PDO
或<?php
$server = “SQLINSTANCE”;
$options = array( "UID" => “uname”, "PWD" => “password, "Database" => "HomeSensors");
$conn = sqlsrv_connect($server, $options);
if ($conn === false)
die("<pre>".print_r(sqlsrv_errors(), true));
echo "Successfully connected!<br/>";
$stmt = mssql_init('addSensorData', $conn);
mssql_bind($stmt, '@Pool', 90, SQLINT1, false, false, 3);
mssql_bind($stmt, '@Temp', 80, SQLINT1, false, false, 3);
$proc_result = mssql_execute($stmt);
if( $proc_result === false) {
die( print_r( sqlsrv_errors(), true) );
}
mssql_free_statement($stmt);
?>
以获得更好的功能,持续支持和更好的安全性。
答案 1 :(得分:1)
你应该尝试这样的事情。它使用多维数组对项目进行分组。
<?php
$result = mysql_query("SELECT cameras.cameraid, cameras.model, brands.brand FROM cameras JOIN brands ON cameras.brandid=brands.brandid WHERE cameras.categoryid='$cat' ORDER BY brands.brand ASC, cameras.level ASC")or die(mysql_error());
$brands = array();
while($row = mysql_fetch_array($result))
{
$brands[$row['brand']][] = array($row['model'], $row['cameraid']);
echo "<li><a href='camera.php?id=". $row['cameraid'] . "'>" . $row['brand'] . " " . $row['model'] . "</a></li>";
}
foreach($brands as $brand => $data){
echo $brand . "<br>";
foreach($data as $item){
echo "<li><a href='camera.php?id=" . $item[1] . "'>" . $item[0] . "</a></li>";
}
}
?>
答案 2 :(得分:-1)
下面是网格使用表的代码它将为您提供并了解如何实现您的任务
$sql = mysql_query("SELECT id, member_name FROM member_table ORDER BY id DESC LIMIT 15");
$i = 0;
// Establish the output variable
$dyn_table = '<table border="1" cellpadding="10">';
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$member_name = $row["member_name"];
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dyn_table .= '<tr><td>' . $member_name . '</td>';
} else {
$dyn_table .= '<td>' . $member_name . '</td>';
}
$i++;
}
$dyn_table .= '</tr></table>';
此代码的逐行说明位于以下链接
数组网格输出教程会给你一些想法尝试将此用于你赢得的scnario Array Grid output tutorial well explained video