0

Something is wrong with my code. No matter what the $_SERVER['REQUEST_URI'] value is, gkPageContent is always displayed.

if (strpos($_SERVER['REQUEST_URI'], "online") >= 0) {
    $left_col_class = "gkPageContent";
}
elseif(strpos($_SERVER['REQUEST_URI'], "student-login") >= 0)  {
    $left_col_class = "showme";
    echo "this is not sanjay";
}
elseif(strpos($_SERVER['REQUEST_URI'], "my-account/orders") >= 0)  {
    $left_col_class = "showme";
}
elseif(strpos($_SERVER['REQUEST_URI'], "my-account/my-courses") >= 0)  {
    $left_col_class = "showme";
}
elseif(strpos($_SERVER['REQUEST_URI'], "my-account/fourm") >= 0)  {
    $left_col_class = "showme";
}
elseif(strpos($_SERVER['REQUEST_URI'], "student-login") >= 0)  {
    $left_col_class = "showme";
}
elseif (strpos($_SERVER['REQUEST_URI'], "student-login") >= 0)  {
    $left_col_class = "showme";
}
else{
    echo "dam thing";
}

echo $left_col_class;

Here's a demonstration.

Why isn't strpos() evaluating the string as intended?

  • 2
    Well it's not utilising the Joomla API for starters. Are you getting any errors with the code? If not try setting error reporting in the Joomla Global Configuration to development – Lodder Sep 28 '17 at 17:27
  • Adding an explanation of what you are trying to do and what's not working might help. – Neil Robertson Sep 28 '17 at 23:13
1

Strpos returns false if a value is not found. False is treated the same as 0. That means >= 0 will always be true.

Change that to != false and it should work.

That said, Lodder's comment is correct - you should be using Joomla's api and framework for a Joomla site, assuming you are posting in the correct forum (and if not, you might want to look at switch instead of lots of messy elseifs).

Getting the itemid will probably do what you want.

0

Your battery of conditionals, I'm sorry to say, is bloated, redundant, and inefficient. Whenever I see a long-winded if-elseif-else or case-switch block, I recommend a lookup array. A lookup array will be far more compact, easier to read/maintain, and requires far less function calls.

Here's how you generate the lookup:

  1. Isolate the needle value that you'll be searching for and use these unique values as keys.
  2. Isolate the value(s) that you will want to access upon finding a match.

For simplicity, I'm going to omit the mention of "this is not sanjay" because it will add complexity to the lookup array structure and over-complicate my demonstration.

$lookup = array(
              'online' => 'gkPageContent',
              'student-login' => 'showme',
              'my-account/orders' => 'showme',
              'my-account/my-courses' => 'showme',
              'my-account/fourm' => 'showme'       // check your spelling
          );

See, this is clean and compact and if you accidentally duplicate a key-value pair then only one will be retained because php array keys must be unique.

Now to use the lookup, you only need to call upon the speedy isset() function one time like this:

if (isset($lookup[$_SERVER['REQUEST_URI']])) {
    $left_col_class = $lookup[$_SERVER['REQUEST_URI']];
} else {
    $left_col_class = $default_fallback_value; // ..."darn thing" would be unhelpful, me thinks
}

Finally, if you need to access multiple values from a key match, then instead of assigning strings like gkPageContent and showme, you can assign an array to each key like: array('class' => 'gkPageContent', 'note' => 'Sanjay') or array('class' => 'showme', 'note' => 'Not Sanjay') and access them respectively like: $lookup[$_SERVER['REQUEST_URI']]['class'] and $lookup[$_SERVER['REQUEST_URI']]['note'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.