MsKazza Posted February 11, 2007 Share Posted February 11, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/38074-if-mysql-field-yes-how-to-display-image/ Share on other sites More sharing options...
Jessica Posted February 11, 2007 Share Posted February 11, 2007 variables start with $ $vTakeout is a variable. vTakeout is a string literal. Turn on error reporting to ALL, and you'll get a better error for that. Quote Link to comment https://forums.phpfreaks.com/topic/38074-if-mysql-field-yes-how-to-display-image/#findComment-182260 Share on other sites More sharing options...
MsKazza Posted February 11, 2007 Author Share Posted February 11, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/38074-if-mysql-field-yes-how-to-display-image/#findComment-182283 Share on other sites More sharing options...
MsKazza Posted February 12, 2007 Author Share Posted February 12, 2007 Can anyone pls offer some suggestions??? Quote Link to comment https://forums.phpfreaks.com/topic/38074-if-mysql-field-yes-how-to-display-image/#findComment-182664 Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 Maybe the image is broken - is it in the same folder as the file? Don't use dreamweaver, you're making it harder for yourself to learn in the long run. Quote Link to comment https://forums.phpfreaks.com/topic/38074-if-mysql-field-yes-how-to-display-image/#findComment-182669 Share on other sites More sharing options...
Solution MsKazza Posted October 5, 2016 Author Solution Share Posted October 5, 2016 <? if ($vTakeout=='y') {?><img src="takeoutyes.jpg"><? } elseif ($vTakeout=='n') { ?><img src="takeoutno.jpg"><? } ?> I was missing ; from after the two images. Quote Link to comment https://forums.phpfreaks.com/topic/38074-if-mysql-field-yes-how-to-display-image/#findComment-1538038 Share on other sites More sharing options...
Barand Posted October 5, 2016 Share Posted October 5, 2016 Thanks, I have been worrying about that for the last nine years. Quote Link to comment https://forums.phpfreaks.com/topic/38074-if-mysql-field-yes-how-to-display-image/#findComment-1538042 Share on other sites More sharing options...
ginerjm Posted October 5, 2016 Share Posted October 5, 2016 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; Quote Link to comment https://forums.phpfreaks.com/topic/38074-if-mysql-field-yes-how-to-display-image/#findComment-1538043 Share on other sites More sharing options...
Jacques1 Posted October 5, 2016 Share Posted October 5, 2016 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/38074-if-mysql-field-yes-how-to-display-image/#findComment-1538058 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.