PHP If Query!

Associate
Joined
18 May 2006
Posts
785
OK so I've got a switch working fine and a simple query:

<?php if ($Brand) ?> which will display the code within it if any brand has been entered in the string.. perfect! however I'd like to add in an else show X

for example:

<?php if ($Brand) else echo ('HTML HERE') ?>

but not sure on the correct syntax and can't seem to find it on the net as most examples have if brand echo "code" else echo "code" but I don't need to echo anything for the first instance
 
Why not use switch?

PHP:
<?php
switch ($brand) {
    case 'brand1':
        //Brand 1 stuff here;
        break;
    default:
       echo 'Default Brand Stuff';
}
?>

Then you can list all your valid brand options and a default one if someone tries manipulating the variable.
 
Bear in mind the example you've put at the top will always return true if the $brand variable is defined before it's set with the value. You should do a check like

if($brand !== "") {
// Do something
} else {
// Do something else
}
 
I am using a switch currently.

I'll try to explain better.


The <?php if ($Brand) ?> is working fine - ie - if no brand is written in the url then it doesnt show any of the code between the <?php if ($Brand) ?> and <?php endif; ?> which is great.

If there is a brand in the url it will show the code so that is all good.

All I need is to be able to say if no brand show this code.

I think what I'm struggling to say is how do I code if no brand show X and hide the code that if brand is linked with brand.
 
the problem with:

<?php
if (condition) { do this... }
else { do this... } ?>

is that I don't want to specify something for the first condition to do as its working fine at the moment - I just need to specify what to show for the else -make sense?
 
The <?php if ($Brand) ?> is working fine - ie - if no brand is written in the url then it doesnt show any of the code between the <?php if ($Brand) ?> and <?php endif; ?> which is great.

If there is a brand in the url it will show the code so that is all good.

All I need is to be able to say if no brand show this code.

I think what I'm struggling to say is how do I code if no brand show X and hide the code that if brand is linked with brand.

If that split php code is working then try <?php if ($Brand) ?> and <?php else; ?> and <?php endif; ?>

That way you can put code that would show in between the else one.

I'm not sure if it works as I wasn't even sure you could split it like that (I thought it would just display all code outside the php tags).
 
The other reason I don't think I can move the if brand into an echo is because there are php tags in what I need it to show like <?php echo $Heading; ?> which will break the code?
 
Use double quotes then, php will parse variable names and replace them when you use double quotes to echo something. That's why if you don't need anything replaced and are echo'ing something simple like 'Hello' then you use single quotes.

<?php else; echo 'HTML HERE'?>

is this the right way to write it? I'll give it a go


No I meant

<?php if ($Brand) ?>
HTML Code here
<?php else; ?>
HTML Code here
<?php endif; ?>

I'll try and see if I can make a working example.
 
Thanks I guess I need this at the end also? <?php endelse; ?> actually that doesn't seem correct - how do you end a else?
 
Don't use switch, it's nasty.

PHP:
<? 
$brands = array(
  'a' => '<div>some html for brand a</div>',
  'b' => '<div>some html for brand b</div>'
);

if (isset($_GET['brand']) && isset($brands[$_GET['brand']])) {
  echo $brands[$_GET['brand']];
} else {
  echo '<div>default html for unset/unknown brand</div>';
}

?>
 
done it I was referencing a part wrong.. just 2 queries.

1, how to I get the php to execute when it comes from a csv file ie
www.'.$brand_to_use.'.net

and can a array have different cases like a switch ie:

$recognised_brands = array(
switch($brand) {
case "apple":
$brandname ='Apple';
$brandurl ='www.apple.co.uk';

would that work?
 
Back
Top Bottom