PHP code within array being displayed as plain text

Hola,
I’ve got a bit of a problem with an array of mine.

[php]
$menu = array(
‘category’ => array(
‘icon’ => ‘fa fa-bars’,
‘title’ => ‘<?php if ($title == constant("my-account") || $title == constant("password") || $title == constant("email-address") || $title == constant("logout")) { echo class=active } ?>’,
‘description’ => ‘<?php constant("account-overview-title"); ?>’,
‘class’ => ‘<?php if ($title == constant("my-account") || $title == constant("password") || $title == constant("email-address") || $title == constant("logout")) { echo class=active } ?>’,
‘links’ => array (
‘icon’ => ‘fa fa-user’,
‘title’ => ‘<?php if ($title == constant("my-account")) { echo "id="current""; } echo "href="'.URLADDR.'account-overview/my-account/""; echo "title="constant("my-account-title");"" ?>’,
‘description’ => ‘<?php constant("my-account") ?>’))
);

foreach ($menu as $section => $items) {
$cIcon = $items[‘icon’];
$cTitle = $items[‘title’];
$cDesc = $items[‘description’];
$cClass = $items[‘class’];
$lIcon = $items[‘links’][‘icon’];
$lTitle = $items[‘links’][‘title’];
$lDesc = $items[‘links’][‘description’];
[/php]

I just want to focus on one part for now - once that’s fixed all the rest will fall in line.
Check out the value of “title” beneath “category” - It’s an if statement that dictates when that particular element should be given the class “active” in order to give the appearance of that category being selected.

Let me just say, the original code works perfectly fine, but I want to use an array because it’s really quite a bit of code just for one menu. An array is the most efficient way to reduce the size of this code.

$cTitle is called a couple lines down, here:
[php]<li <?php var_export($cTitle, TRUE) ?>>[/php]
In browser source, it appears like this:

<li >

I change how $cTitle is introduced:
[php]<li <?php echo $cTitle; ?>>[/php]
In browser source, it appears like this:
[php]<li <?php if ($title == constant("my-account") || $title == constant("password") || $title == constant("email-address") || $title == constant("logout")) { echo class=active } ?>>[/php]

My first thought for a solution was to remove the PHP from the title key, put the PHP back in the foreach loop, and then put the constant values (my-account, password, email-address, etc.) as my values for multiple title keys, but the problem is that other “categories” may have less or more constants than the one I’ve included here.

tl;dr - My array values that contain PHP are outputting plain text instead of the code used to determine one thing or another.

I can provide the full source or a link to a working version online, I just don’t want to make the post too cluttered to see clearly.

Any help is much appreciated.

Thanks,
awl19

I've got a bit of a problem with an array of mine

Yes you do… :o :o

I will have to come back to this when I finish scratching and shaking my head.

Lol, okay - maybe the full source will make more sense.

Original “menu.inc.php”:

[code]




    <li <?php if ($title == constant('my-account') || $title == constant('password') || $title == constant('email-address') || $title == constant('logout')) { echo 'class="active"'; } ?>>
    <h3 title="<?php echo constant('account-overview-title'); ?>" <?php if ($title == constant('my-account') || $title == constant('password') || $title == constant('email-address') || $title == constant('logout')) { echo 'class="active"'; } ?>>

    <?php
    if (strlen(constant(‘account-overview’)) > 25) {
    $menuTitle = substr(constant(‘account-overview’), 0, 24) . ‘…’;
    echo $menuTitle;
    } else {
    echo constant(‘account-overview’);
    }
    ?>



    • <a <?php if ($title == constant('my-account')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>account-overview/my-account/” title="<?php echo constant('my-account-title'); ?>">

      <?php
      if (strlen(constant(‘my-account’)) > 22) {
      $menuTitle = substr(constant(‘my-account’), 0, 21) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘my-account’);
      }
      ?>




    • <a <?php if ($title == constant('password')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>account-overview/change-password/” title="<?php echo constant('password-title'); ?>">

      <?php
      if (strlen(constant(‘password’)) > 22) {
      $menuTitle = substr(constant(‘password’), 0, 21) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘password’);
      }
      ?>




    • <a <?php if ($title == constant('email-address')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>account-overview/update-email/” title="<?php echo constant('email-address-title'); ?>">

      <?php
      if (strlen(constant(‘email-address’)) > 22) {
      $menuTitle = substr(constant(‘email-address’), 0, 21) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘email-address’);
      }
      ?>




    • <a <?php if ($title == constant('logout')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>account-overview/logout/” title="<?php echo constant('logout-title'); ?>">

      <?php
      if (strlen(constant(‘logout’)) > 22) {
      $menuTitle = substr(constant(‘logout’), 0, 21) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘logout’);
      }
      ?>





    <li <?php if ($title == constant('page-list') || $title == constant('page-add') || $title == constant('page-edit') || $title == constant('vendor-list')) { echo 'class="active"'; } ?>>
    <h3 title="<?php echo constant('page-manage-title'); ?>" <?php if ($title == constant('page-list') || $title == constant('page-add') || $title == constant('page-edit') || $title == constant('vendor-list')) { echo 'class="active"'; } ?>>

    <?php
    if (strlen(constant(‘page-manage’)) > 25) {
    $menuTitle = substr(constant(‘page-manage’), 0, 24) . ‘…’;
    echo $menuTitle;
    } else {
    echo constant(‘page-manage’);
    }
    ?>



    • <a <?php if ($title == constant('page-list') || $title == constant('page-edit')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>manage-content/page-list/” title="<?php echo constant('page-list-title'); ?>">

      <?php echo constant('page-list'); ?>




    • <a <?php if ($title == constant('page-add')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>manage-content/page-add/” title="<?php echo constant('page-add-title'); ?>">

      <?php echo constant('page-add'); ?>




    • <a <?php if ($title == constant('vendor-list')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>manage-content/vendor-list/” title="<?php echo constant('vendor-list-title'); ?>">

      <?php
      if (strlen(constant(‘vendor-list’)) > 22) {
      $menuTitle = substr(constant(‘vendor-list’), 0, 21) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘vendor-list’);
      }
      ?>





    <li <?php if ($title == constant('user-list') || $title == constant('user-add') || $title == constant('user-invite')) { echo 'class="active"'; } ?>>
    <h3 title="<?php echo constant('user-manage-title'); ?>" <?php if ($title == constant('user-list') || $title == constant('user-add') || $title == constant('user-invite')) { echo 'class="active"'; } ?>>

    <?php
    if (strlen(constant(‘user-manage’)) > 25) {
    $menuTitle = substr(constant(‘user-manage’), 0, 24) . ‘…’;
    echo $menuTitle;
    } else {
    echo constant(‘user-manage’);
    }
    ?>



    • <a <?php if ($title == constant('user-list')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>manage-users/user-list/” title="<?php echo constant('user-list-title'); ?>">

      <?php
      if (strlen(constant(‘user-list’)) > 22) {
      $menuTitle = substr(constant(‘user-list’), 0, 21) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘user-list’);
      }
      ?>




    • <a <?php if ($title == constant('user-add')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>manage-users/user-add/” title="<?php echo constant('user-add-title'); ?>">

      <?php
      if (strlen(constant(‘user-add’)) > 22) {
      $menuTitle = substr(constant(‘user-add’), 0, 21) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘user-add’);
      }
      ?>




    • <a <?php if ($title == constant('user-invite')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>manage-users/user-invite/” title="<?php echo constant('user-invite-title'); ?>">

      <?php
      if (strlen(constant(‘user-invite’)) > 22) {
      $menuTitle = substr(constant(‘user-invite’), 0, 21) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘user-invite’);
      }
      ?>





    <li <?php if ($title == constant('website-title') || $title == constant('social-media') || $title == constant('access-levels')) { echo 'class="active"'; } ?>>
    <h3 title="<?php echo constant('admin-settings-title'); ?>" <?php if ($title == constant('website-title') || $title == constant('social-media') || $title == constant('access-levels')) { echo 'class="active"'; } ?>>

    <?php
    if (strlen(constant(‘admin-settings’)) > 25) {
    $menuTitle = substr(constant(‘admin-settings’), 0, 24) . ‘…’;
    echo $menuTitle;
    } else {
    echo constant(‘admin-settings’);
    }
    ?>



    • <a <?php if ($title == constant('website-title')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>admin-settings/website-title/” title="<?php echo constant('website-title-title'); ?>">

      <?php
      if (strlen(constant(‘website-title’)) > 22) {
      $menuTitle = substr(constant(‘website-title’), 0, 21) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘website-title’);
      }
      ?>




    • <a <?php if ($title == constant('social-media')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>admin-settings/social-media/” title="<?php echo constant('social-media-title'); ?>">

      <?php
      if (strlen(constant(‘social-media’)) > 22) {
      $menuTitle = substr(constant(‘social-media’), 0, 21) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘social-media’);
      }
      ?>




    • <a <?php if ($title == constant('access-levels')) { echo 'id="current"'; } ?> href=“<?php echo URLADDR ?>admin-settings/access-levels/” title="<?php echo constant('access-levels-title'); ?>">

      <?php
      if (strlen(constant(‘access-levels’)) > 25) {
      $menuTitle = substr(constant(‘access-levels’), 0, 24) . ‘…’;
      echo $menuTitle;
      } else {
      echo constant(‘access-levels’);
      }
      ?>





[/code]

Here’s the revised “menu.inc.php” - with an array and nearly all the code stripped down to barely nothing.
[php]<?php
if (!isset($page_order)) $page_order = ‘’;
if (!isset($sortID)) $sortID = ‘’;
$menu = array(
‘category’ => array(
‘icon’ => ‘fa fa-bars’,
‘title’ => ‘<?php if ($title == constant("my-account") || $title == constant("password") || $title == constant("email-address") || $title == constant("logout")) { echo class=active } ?>’,
‘description’ => ‘<?php constant("account-overview-title"); ?>’,
‘class’ => ‘<?php if ($title == constant("my-account") || $title == constant("password") || $title == constant("email-address") || $title == constant("logout")) { echo class=active } ?>’,
‘links’ => array (
‘icon’ => ‘fa fa-user’,
‘title’ => ‘<?php if ($title == constant("my-account")) { echo "id="current""; } echo "href="'.URLADDR.'account-overview/my-account/""; echo "title="constant("my-account-title");"" ?>’,
‘description’ => ‘<?php constant("my-account") ?>’))
);

foreach ($menu as $section => $items) {
$cIcon = $items[‘icon’];
$cTitle = $items[‘title’];
$cDesc = $items[‘description’];
$cClass = $items[‘class’];
$lIcon = $items[‘links’][‘icon’];
$lTitle = $items[‘links’][‘title’];
$lDesc = $items[‘links’][‘description’];
?>

<?php } ?>[/php]

Revised menu: http://andrewliesenfeld.com/pxbpanel/account-overview/my-account/ (I’m specifically using this page only to do the revisions)
Original menu seen here: http://andrewliesenfeld.com/pxbpanel/account-overview/change-password/

You have quite a few issues here…

You’re using <?php tags inside a php tag. This will result in your PHP code being parsed as a string.

[php]‘title’ => ‘<?php if ($title == constant("my-account") || $title == constant("password") || $title == constant("email-address") || $title == constant("logout")) { echo class=active } ?>’,[/php]

You’re not properly creating strings in the php tags.

[php]echo class=active[/php]

Should be

[php]echo ‘class=“active”’[/php]

You can use a ternary operator, here is an example:

(Note: you do not use echo here since you are assigning an array value)

[php]‘title’ => ($title == constant(“my-account”) || $title == constant(“password”) || $title == constant(“email-address”) || $title == constant(“logout”) ? ‘class=“active”’ : ‘’),[/php]

You have so many issues here, I recommend reading up on strings for starters.

http://www.php.net/manual/en/language.types.string.php

Matt,
I truly appreciate your diligence in assisting me. I do appreciate it.

I was wondering if you could help me a bit further by explaining how I’d go about fixing this line:
[php]‘title’ => ($title == constant(“my-account”) ? ‘id=“current”’ : ‘’) ‘href=’.URLADDR.‘account-overview/my-account/’ 'title='constant(“my-account-title”),[/php]
I’m receiving this error for that line:

Parse error: syntax error, unexpected ''href='' (T_CONSTANT_ENCAPSED_STRING), expecting ')' in

What I’m really curious about is those operators you used earlier.
[php]($title == constant(“my-account”) || $title == constant(“password”) || $title == constant(“email-address”) || $title == constant(“logout”) ? ‘class=“active”’ : ‘’)[/php]
Works like a charm. So, “?” counts as the if result brackets and the colon is an else statement, I’m guessing? I can’t be sure. I’ve never seen PHP written in that manner and could not find a suitable set of keywords in Google to match that.

I’d be very interested to read your explanation on the operators in the array and when they should be used.

Thanks,
awl19

Did you read the manual page for strings? You are not doing a proper “concat” on the string e.g.

[php]($title == constant(“my-account”) ? ‘id=“current”’ : ‘’) ‘href=’.URLADDR.’[/php]

Should be

[php]($title == constant(“my-account”) ? ‘id=“current”’ : ‘’) . ‘href=’.URLADDR.’[/php]

However, I’m not sure this is what you want to do here.

The ternary operator is a simple inline if/else for example:

[php]echo ($value == 1 ? “true” : “false”)[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service