Show image when condition is fulfilled

I have made a simple form with two checkboxes.
if the checkbox is checked, then the value “yes” is stored in a database

Now I want to display a table with all results (from the form) like the code below will provide :

$result = mysql_query(“SELECT * FROM register”);
$test = mysql_query(“SELECT * FROM register”);
while($row = mysql_fetch_array($result))
{
echo("




Coelestis: ".$row['coelestis']."
Xanthops: ".$row['xanthops']."
"); }

Using this code, I get following table displayed : (example)

coelestis: yes
xanthops:

coelestis:
xanthops: yes

But instead of “yes”, I would like to show an image next to coelestis and xanthops. For example show image1.gif if the value is yes and else show image2.gif.

Can someone help me to modify the code I have given above to obtain this ? I don’t know exactly how to insert if/else statements into this code.

Well, first I did some reformatting (indenting mostly) and put your code between [php] tags, to enable syntax highlighting. Makes it easier for all of us :wink:

Next: why are you using two MySQL queries? First you put the result in $result, then you put it in $text. I’m not sure if you’re going to use the second one later on, but I just thought I’d point out that it’s not common.

[php]
$result = mysql_query(“SELECT * FROM register”);
$test = mysql_query(“SELECT * FROM register”);
while($row = mysql_fetch_array($result)) {
echo("
















Coelestis: “.$row[‘coelestis’].”
Xanthops: “.$row[‘xanthops’].”
");
}
[/php]

As for control flow, if you’re planning on becoming a scripter or programmer, you’re going to have to learn about the different control flow structures. Here’s a page on control flow structures in PHP. Be sure to study and practice :wink:

As for if statements:

[php]
if ($expression == true) {
// do conditional stuff here
}
[/php]

So the following should get you on your way:

[php]
if ($row[‘coelestis’] == “yes”) {
$row[‘coelestis’] = “image1.gif”;
} else {
$row[‘coelestis’] = “image2.gif”;
}
[/php]

Thank you very much Zyppora, that is exactly the information I needed. It is working now !

Concerning the two queries : The second is indeed necessary, that was a “leftover” from other trials…

If the code below is executed, I get a table

naam : naam1
voornaam : voornaam1

naam : naam2
voornaam : voornaam 2

This table displays ALL inputs that have been made via a form into a database. This means that if I have for instance 100 inputs, I will have one very long page with a table containing the 100 inputs.
Now I would like to split the resulting table so that only the first 10 inputs (e.g. input 1 to 10) are displayed from the database.
Then I would like to create another page that displays the following 10 inputs (e.g. input 11 to 20), then a next one (21-30),…

Can someone help me with this ??

$result = mysql_query(“SELECT * FROM register”);

while($row = mysql_fetch_array($result))
{

echo("

 
Naam:
".$row['naam']." ".$row['voornaam']."
");

}

You could try the LIMIT clause in the SELECT query.

Yes, that would probably work.
But I have found another way : in my database table I have added a field (id) that is set to auto-increment.
So when a new entry is made to the table, id is incremented automatically.

I then have changed the SELECT query as follows :

$result = mysql_query(“SELECT * FROM register where id <= 10”);

Now the resulting table lists only the entries with id’s 1 to 10, i.e. the first 10 entries.

for the next page :

$result = mysql_query(“SELECT * FROM register where id > 10 AND id <= 20”);

this displays entries 11-20

etc…

Thank you for your help !

That becomes faulty when a database entry is deleted somewhere (or when an additional condition is to be met in the WHERE clause). Under the banner of ‘using the right tool for the right job’, I’d suggest using LIMIT.

You are right again !
LIMIT seems indeed to be the best way .

$result = mysql_query(“SELECT * FROM register LIMIT 0,10”);
for first 10 entries

$result = mysql_query(“SELECT * FROM register LIMIT 10,10”);
for entries 11 -20

$result = mysql_query(“SELECT * FROM register LIMIT 20,10”);
for entries 21-30
etc…

correct ?

Yep, that’s correct :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service