Jump to content

If mysql field = yes how to display image?


MsKazza
Go to solution Solved by MsKazza,

Recommended Posts

I am creating a site for a restaurant directory, on the listing form users can select from different options, e.g. takeout, parking, tv, disabled, etc via checkboxes. How can i make it on the listings page so that if the mysql field = y it displays the appropriate image rather than text?

e.g. if takeout=y

then display takeout.gif

if takeout=n

then no image to display.

I want all the images to appear in a line along the end of the listing so site visitors can see the features offered by the restaurants.

 

if field = no, i don´t want it to display an image at all, how can i do that without leaving a big space. In other words i only want it to display pics for fields that have a Y in them

 

i have just tried it using the following:

<td><? if (vTakeout='y')?>

<img src="takeoutyes.jpg">

<? else if (vTakeout='n')?>

<img src="takeoutno.jpg">

<? end if?> </td>

 

and get this error:

Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\Diningout\site... on line 223

 

i have also tried the following, but nothing happens.

<td><? if (vTakeout=='y') {?>

<img src="takeoutyes.jpg">

<? } elseif (vTakeout=='n') { ?>

<img src="takeoutno.jpg">

<? } ?> </td>

 

i know that the page is reading the fields correctly as i´ve put in the field in text format to show y or n

 

thanks in advance for your help :)

 

MsKazza

Link to comment
Share on other sites

I´m running it on my local machine whilst it´s in development using Xampp, i´m not sure how to turn on all error reporting.

 

At the moment i have the code as such:

<td><? if ($vTakeout=='y') {?>

<img src="takeoutyes.jpg">

<? } elseif ($vTakeout=='n') { ?>

<img src="takeoutno.jpg">

<? } ?> </td>

 

but all that happens is i get a white space on the page, i have it in text format below, so it´s picking up the db okay.  I´m using Dreamweaver and Xampp

Link to comment
Share on other sites

  • 9 years later...

You REALLY need to learn to write your code in a better, more readable fashion.

 

(assuming you are in php mode)
if ($vTakeout=='y')
{
echo "<img src='takeoutyes.jpg'>";
}
elseif ($vTakeout=='n')
{
echo "<img src='takeoutno.jpg'>";
}
(still in php mode.)

 

OTOH - you can make this easier by doing your logic (the above) before you begin doing your presentation (html) output. Use the above code to set a variable instead and then include that variable inside all the html you will output at the end of the script.

 

Keeping your php code separate from the html/js code as much as possible is to be strived for. Mixing it up makes for a nightmare when doing modifications later on or in simply reading thru the code.

 

if ($vTakeout=='y')
$takeout_img = 'takeoutyes.jpg';
else
$takeout_img = 'takeoutno.jpg';

 

 

Down below in your script where you ouput the entirety of your html just insert this variable. Use the heredocs operator to make this all easier.

 

(in php mode)
$code=<<<heredocs
....
...
html header code
---
---
<header>
...
...

..

</header>
..
<body....>
...
...
... lines of html....
...
...
<img src="$takeout_img">
...

...

.... end of html...
heredocs;
echo $code;
Link to comment
Share on other sites

While we're at it: Representing truth values with “y” and “n” is a bad idea. While this may make perfect sense for a human, it doesn't make any sense for the database system. From a technical perspective, it's just arbitrary text, so if a buggy application or confused human instead stores “yes”/“no” or “1”/“0” or “yea”/“nay”, this is accepted as well, and now it's no longer clear whether the value is true or false.

 

Use the BOOLEAN type for an unambiguous representation of booleans.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.