dp19001 2015-04-09 14:56
浏览 112
已采纳

mysqli循环中的php循环

I have one mysql table which contains 2 columns and looks like this...

-------------------------------------------
| sku                 | superseded_sku    |
+---------------------+-------------------+
| part1               | part2             |
| part2               | part3             |
| part3               | part4             |
| part5               | part6             |
| part6               | part7             |

the table basically shows where products sold by a company have been replaced by something newer. My task is to reshape the table to resemble the following...

-------------------------------------------
| sku                 | superseded_sku    |
+---------------------+-------------------+
| part1               | part4             |
| part2               | part4             |
| part3               | part4             |
| part5               | part7             |
| part6               | part7             |

Thus eradicating the need to shuffle through the list to find the current replacement.

I have written the following which has one basic downfall in that if a product had been replaced more times that I have allowed for in my code then it wouldn't look that far. I have tried many times to include a while loop, a do {} while loop and a foreach loop but so far I've failed massively.

Here's my code as it stands

<?php    
    // Error reporting on

    ini_set("display_errors", 1);
    ini_set("error_reporting", E_ALL);

    // variables

    // Page variable
    $P = $_REQUEST["p"];

    // variables for latest BritPart list

    $dbhost = 'localhost';
    $dbuser = 'userame';
    $dbpass = 'password';
    $dbselect = 'superseded';

    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbselect);
    if (mysqli_connect_error()) {
        die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
        }

    // Start throwing data about  
    $compare = "SELECT * FROM mar15 ORDER BY sku";

    echo "<h3 style=\"text-align: center;\">Supersedes Tables</h3>
    <div class=\"output\" style=\"width: 940px;\">
    <table cellspacing=\"0\" class=\"db-table\" style=\"width: 940px; border-radius: 5px 5px 0 0\"><thead><tr>
    <th width=\"398\">sku</th>
    <th width=\"270\">superseded_sku</th>
    <th width=\"270\">supersede_length</th>
    </tr></thead>
    <tbody>";

    if ($result = mysqli_query($mysqli,"$compare")) {
        while($row = mysqli_fetch_array($result)) {

            $sku = $row['sku'];
            $ss1 = $row['superseded_sku'];
            $ssl = "1";

    // ---------------------------------------------------------------
    // I want to loop through this section until it doesn't find a match
    // ---------------------------------------------------------------

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

    // ---------------------------------------------------------------
    // Instead I've done this as I don't know how to do what I want
    // ---------------------------------------------------------------

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

            $query1 = "SELECT superseded_sku FROM mar15 WHERE sku = \"$ss1\"";
                if ($result1 = mysqli_query($mysqli,"$query1")) {
                    while($row1 = mysqli_fetch_array($result1)) {
                        $ss2 = $row1['superseded_sku'];
                        if($ss2 != $ss1) {
                            $ssl++;
                            $ss1 = $ss2;
                        }
                    }
                }

    // --------------------------------------------------------------------------------------

        echo "<tr";
            if($ssl > 1) {
                echo " style=\"background: #ff0;\"";
            }
        echo ">
                <td width=\"398\">$sku</td>
                <td width=\"270\">$ss1</td>
                <td width=\"270\">$ssl</td>
                </tr>";
    }
    $result->close();
    }
    echo "</tr></tbody></table><a href=\"#\" class=\"export linkbutton\" style=\"margin: 10px 0 0 0; display: block; text-align: center;\">Export CSV</a></div>";

    mysqli_close($mysqli);

?>

You can see that I've commented where I want a loop to sit, I've no idea how to make one work however, my redneck methods work but I'm not happy with them and wish to improve my skills. Those aren't my real user/pass before anybody asks.

  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 ARfoundation (可能由于没打开摄像头的)黑屏问题
      • ¥15 C++类模板继承模板本身,有偿
      • ¥15 pycharm打包项目包含mp3文件,运行exe程序后mp3文件没有执行。
      • ¥15 Quantlib求discount factors
      • ¥15 JS宏提取满足比率的字符区段
      • ¥20 webapi项目的XUnitTest单元测试怎么注入token和自写拦截器(filter)(语言-c#)
      • ¥15 exe和xml必须在一个文件夹才能打开exe文件
      • ¥15 房号如何排序,sql或算法
      • ¥15 macOS使用IDEA 2022.2.4打包JDK6项目报错
      • ¥20 使用matlab进行含参数的最优化求解及数值模拟