Database Query results

Hi.

I just wrote the below code to run and show results from a database. The results show one above the other down the left side of the page. I would like them to show side by side , 3 or 4 then start a new row , until all results have been shown. Is this possible with CSS or do i have to go learn how to do it in php. I am able to style the results into boxes , but as i see it the style applies to all results rather than each result so cant see a way to seperate each box. Been wrestilng with this for ages.

Thanks amd appreciate any help.

Search.php

if (isset ($_POST['search'])) {
     $search = $_POST['search'];
     $search = "%" . $search . "%"; // MySQL wildcard % either side of search to get partially matching results

                                   //  No wildcard if you want results to match fully
}



$stmt = $conn->prepare("SELECT * FROM whisky_results WHERE name LIKE :name ORDER BY name ASC"); // Use = instead of LIKE for full matching
$stmt->bindParam(':name', $search);  
$stmt->execute();
$count = $stmt->rowCount(); // Added to count no. of results returned

if ($count >= 1) { // Only displays results if $count is 1 or more

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

    echo "<br>";
echo "<div class=\"results\">";
    echo "<div class=\"result_name\">";
            echo "<b>Whisky Name:</b><br>";
            echo $row['name'];
    echo "</div>";

        //echo "<div class= \"result_description\">";
            //echo "Whisky Description:  ";
            //echo $row['description'];
        //echo "</div>";

    echo "<div class= \"result_highprice\">";
            echo "Highest Price Paid: £";
            echo $row['highest_price'];
    echo "</div>";
            
    echo "<div class= \"result_lowprice\">";
            echo "Lowest Price Paid: £";
            echo $row['lowest_price'];
    echo "</div>";

    echo "<div class= \"average_price\">";
            echo "Average Price Paid: £";
            echo $row['average_price'];
    echo "</div>";

    echo "<div class= \"last_price\">";
            echo "Last Price Paid: £";
            echo $row['last_price'];
    echo "</div>";


    echo "<div class= \"last_date\">";
            echo "Last date sold: ";
            echo $row['last_date'];
    echo "</div>";

echo "</div>";
    echo "<br>";

}

} else {
      echo " Sorry no records were found";
}

In terms of styling, we need you to post the actual HTML output, along with any CSS you have. The PHP parts (back end) are irrelevant to styling (front end).

I would like them to show side by side , 3 or 4 then start a new row , until all results have been shown. Is this possible with CSS

Yes, that’s the job of CSS. There are various ways to do that — even some cool new ways. :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.