简体   繁体   English

以正确的顺序减少MySQL查询和输出数据的数量

[英]Reduce number of MySQL Queries and Output Data in Correct Order

My main issue is the number of times I query the database (see below). 我的主要问题是查询数据库的次数(请参见下文)。 Also, I would like to check that the current product (optionsToProducts.productID) has options for the current optionName before outputting the select statement! 另外,在输出select语句之前,我想检查当前产品(optionsToProducts.productID)是否具有当前optionName的选项! See the final image below to see the blank select box... 请参阅下面的最终图像,以查看空白的选择框...

I have 8 tables in total, but the 3 that matter are: 我总共有8张桌子,但重要的3张桌子是:

optionNames optionNames

http://www.grabb.co.uk/stack/001.png http://www.grabb.co.uk/stack/001.png

productOptions productOptions

http://www.grabb.co.uk/stack/002.png http://www.grabb.co.uk/stack/002.png

optionsToProducts optionsToProducts

http://www.grabb.co.uk/stack/003.png http://www.grabb.co.uk/stack/003.png

<?php

            $i=0;

            $optionsquery = "SELECT * FROM optionNames WHERE categoryID = ".$categoryID."";
            $optionsresult= mysql_query($optionsquery) or die(mysql_error());

                while ($optionnames = mysql_fetch_array($optionsresult)) {

                    $i++;
                    $optionname = $optionnames["optionName"];
                    $optionID = $optionnames["optionNamesID"];
                    //echo $optionname."<br />";

                    ?>
                        <label for="option<?php echo $i; ?>"><?php echo $optionname; ?></label>
                        <select name="option<?php echo $i; ?>" id="<?php echo $i; ?>">

                    <?php

                            //$optionvalues = "SELECT * FROM (optionsToProducts,productOptions) WHERE optionsToProducts.productID = ".$productID." AND productOptions.optionNamesID = ".$optionID."";
                            //echo $optionvalues."<br /><br />";
                            $optionvalues = "SELECT * FROM optionsToProducts WHERE productID = ".$productID."";
                            $valuesresult= mysql_query($optionvalues) or die(mysql_error());

                                    while ($optionvals = mysql_fetch_array($valuesresult)) {

                                        $valueName = $optionvals["optionValue"];
                                        $valueID = $optionvals["productOptionsID"];
                                        //echo $valueName."<br />";

                                                $optionfinal = "SELECT * FROM productOptions WHERE productOptionsID = ".$valueID." AND optionNamesID = ".$optionID."";
                                                $finalresult= mysql_query($optionfinal) or die(mysql_error());

                                                while ($optionlast = mysql_fetch_array($finalresult)) {

                                                $optionValueName = $optionlast["optionValue"];
                                                $optionValueID = $optionlast["productOptionsID"];   
                                                $num_rows = mysql_num_rows($finalresult);
                                        ?>
                                        <option value="<?php echo $optionValueID; ?>"><?php echo $optionValueName; ?></option> 
                                        <?php
                                        }   
                                    }

                        echo "</select>";

                }

            ?>

final Output: 最终输出:

http://www.grabb.co.uk/stack/004.png http://www.grabb.co.uk/stack/004.png

As always, your help is appreciated. 与往常一样,我们感谢您的帮助。 Thank you. 谢谢。

Since you tagged this question with the join tag, you probably know you need to write a join query to get what you need. 由于您用join标记标记了这个问题,因此您可能知道您需要编写一个join查询来获得所需的内容。

<?php
$i=0;
$query = "SELECT options.optionName, options.optionNamesID, po.optionValue, po.productOptionsID
FROM optionNames AS options
INNER JOIN productOptions AS po ON po.optionNamesID=options.optionNamesID
INNER JOIN optionsToProducts AS otp ON otp.productOptionsID=po.productOptionsID
WHERE otp.productID=" . (int) $productID 
. " AND options.categoryID=" . (int) $categoryID;
$result = mysql_query($query);
if($result) {
    $rows = array();
    while($row = mysql_fetch_assoc($result) ) {
        $rows[] = $row;
    }

    $i = 0;
    $optionId = null;
    foreach($rows as $row) {
        if($optionId != $row['optionNamesID']) {
            $optionId = $row['optionNamesID'];
            ?>
            <label for="option<?php echo $optionId; ?>"><?php echo $row['optionName']; ?></label>
            <select name="option<?php echo $optionId; ?>" id="<?php echo $optionId; ?>">
        <?php } ?>
                <option value="<?php echo $row['productOptionsID']; ?>"><?php echo $row['optionValue']; ?></option> 
        <?php 
        //Close select element when the optionNamesID changes or on the last row
        if( (isset($rows[$i + 1]) && $rows[$i + 1]['optionNamesID'] != $optionId) || 
                !isset($rows[$i + 1]) ) { ?>
            </select>
        <?php }
            $i++;
    }
} else {
    //Debug query, remove in production
    echo mysql_error();
}
?>

I also made some small changes - I use the optionNamesID in the select and label tag names - I don't know how you knew previously which select belonged to which option. 我还做了一些小的更改-我在select和label标记名称中使用optionNamesID-我不知道您以前如何知道哪个选择属于哪个选项。 I also assumed that categoryID and productID came from somewhere, since it's not specified in the code. 我还假定categoryID和productID来自某个地方,因为代码中未指定。

Pushing all the rows to an array at the beginning is optional, but it makes the code a bit more organized (since you can check ahead in the array to see where to close the select tags). 在开始时将所有行都推送到数组是可选的,但这会使代码更加井井有条(因为您可以在数组中预先检查以查看在何处关闭select标签)。

NOTICE - this code is untested so there could some minor typos. 注意 -此代码未经测试,因此可能会有一些小的错别字。 Please make the needed corrections if necessary. 如有必要,请进行必要的更正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM