0

I would like to hide part of a pages content depending on the url.

at the moment if I typed:

example.co.uk/index.php?brand=apple

then the apple content will show - however there is some default div's on the page that I'd like to hide if no brand was shown.

I was thinking along the lines of:

    <?php if brand==""
echo "<!----- comment out all the default HTML"
/>

default html here

<?php if brand==""
echo "-->"
/>

Would that be the correct way to go about clearing the page if no brand was mentioned in the url?

3
  • 5
    Wouldn't it make more sense to only include the content you want shown rather than trying to hide everything else? Commented Jul 16, 2012 at 8:22
  • 1
    it seems you only want to echo some elements, if brand is not empty, right? Commented Jul 16, 2012 at 8:33
  • that's true - the if statement below works perfect though I just need a statement along the lines of "if nothing is defined for brand show x" that will also not be drawn if brand is defined Commented Jul 16, 2012 at 8:49

5 Answers 5

3

You don't need to comment out the HTML, you simply need to not output the HTML you don't want shown.

<?php if (/* something */) : ?>

    Output HTML for something.

<?php endif; ?>

The "HTML for something" will not be output unless the condition is true...

Sign up to request clarification or add additional context in comments.

7 Comments

Quick q... the if something works perfect to show/hide the content depending on the string but how would I output some html if no brand was selected and then it'd not be drawn if a brand was in the string.. a kind of "if nothing - show x"
Does if (...) { ... } else if (...) { ... } else (...) help?
@user1523591: if(empty($_GET['brand'])) or if(!isset($_GET['brand'])) then {...}
I think I'm struggling for the name of what you reference "nothing" or "empty" as ? is it just empty?
@user Whatever your "non-condition" is. Using if..else if..else, else is your catch-all-if-nothing-else-matched condition. Otherwise, something like if ($brand == false) or whatever is appropriate that matches only the case if no brand was selected.
|
1

You need to be aware, that even if you "hide" your html with comments, it is not really hidden, as first it is still visible in the source code and secondly, you can easily get into troubles, e.g.:

you have one comment <!-- text 1 and another comment inside this <!-- text 2 --> and then want to close the first comment -->.

The closing of text 2 will actually close text 1, etc. So it is really not a clean solution. If you work with PHP, it would be best practice to just print this code in your html, that you DO want to show.

If you have $_GET['brand'] then use if or switch to just echo the content you'd like to show.

EDIT: it seems, that you only want to show some elements, if $_GET['brand'] is not emtpy, right? In that case:

if(isset($_GET['brand'])) {
    // echo your html here...
}

Comments

0

You don't even need the comments; just the PHP if block will be fine:

<?php if ($brand) { ?>

...html - note this section is bounded by PHP, but is not PHP code...

<?php } ?>

Alternatively, store your "default html" in a string, and echo it:

<?php
if ($brand) {
  echo $strHTML;
}
?>

Comments

0

Don't hide what you don't need, show only, what you need:

<?php

switch ( $type ) {

case "apple":
  // apple content
break;

case "peaches":
    // peaches content
break;

...


}

?>

2 Comments

I'm currently using a switch method but my reasoning for my suggestion was that I didn't want to be replacing the same default text in each switch - so I replaced only what was different and left the default in place and thought hiding it would be best
It's never good to <!-- --> hide something. Just don't output it.
0

No, the bit of comment syntax that means "start of comment" and "end of comment" in HTML 4 and XHTML is -- so your syntax has a closed comment on line 1.

You want <!--.

This will break if the HTML contains comments though.

You also need some () for the if.

You are probably best off not including the HTML in the output at all if the match matches.

<?php 
    if (brand != "") {
?>
HTML here
<?php
    }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.