Need to load an image based on a database table status

They have: 2 posts

Joined: Jan 2007

I have a grid of 50 lots.

Each lot is a seperate image. They are all unsold so each one is white. However, when someone buys a lot another database guy is going to make a form that will change the status of a table in the database to show that the lot is now either reserved or sold.

If the lot is reserved, the lots image needs to change from white to gray. If the lot is sold, the image needs to turn to red.

How can I accomplish something like this. I can dabble with PHP a little, but nowhere near this scale.

He has: 1,758 posts

Joined: Jul 2002

It should be a case of simple conditionals... Firstly, ensure that your image has a transparent background. Your basic markup (when the image is white) would look like this:

<img src="blah.gif" with="50" height="50" style="background-color: white;" >'

So... assuming that you already have the status from the database and you have a choice of three values, you simply do the following:

<?php
if($status == 'sold') {

 
$color = 'red';

} elseif(
$status == 'reserved') {
 
 
$color = 'gray';

} else {

 
$color = 'white';

}
?>

Then modify your markup for the image to:

<img src="blah.gif" with="50" height="50" style="background-color: <?= $color ?>;" >'

(obviously, this is all assuming that you are looping through the database to pull out each image)

Andy

They have: 2 posts

Joined: Jan 2007

No, I have very little database coding experience. I had planned on just having sperate images for each lot status.

I could figure out that its just simple if then's.. problem is, I don't know how to query the database to check the status.

I was thinking something like this. I know its not pretty or neat.. but I need to get this done tonight.

If status of lot 1 = open
then display lot1open.gif

elseif status of lot 1 = reserved
then display lot1reserved.gif

else status of lot 1 = sold
then display lot1sold.gif

Then just do that for EACH lot.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

This is a little difficult to answer without knowing how the database is setup. Are the lots held in one table with the status in another?

To query the database you probably want to do something like:

<?php
$conn
= mysql_connect(\"host\",\"username\",\"password\");

if (!
$conn) {
   die(\"Could not connect to DB. Error: \" . mysql_error();
}

$db = mysql_select_db(\"databasename\");

$query = \"SELECT lotname, statusname
   FROM lot
   INNER JOIN status ON lot.statusid = status.statusid\";
$result = mysql_query($query);

while (
$row = mysql_fetch_array($result)) {
   echo '<img src=\"' .
$row[\"lotname\"] . $row[\"lotstatus\"] . '.gif\" width=\"50\" height=\"50\" alt=\"' . $row[\"lotname\"] . ' is ' . $row[\"lotstatus\"] . '\" />';
}
?>

Note: the 'host' in mysql_connect is usually localhost.

I can't really test this so you might find some problems, it does also require you to fill-in a few details, such as: the host, username/password, table names in the SQL etc.

Hopefully this will give you a start. Smiling

a Padded Cell our articles site!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.