-1

我坚持这个,

我想创建一个包含 10 行的表,每行都有 10 个产品的“相同下拉菜单”。但在每一行中选择了不同的产品。例如在第一行中的第一个产品在第二行中的第二个产品,依此类推。波纹管代码在每一行中显示下拉菜单,但每一行都选择了第一个产品。如何为下一行选择下一个产品?

    <table>
    <tr>
        <td>
            <select>
                <?php
                    $query = "SELECT * FROM table_name WHERE featured=1 LIMIT 10";
                    $result = mysqli_query($connection, $query);
                    while($row = mysqli_fetch_array($result)){
                        echo "<option>".$row["product_name"]."</option>"
                    }
                ?>
             </select>
        </td>
        <td>
            <select>
                <?php
                    $query = "SELECT * FROM table_name WHERE featured=1 LIMIT 10";
                    $result = mysqli_query($connection, $query);
                    while($row = mysqli_fetch_array($result)){
                        echo "<option>".$row["product_name"]."</option>"
                    }
                ?>
             </select>
        </td>
    </tr>
</table>
4

2 回答 2

1
    <? 
    //Only Once call to DB
    $query = "SELECT * FROM tbl WHERE featured=1 LIMIT 10";
    $result = mysqli_query($connection, $query); 
    $pos = 0;
    $currentRow = 0;
    ?>

    <table>
        <tr>
            </td>
            <select>
                <?php

                while($row = mysqli_fetch_array($result)){

                if($pos==$currentRow)
                   echo "<option selected=\"selected\">".$row["product_name"]."                     </option>";
                else
                   echo "<option>".$row["product_name"]."</option>";
                $pos++;

                }

                mysqli_data_seek( $result, 0 );
                $pos = 0;
                $currentRow++;

                ?>
            </select>
            </td>
        </tr>
        <tr>
            </td>
            <select>
                <?php
               while($row = mysqli_fetch_array($result)){

                if($pos==$currentRow)
                   echo "<option selected=\"selected\">".$row["product_name"]."                     </option>";
                else
                   echo "<option>".$row["product_name"]."</option>";
                           $pos++;

                }

                mysqli_data_seek( $result, 0 );
                $pos = 0;
                $currentRow++;
                ?>

            </select>
            </td>
        </tr>
        </table>
于 2012-10-19T05:25:42.020 回答
0

你的第二个while循环

$i=0;    
while($row = mysqli_fetch_array($result)){
$i++;
if($i==2)
    echo "<option selected=\"selected\">".$row["product_name"]."</option>";
else
 echo "<option>".$row["product_name"]."</option>";
    }
    ?>
于 2012-10-19T05:30:25.227 回答