PHP - 循环查询

时间:2016-10-17 07:49:10

标签: php foreach

我正在尝试建立一个网站,当你点击应用程序时,它会打开它并且它现在正常工作,但是打开每个应用程序,这显然是我不想要的。

我相信我需要放一个foreach loop so for every application it puts a different $ appLocation`吗?

这对我来说只是第一个项目,所以也许有人可以指出我正确的方向。

<?php

    $appQuery = "SELECT app_name, app_location, app_status, app_image FROM applications";
    $select_posts = mysqli_query($conn, $appQuery);

    if ($result = mysqli_query($conn, $appQuery)) {

        /* fetch associative array */
        while ($row = mysqli_fetch_assoc($result)) {

            $appName = $row['app_name'];  // List Application Name
            $appLocation = $row['app_location']; // List Application Location
            $appStatus = $row['app_status']; // List Application Status - 1 = Enabled / 0 = Disabled
            $appImage = $row['app_image']; // List Application Image Locations
?>


            <!-- Tile with image container -->
            <div class="tile">
                <div class="tile-content">
                    <div class="image-container">
                        <form method="post">
                            <div class="frame">
                                <button name="appButton"><img src="<?php echo $appImage ?>"></button>
                            </div>
                        </form>
                        <?php
                            if (isset($_POST['appButton'])) {
                              exec("start $appLocation");
                            }
                        ?>
                    </div>
                </div>
            </div>
<?php
        }
?>

1 个答案:

答案 0 :(得分:1)

你可以试着记住我之前的评论

<?php

    $appQuery = "SELECT app_name, app_location, app_status, app_image FROM applications";
    if ( $result = mysqli_query( $conn, $appQuery ) ) {

        /* fetch associative array */
        while ($row = mysqli_fetch_assoc($result)) {
            $appName = $row['app_name'];  // List Application Name
            $appLocation = $row['app_location']; // List Application Location
            $appStatus = $row['app_status']; // List Application Status - 1 = Enabled / 0 = Disabled
            $appImage = $row['app_image']; // List Application Image Locations


?>

        <!-- Tile with image container -->
        <div class="tile">
            <div class="tile-content">
                <form method="post">
                    <div class="image-container">
                    <?php
                        $bttn = 'appButton_'.$appName;
                        echo "
                            <div class='frame'>
                                <button name='{$bttn}'><img src='{$appImage}' /></button>
                            </div>";
                    ?>



                </form>
                        <?php
                            if (isset($_POST[ $bttn ])) {
                              exec("start $appLocation");
                            }
                        ?>
                </div>
            </div>
        </div>