Alex Young
Alex Young

Reputation: 25

PHP - While Loop Query

I'm trying to make a website that when you click the application it opens it and it works at the moment but opens every single application, which obviously i don't want.

I believe i need to put a foreach loop so for every application it puts a different$appLocation` in?

This is just a first project for me so maybe if someone can point me in the right direction.

<?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
        }
?>

Upvotes: 1

Views: 73

Answers (1)

Professor Abronsius
Professor Abronsius

Reputation: 33813

You could try along these lines bearing in mind my previous comments

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

Upvotes: 1

Related Questions