在while循环中访问第二个表

时间:2012-07-20 21:02:57

标签: php mysql database

请原谅我本周刚刚学会了php,所以我不确定我是不是这样做了。

它开始访问数据库,以及标题的类别表;然后它会获取该信息并创建标题,定价和目录链接。

然后在完成第一部分之后的while循环中,它应该运行第二个while循环来访问products表,以列出category_id与categories表中的cat_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);
?>

提前致谢

2 个答案:

答案 0 :(得分:2)

在两个表上执行INNER JOIN会更加精简

SELECT
    A.cat_id,A.cat_name,A.cat_title,A.cat_pdf,A.cat_pricing,
    B.prod_name,B.prod_img1,B.prod_img2
FROM categories A INNER JOIN products B ON A.cat_id = B.category_id;

您可以迭代A.cat_id

这是我提出的建议(大括号可能会关闭,但这里是cat_id的迭代应该是什么样子)。请更改启动和停止标记的样式。

<?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 A.cat_id,A.cat_name,A.cat_title,A.cat_pdf,A.cat_pricing,B.prod_name,B.prod_img1,B.prod_img2 FROM categories A INNER JOIN products B ON A.cat_id = B.category_id");

$current_catid = -1;

//begin table build
while($row = mysql_fetch_array($categories))
{
        if ( $current_catid != $row['cat_id'] )
        {
            if ( $current_catid > -1 ) { echo "</table>"; }
            $current_catid != $row['cat_id']

    //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>";
        }

//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 (Fix this, might produce extra table tag)
    echo "</table>";
}

//close connection
mysql_close($con);
?>

答案 1 :(得分:0)

您正在加载相关数据,因此简短的答案是否定的,因为如果您使用连接选择它们,可能会有很多开销。

此外,尝试通过仅选择您想要的而不是所有内容来限制*创建的开销。

对于新的mysql(i)调用,请遵循Truth的建议。一旦你开始使用sql和php,你可以转移到其他类型的数据库调用(使用面向对象的代码 - 或activerecord),但请尝试先了解mysql调用是如何完成的。