0

I have written the following IF statement

if ($mainCat && $subCat) {
    $url = "/subjects/".$mainCat->alias."/".$r->alias;
}

Which is working great. You can see a demo here: http://217.199.187.199/equityschooltravel.co.uk/

However I have expanded on it and want to include an elseif, if a variable is present but it's not working:

if ($subCat == 78) {
    $url = "/subjects/".$mainCat->alias."/".$subCat->alias."/".$r->alias;
}
elseif ($mainCat && $subCat) {
    $url = "/subjects/".$mainCat->alias."/".$r->alias;
}

The idea is, if a country ($subcat) is selected with an ID of '78' the URL will be rendered differently.

8
  • 1
    Can you please explain what does 'not working' mean? Do you get any errors? Is the result not expected? Commented Sep 9, 2015 at 11:46
  • Considered isset(); or empty(); although I might have misunderstood your question Commented Sep 9, 2015 at 11:47
  • 1
    this is obviously failing you if ($subCat == 78) since the first piece of code contains the same but set in your elseif Commented Sep 9, 2015 at 11:49
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. Commented Sep 9, 2015 at 11:49
  • @Andrew I've just updated the question - thanks Commented Sep 9, 2015 at 11:51

3 Answers 3

1

if $maincat == 78, you'll never enter in else if. check for not existing subcat in if and then if the subcat is set you'll go for the else if.

if ($main && !isset($subcat)) {
    $url = "/subjects/".$mainCat->alias."/".$r->alias;
}
elseif ($mainCat && $subCat) {
    $url = "/subjects/".$mainCat->alias"/".$subCat->alias."/".$r->alias;
}
else ....
Sign up to request clarification or add additional context in comments.

Comments

1

There is no error....

or may be i misunderstood your question:

<?php

$url = "";

$subCat = 70; // 78

$mainCat = 1;

if ($mainCat && $subCat == 78) {
    $url = "subcat 78";
}
elseif ($mainCat && $subCat && $subCat != 78) {
    $url = "url other";
}

echo $url;

?>

Comments

0

In the end, I fixed it by doing:

$diffCats = array('76','77','78','79','80');

if ($mainCat && $subCat) {
    $url = "/subjects/".$mainCat->alias."/".$r->alias;

    if (in_array($subCat->id, $diffCats)) {
        $url = "/subjects/".$mainCat->alias."/".$subCat->alias."-language-trips/".$r->alias;
    }

}

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.