schwarzenhut
schwarzenhut

Reputation: 3

PHP hide data IF mysql string empty

I have some PHP that's pulling MySQL entries into an HTML table. The table headers are still visible when there are no results provided by the query so I wanted to use an IF function to display an alternate message on the front end.

My so far limited knowledge of PHP means I'm struggling to find any way to check the results. Every method I've come across online had been a dead end.

Can anybody see what I'm missing please?

$result = mysqli_query($con,"SELECT * FROM stock WHERE (SKU='$SKUsearch1' AND Location='London Store') OR (SKU='$SKUsearch2' AND Location='London Store') OR (SKU='$SKUsearch3' AND Location='London Store') OR (SKU='$SKUsearch4' AND Location='London Store') OR (SKU='$SKUsearch5' AND Location='London Store') OR (SKU='$SKUsearch6' AND Location='London Store') OR (SKU='$SKUsearch7' AND Location='London Store') OR (SKU='$SKUsearch8' AND Location='London Store') OR (SKU='$SKUsearch9' AND Location='London Store') OR (SKU='$SKUsearch10' AND Location='London Store') OR (SKU='$SKUsearch11' AND Location='London Store') OR (SKU='$SKUsearch12' AND Location='London Store') ");


if (!$row['Location']) {

                echo "<table class='availableTable' border='1'>
                <tr>
                <th>Location</th>
                <th>Item</th>
                <th>Availability</th>
                </tr>";

                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
                {
                echo "<tr>";
                echo "<td>" . $row['Location'] . "</td>";
                echo "<td>" . $row['Name'] . "</td>";

                if(($row['Available']) > 0) {
                echo "<td> In Stock </td>";
                }
                else {
                echo "<td> Out of Stock </td>";
                }

                echo "</tr>";
                }
                echo "</table>";




}

else {

        echo "<div class='availableText'>No instore stock information is currently available for this product.</div>";

}

Upvotes: 0

Views: 59

Answers (1)

Krish
Krish

Reputation: 387

$row['Location'] $row is not available before while loop

$result = mysqli_query($con,"SELECT * FROM stock WHERE (SKU='$SKUsearch1' AND Location='London Store') OR (SKU='$SKUsearch2' AND Location='London Store') OR (SKU='$SKUsearch3' AND Location='London Store') OR (SKU='$SKUsearch4' AND Location='London Store') OR (SKU='$SKUsearch5' AND Location='London Store') OR (SKU='$SKUsearch6' AND Location='London Store') OR (SKU='$SKUsearch7' AND Location='London Store') OR (SKU='$SKUsearch8' AND Location='London Store') OR (SKU='$SKUsearch9' AND Location='London Store') OR (SKU='$SKUsearch10' AND Location='London Store') OR (SKU='$SKUsearch11' AND Location='London Store') OR (SKU='$SKUsearch12' AND Location='London Store') ");


    if (mysqli_num_rows($result)>0) {

                    echo "<table class='availableTable' border='1'>
                    <tr>
                    <th>Location</th>
                    <th>Item</th>
                    <th>Availability</th>
                    </tr>";

                    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
                    {
                    echo "<tr>";
                    echo "<td>" . $row['Location'] . "</td>";
                    echo "<td>" . $row['Name'] . "</td>";

                    if(($row['Available']) > 0) {
                    echo "<td> In Stock </td>";
                    }
                    else {
                    echo "<td> Out of Stock </td>";
                    }

                    echo "</tr>";
                    }
                    echo "</table>";




        }

        else {

                echo "<div class='availableText'>No instore stock information is currently available for this product.</div>";
    }

try mysqli_num_rows($result) http://php.net/manual/en/mysqli-result.num-rows.php

Upvotes: 1

Related Questions