Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

making a div disappear using a DB value

Status
Not open for further replies.

Hondy

Technical User
Mar 3, 2003
864
GB
Hi

I'm trying to make this div disappear on condition that a value is zero but I'm having trouble with the quotes and the fact there is php in the middle of the code already

basically
Code:
<?php if ($row_rs_images['img2'] == '0') echo ""; else echo "

<div id="item1" onclick="fn_getImage('<?php echo $sideimage4?>')"><a href="#" id="linky"><img src="<?php echo $sideimage4;?>" width="110" height="110" /></a>
            </div>

"

so confused... can anyone help?

Thanks

H
 
Code:
<?php if ($row_rs_images['img2'] != '0){ ?>

<div id="item1" onclick="fn_getImage('<?php echo $sideimage4?>')"><a href="#" id="linky"><img src="<?php echo $sideimage4;?>" width="110" height="110" /></a>
            </div>

<?php } ?>
Note the } on the last line is the close for the if. One of the joys of PHP is the ability to freely break in and out of code and back to HTML it great for things like this.

You could rewrite it such that the DIV is in an echo but then you have to work harder to get all the quotes right. This is easier ;)

Alan
 
So many things wrong there, but basically double quotes inside double quotes equals many syntax errors.
Additionally all your HTML is already inside an echo
statement, making the PHP tags and additional echo statements inside it not only wrong and superfluous but again devastating as they cause even more syntax errors.

Anyway, you have a few choices:

1. Escape all your double quotes except the outer ones obviously. And of course remove the PHP tags and echos inside the outer echo statement. Also for clarity its always a good practice (though not necessary) to have curly braces { } surrounding if and else statement blocks:

if(condition)[red]{[/red] do something; [red]}[/red] else [red]{[/red] do something else; [red]}[/red]

Or

2. Change all inner quotes into single quotes. again removing all PHP tags an echo statements inside.

Or

3. Use a heredoc syntax to echo out your DIV html. Again removing all inner PHP tags an echos.

Or

4. exit out of the PHP tags entirely. To avoid any quoting issues. And then keep the PHP tags and echos where they
are.

I suppose the easiest would be to simply exit the php tags and proceed as normal.

Code:
<?php 

if ($row_rs_images['img2'] == '0') [red]{[/red] echo ""; [red]}[/red] else [red]{ ?> [/red] 
<div id="item1" onclick="fn_getImage('<?php echo $sideimage4?>')"><a href="#" id="linky"><img src="<?php echo $sideimage4;?>" width="110" height="110" /></a>
            </div>
[red]<?PHP } ?>[/red]


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thanks guys for your detailed responses, I'm going to have to revise what you have both said, I'm not a pro programmer so it'll take me a while! :)
 
I would approach it differently

Code:
<?php 
if ($row_rs_images['img2'] > 0){
echo <<<HTML
<div id="item1" onclick="fn_getImage('$sideimage4')"><a href="#" id="linky"><img src="$sideimage4" width="110" height="110" /></a></div>
HTML;
}

this means that you can rely on php's implicit type casting to transform the img2 variable into a number and you are only comparing for whether something is greater than zero.

when you have javascript inside html, or any nested quotes, you will find that using heredoc syntax makes life easier.(as vacunita suggests in his point 3).

although vacunita correctly states that it is not always necessary to use braces around if ... else conditions, in your case it would be. the only time when it is permissible to ignore the braces is where you have a single command under each condition. in your case, with the nested php tags, you have multiple commands. if you transformed to heredoc you could get away with it. But it is bad practice (even though it is recommended style in the wordpress coding standards).

as a matter of cording style I would also not add an onclick event to the html using php but use javascript to bind to the event on window load. you can use the linky class to make the binding easier.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top