Jump to content

selecting the right item in a menu


exhaler

Recommended Posts

hello,

 

i have this simple problem, how can i point to an item in a menu so that it shows the correct page i'm currently visiting?

for example, i have this menu :

 

<div class="submenu">
   <div class="submenu-head">Products</div>
   
   <ul>
      <li class="first-item current">
         <a tabindex="1" class="first-item current" href="index.php">Products</a>
      </li>
      
      <li>
         <a tabindex="1" href="add_product.php">
            Add new Product
         </a>
      </li>
   </ul>
</div>

 

where class="first-item current" points to the current page i'm visiting (i.e Products is highlited)

so when i visit another page (add_product.php) i want to do this

 

<div class="submenu">
   <div class="submenu-head">Products</div>
   
   <ul>
      <li >
         <a tabindex="1" href="index.php">Products</a>
      </li>
      
      <li class="first-item current">
         <a tabindex="1" class="first-item current"  href="add_product.php">
            Add new Product
         </a>
      </li>
   </ul>
</div>

 

the usual way i solve this problem is by using a switch statement

 

switch($header_title) {
   case "index":
      echo "<div class="submenu">
   <div class="submenu-head">Products</div>
   
   <ul>
      <li class="first-item current">
         <a tabindex="1" class="first-item current" href="index.php">Products</a>
      </li>
      
      <li>
         <a tabindex="1" href="add_product.php">
            Add new Product
         </a>
      </li>
   </ul>
</div>";
break;

switch "add_product":
echo "<div class="submenu">
   <div class="submenu-head">Products</div>
   
   <ul>
      <li >
         <a tabindex="1" href="index.php">Products</a>
      </li>
      
      <li class="first-item current">
         <a tabindex="1" class="first-item current"  href="add_product.php">
            Add new Product
         </a>
      </li>
   </ul>
</div>";
break}

 

but this is not very efficient, cause if have 10 menu links i would have to write 10 cases  >:(

so how can u do this without having to write all those cases,  like maybe use a function instead?? :shrug:

Link to comment
Share on other sites

Yeah, that is a pretty poor implementation. I would simply create an array of all the links youwant to create then use a for loop to iterate over each one and set the class appropriate based upon the currently selected page. I really don't understand why you would want to put a switch statement inside an array declaration. Makes your code much harder to read/debug.

 

The following code should help. All you should need to do is add your options to the array

<?php

//Create array of all menu options using $header_title as key with label/href values
$menuOptions = array(
    'index'       => array('label' => 'Products', 'href' => 'index.php'),
    'add_product' => array('label' => 'Add new Product', 'href' => 'add_product.php')
);

//Set the current page to default, if not in array
if(!in_array($header_title, $menuOptions))
{
    $header_title = 'index';
}

//Create HTML for the menu options
$menuOptions = '';
foreach($menuOptions as $pageIdx => $option)
{
    $class = ($pageIdx==$header_title) ? 'first-item current' : '';
    $menuOptions .= "<li class=\"{$class}\">";
    $menuOptions .= "<a tabindex=\"1\" class=\"{$class}\" href=\"{$option['href']}\">{$option['label']}</a>";
    $menuOptions .= "</li>\n";
}

?>

<div class="submenu">
  <div class="submenu-head">Products</div> 
  <ul>
  <?php echo $menuOptions; ?>
  </ul>
</div>

Link to comment
Share on other sites

Thank you mjdamato, that did wonders for me :D, just had to fix the default current page to this

if (in_array($header_title, array($menuOptions))) {
$header_title = 'Products';
}

 

i have another question, what is a good implementation of saving text taken from TinyMCE into database??

 

below is an example of how tinymce text is saved in phpmyadmin in my website:

<p><strong>b</strong> <em>i</em> <span style="text-decoration: underline;">u</span></p><p style="text-align: left;">align 1</p><p style="text-align: center;">align 2</p><p style="text-align: right;">align 3</p><p style="text-align: justify;">align 4</p><ol><li>2Ô</li><li>3</li><li>4</li></ol><ul><li>2454</li><li>4</li><li>asd</li></ul><p><span style="text-decoration: line-through;">kill all</span></p><p><sub>sup</sub> another <sup>sup</sup></p><p>symbols: “ατΨ♣♦ο</p><p><a>link 1</a></p><p>no style</p><p>text plain</p><p>text word<!-- pagebreak -->more crap goes here</p>

 

and this is how tinymce text is saved in phpmyadmin in wordpress:

<strong>b</strong> <em>i</em> u

align 1

align 2

align 3

align 4
<ol>
<li>2Ô</li>
<li>3</li>
<li>4</li>
</ol>
<ul>
<li>2454</li>
<li>4</li>
<li>asd</li>
</ul>
kill all

<sub>sup</sub> another <sup>sup</sup>

symbols: “ατΨ♣♦ο

<a>link 1</a>

no style

text plain

text word<img src="http://localhost/EAM/eam-admin/tiny_mce/plugins/pagebreak/img/trans.gif" alt="" />more crap goes here

 

 

see the difference and how wordpress has line-breaks in it, also some tags are removed.

note: the difference is not from the tinymce configuration cause i've did the same thing and nothing happened

thanks for the help

 

Link to comment
Share on other sites

Yeah, I made a change in the array structure and forgot to change that IF condition for setting the default value.

 

As for the TinyMCE issue, I really have no clue. I don't user TinyMCE or WordPress. The line breaks should not be an issue since it will eventually be rendered in HTML the line breaks won't be interpreted (unless you are displaying the code withing PRE tags or something). The real problem would seem to be the missing tags which are there to style the content. Since they are missing in your WordPress example I would guess whatever functions/processes you are using from WordPress are removing those tags. I suspect the code in question does that to prevent CSS injection.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.