原谅我这周我刚刚学了 php,所以我不确定我是否做得很好。
它开始访问数据库和标题的类别表;然后它获取该信息并创建标题、定价和目录链接。
然后在它完成第一部分之后的那个while循环中,它应该运行第二个while循环来访问产品表以列出所有具有与类别表中的cat_id匹配的category_id的产品。
当它打印出来时,它应该是
标头
定价 PDF
项目尺寸 图片 图片
项目尺寸 图片 图片
项目尺寸 图片 图片
等标头
定价 PDF
项目尺寸图像图像
等....
到目前为止,第一个 while 循环有效,但第二个循环无效。是否有正确的方法来传递变量?我可以在第一个表的 while 循环中不访问第二个表吗?我不知道......我已经尝试了一些东西,但没有什么效果很好
<?php
//connect to server
$con = mysql_connect('localhost','username','password');
//test connection
if (!$con)
{
die ('Could not connect: ' . mysql_error());
}
//access primary DB
mysql_select_db("main_db", $con);
//place table into variable
$categories = mysql_query("SELECT * FROM categories");
//begin table build
while($row = mysql_fetch_array($categories))
{
//set shading variable
$table_row = 0;
//set current set
$cur_set = $row['cat_id'];
//create document link and header
echo "<a name='" . $row['cat_name'] . "'><h3>" . $row['cat_title'] . "</h3></a>";
//create table and table formatting cell
echo "<table id='productTable'><tr id='tableHead'>";
//table width formattting here
echo "<td style='width:165px;'></td>";
echo "<td style='width:235px;'></td>";
echo "<td style='width:155px;'>";
//link and icons to category catalog
echo "<a href='catalog/" . $row['cat_pdf'] . ".pdf'><img src='data/pdflogo.png' alt='pdf button' /></a>";
//link and icons to category pricing sheet
echo "<a href='catalog/" . $row['cat_pricing'] . ".pdf'><img src='data/pricinglogo.png' alt='pricing button' /></a>";
//finish formatting
echo "</td></tr>";
//place table into variable
$products = mysql_query("SELECT * FROM products WHERE category_id='" . $row['cat_id'] . "'");
//begin table build
while($table = mysql_fetch_array($products));
{
//create up row
echo "<tr id='tr" . $table_row . "'>";
//create first cell
echo "<td>" . $table['prod_name'] . "</td>";
//create second cell
echo "<td>" . $table['prod_dim'] . "</td>";
//create third cell
echo "<td>";
//create third cell, first image
echo "<a href='catalog/" . $table['prod_img1'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
//create third cell, second image
echo "<a href='catalog/" . $row2['prod_img2'] . ".jpg'>" . "<img src='data/jpglogo.png' alt='image button' />" . "</a>";
//finish formatting
echo "</td></tr>";
//cycle row
if ($table_row == 0)
{
$table_row = 1;
}
else
{
$table_row = 0;
}
//end table
echo "</table>";
}
}
//close connection
mysql_close($con);
?>
提前致谢