Viewed   68 times

Hi I have a menu on my site on each page, I want to put it in it's own menu.php file but i'm not sure how to set the class="active" for whatever page i'm on. Here is my code: please help me

menu.php:

<li class=" has-sub">
    <a class="" href="javascript:;"><i class=" icon-time"></i> Zeiten<span class="arrow"></span></a>
    <ul class="sub">
       <li><a class="" href="offnungszeiten.php">Öffnungszeiten</a></li>
       <li><a class="" href="sauna.php">Sauna</a></li>
       <li><a class="" href="frauensauna.php">Frauensauna</a></li>
       <li class=""><a class="" href="custom.php">Beauty Lounge</a></li>
       <li><a class="" href="feiertage.php">Feiertage</a></li>
    </ul>
</li>

 Answers

2

It would be easier if you would build an array of pages in your script and passed it to the view file along with the currently active page:

//index.php or controller

$pages = array();
$pages["offnungszeiten.php"] = "Öffnungszeiten";
$pages["sauna.php"] = "Sauna";
$pages["frauensauna.php"] = "Frauensauna";
$pages["custom.php"] = "Beauty Lounge";
$pages["feiertage.php"] = "Feiertage";

$activePage = "offnungszeiten.php";


//menu.php
<?php foreach($pages as $url=>$title):?>
  <li>
       <a <?php if($url === $activePage):?>class="active"<?php endif;?> href="<?php echo $url;?>">
         <?php echo $title;?>
      </a>
  </li>

<?php endforeach;?>

With a templating engine like Smarty your menu.php would look even nicer:

//menu.php
{foreach $pages as $url=>$title}
   <li>
       <a {if $url === $activePage}class="active"{/if} href="{$url}">
         {$title}
      </a>
   </li>
{/foreach}
Thursday, October 6, 2022
4

I came up with my own crude solution and created a class to do what I was looking for. My sources are referenced at the bottom.

class css2string {
    var $css;

    function parseStr($string) {
        preg_match_all( '/(?ims)([a-z0-9, s.:#_-@]+){([^}]*)}/', $string, $arr);
        $this->css = array();
        foreach ($arr[0] as $i => $x)
        {
            $selector = trim($arr[1][$i]);
            $rules = explode(';', trim($arr[2][$i]));
            $this->css[$selector] = array();
            foreach ($rules as $strRule)
            {
                if (!empty($strRule))
                {
                    $rule = explode(":", $strRule);
                    $this->css[$selector][trim($rule[0])] = trim($rule[1]);
                }
            }
        }
    }

    function arrayImplode($glue,$separator,$array) {
        if (!is_array($array)) return $array;
        $styleString = array();
        foreach ($array as $key => $val) {
            if (is_array($val))
                $val = implode(',',$val);
            $styleString[] = "{$key}{$glue}{$val}";

        }
        return implode($separator,$styleString);   
    }

    function getSelector($selectorName) {
        return $this->arrayImplode(":",";",$this->css[$selectorName]);
    }

}

You can run it as follows:

$cssString = "
h1 {
  font-size: 15px;
  font-weight: bold;
  font-style: italic;
  font-family: Verdana, Arial, Helvetica, sans-serif;
}

div.item {
  font-size: 12px;
  border:1px solid #EEE;
}";

$getStyle = new css2string();
$getStyle->parseStr(cssString);
echo $getStyle->getSelector("div.item");

The output would be as follows:

font-size:12px;border:1px solid #EEE

This solution works even with comments within your CSS file, as long as the comments are not inside selectors.

References: http://www.php.net/manual/en/function.implode.php#106085 http://.com/questions/1215074/break-a-css-file-into-an-array-with-php

Tuesday, October 4, 2022
 
bcag2
 
3

This one should work. Using this function you can set any array element in any depth by passing a single string containing the keys separated by .

function setArray(&$array, $keys, $value) {
  $keys = explode(".", $keys);
  $current = &$array;
  foreach($keys as $key) {
    $current = &$current[$key];
  }
  $current = $value;
}

You can use this as follows:

$array = Array();
setArray($array, "key", Array('value' => 2));
setArray($array, "key.test.value", 3);
print_r($array);

output:

Array (
    [key] => Array
        (
            [value] => 2
            [test] => Array
                (
                    [value] => 3
                )

        )

)
Wednesday, November 16, 2022
 
roryf
 
1

Use echo "<h3>".implode(', ' $w)."</h3>".

The reason is as follows: $val represents a declaration block which is a rule set with several comma-separated selectors (The key $selektor only contains the index of the declaration block which is completely arbitrary for most usages). To get the selectors, use $val->getSelectors() (which you did). This will get you an array of all selectors.

The declaration block:

h1, h2 { value: 1; }

will thus be parsed into a CSSDeclarationBlock object with the selector array ['h1', 'h2']. To get back the selectors as they were originally defined, use implode.

Friday, October 14, 2022
 
wooble
 
3

There is an example that shows how to initiate the page with width and height.

// Define a page size/format by array - page will be 190mm wide x 236mm height
$mpdf=new mPDF('utf-8', array(190,236));
Tuesday, September 27, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :