Asked  7 Months ago    Answers:  5   Viewed   35 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

52

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}
Wednesday, March 31, 2021
 
TheCarver
answered 7 Months ago
26

You have an mistake in your code, here's the correct code:

<?php
// First of all send css header
header("Content-type: text/css");

// Array of css files
$css = array(
    'main.css',
    'menu.css',
    'content.css'
);

// Prevent a notice
$css_content = '';

// Loop the css Array
foreach ($css as $css_file) {
    // Load the content of the css file 
    $css_content .= file_get_contents($css_file);
}

// print the css content
echo $css_content;
?>

And I hope the files are in the same folder. Perhaps you should use __DIR__ or dirname(__FILE__) to get the relative path to your files.

Wednesday, March 31, 2021
 
mozlima
answered 7 Months ago
48

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, March 31, 2021
 
sohum
answered 7 Months ago
58

The most likely cause is that you are using PHP's default Content-Type output of text/html so Firefox thinks:

This is a stylesheet written in HTML, a stylesheet language I don't understand and will therefore ignore.

The other browsers are, presumably, compensating for the error.

Add:

header('Content-type: text/css');
Wednesday, March 31, 2021
 
Besnik
answered 7 Months ago
60

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));
Saturday, May 29, 2021
 
Jubair
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :