Adam Kona Adam Kona - 1 month ago 6
PHP Question

How to store large amounts of images to retrieve with html

At the moment I store details about properties in a database and load them into a html table using php as shown below:

<?php $con=mysqli_connect("localhost:8889","root","root","booksmart_properties");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else{

echo "we connected";
}

if(isset($_POST['submit']))
{
$university = $_POST['university'];
$rent = $_POST['rent']; // i assumed that you name the second Select tag RENT and the Third ROOMTYPE and the last DISTANCE
$roomtype = $_POST['roomtype'];
$distance = $_POST['distance'];
}

// Perform queries
$result=mysqli_query($con,"SELECT * FROM ListedProperties
WHERE PropertyType ='$roomtype' ");
echo "<table border='1'>
<tr>
<th>PropertyType</th>
<th>Description</th>
<th>Rent Price</th>
<th>Location</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['PropertyType'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td>" . $row['RentPrice'] . "</td>";
echo "<td>" . $row['Location'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>


However I was wondering how I would be able set up an image server to store a large number of images so that I could reference one for each property and display an image of it in the results and at a specific size.

Answer

You can do this by adding another column in your database which references the image name. Then insert the image name into that field. Your while() loop has then been amended below to output the image for the row being called. Just make sure the source directory for images is the same for all properties within this loop.

 while($row = mysqli_fetch_array($result))
            {
                echo "<tr>";
                echo "<td>" . $row['PropertyType'] . "</td>";
                echo "<td>" . $row['Description'] . "</td>";
                echo "<td>" . $row['RentPrice'] . "</td>";
                echo "<td>" . $row['Location'] . "</td>";
                echo "<td>" . "<img src=\"images\"" . $row['PropertyImage'] . " alt=\"" . $row['PropertyImage'] . "\" height=\"42\" width=\"42\"></td>"; //$row['PropertyImage'] = Image.png
                echo "</tr>";

or alternatively, you could save the whole path and reference in your loop as above.

Comments