php环境的新手,我想要做的是使用php和MySQL数据库创建一个列表。见下文
-----我的输出要求信息------
主页标题
子标题1(品牌类型的内容= 1)
1111
2222
3333
4444(我希望这些是在同一网页上打开的超链接)
subheading2(brandtype = 2)
5555
6666
7777
8888(我想将这些作为在同一网页上打开的超链接)
我的代码如下
//连接数据库服务器
include 'xxxxxyyyyzzzz.php';
$conn = mysql_connect($db_host,$db_username,$db_password);
mysql_connect($db_host,$db_username,$db_password) or die (mysql_error ());
mysql_select_db($db_database,$conn) or die(mysql_error()); // Select database
echo "<h1>main webpage heading</h1>";
echo "<h3>my subheading</h3>";
// SQL query
$strSQL = "SELECT * FROM table name WHERE brand type='1','Software' ORDER BY serviceName ASC";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
$strName = $row['serviceName'];
$strLink = "<a href = 'person.php?id = " . $row['ID'] . "'>" . $strName . "</a>";
// List link
echo "<li>" . $strLink . "</li>";
// include 'emaild.php';
}
// Close the database connection
mysql_close();
?>
</ul>
</body>
</html>
end of my code
我正在获取列表确定,但必须手动添加子标题(例如echo“
如何调整我的代码,以便在我的品牌类型= 1时打印标题品牌type1,然后在其下面打印出该品牌1的商品,如果没有商品,则不会打印副标题(品牌类型= 1)并转到下一个打印brandtype = 2的子标题并列出其下的内容。
我可以将每个子标题下面的列表项目作为网页的链接,但它不起作用,如何在同一网页上打开这些列表项目以及产品的更多细节,我会使用后退按钮去以前的列表网页...提前谢谢。急
答案 0 :(得分:1)
品牌名称是否在可以阅读的不同表格中?如果不是你可以手动完成,但会更容易(见下文)。如果是这样,请告诉我们它的位置。
SendBrandListings(1, "Brand 1 Header");
SendBrandListings(2, "Brand 2 Header");
function SendBrandListings($brandId, $heading){
echo "<h3>$heading</h3>";
$strSQL = "SELECT * FROM table name WHERE brand type='$brandId','Software' ORDER BY serviceName ASC";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
$strName = $row['serviceName'];
$strLink = "<a href = 'person.php?id = " . $row['ID'] . "'>" . $strName . "</a>";
echo "<li>" . $strLink . "</li>";
}
}
如果您的品牌名称在同一个表格中,请说明字段为brandName
- 您可以将当前循环修改为此(为演示目的而简化)。
首先,您要确保您的结果按品牌FIRST排序,然后按服务名称排序。这是因为我们将按照收到的顺序循环访问服务。这将将类似品牌的项目整理在一起。因此,请确保您的SQL语句的排序参数如下所示:
ORDER BY brandName, serviceName ASC
只需跟踪您显示的最后一个品牌名称,如果它发生变化,请显示新名称。
$c = 0; //Variable to keep count of categories
$lastBrand = ''; //Declare a variable to track the last displayed Brand Name
while($row = mysql_fetch_array($rs)) {
//If the brand name has changed, display it and update the tracking variable
if ($lastBrand != $row['brandName']){
$lastBrand = $row['brandName'];
//If this isn't the first category, end the previous list.
if ($c>0) echo "</ul>";
echo '<h3>'.$row['brandName'].'</h3><ul>';
$c++;
}
$strName = $row['serviceName'];
$strLink = "<a href = 'person.php?id = " . $row['ID'] . "'>" . $strName . "</a>";
echo "<li>" . $strLink . "</li>";
}