1

forgive me I just learned php this week, so I'm not sure I'm doing this all right.

It starts out accessing the DB, and the categories table for headers; it then takes that info and creates the header, pricing, and catalog links.

Then within that while loop after it completes the first part it's supposed to run a second while loop to access the products table to list all the products with the category_id that matches the cat_id from the categories table.

When it prints out it should be

Header
Pricing PDF
Item Dimensions Image Image
Item Dimensions Image Image
Item Dimensions Image Image
etc

Header
Pricing PDF
Item Dimensions Image Image
etc....

And so far the first while loop works but the second isn't. Is there a correct way to pass the variable? Can I just not access a second table while in a while loop for the first table? I dunno...I've tried a few things, and nothing is working well

<?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);
?>

Thanks in advance

4

2 に答える 2

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);
?>
于 2012-07-20T21:10:06.737 に答える
0

相対データをロードしているので、結合でそれらを選択するとおそらくオーバーヘッドが発生するため、短い答えはノーです。

また、すべてではなく必要なものだけを選択して、* によって作成されるオーバーヘッドを制限するようにしてください。

また、新しい mysql(i) 呼び出しについては、Truth の推奨に従ってください。sql と php に慣れたら、他のタイプのデータベース呼び出し (オブジェクト指向コードまたはアクティブレコードを使用) に移行できますが、最初に mysql 呼び出しがどのように行われるかを理解するようにしてください。

于 2012-07-20T21:10:42.750 に答える