Click here to Skip to main content
15,891,033 members
Home / Discussions / Linux, Apache, MySQL, PHP
   

Linux, Apache, MySQL, PHP

 
GeneralRe: Contact form - Bootstrap Error Validation Pin
bigcdh23-Aug-13 9:22
bigcdh23-Aug-13 9:22 
QuestionPHP find all occurrences of string in file Pin
MicahC5-Jul-13 6:44
MicahC5-Jul-13 6:44 
AnswerRe: PHP find all occurrences of string in file Pin
Graham Breach5-Jul-13 21:20
Graham Breach5-Jul-13 21:20 
GeneralRe: PHP find all occurrences of string in file Pin
MicahC7-Jul-13 19:52
MicahC7-Jul-13 19:52 
GeneralRe: PHP find all occurrences of string in file Pin
MicahC10-Jul-13 5:56
MicahC10-Jul-13 5:56 
QuestionNot much traffic here is there..... Pin
Erudite_Eric16-Jun-13 23:40
Erudite_Eric16-Jun-13 23:40 
AnswerRe: Not much traffic here is there..... Pin
Ali Al Omairi(Abu AlHassan)7-Jul-13 22:18
professionalAli Al Omairi(Abu AlHassan)7-Jul-13 22:18 
QuestionCounting Menu Items & Determining If First/Last Pin
NickBogi7-Jun-13 13:20
NickBogi7-Jun-13 13:20 
I'm hoping the Code Project community can help where other forums have been unable to!

I am attempting to have my code count how many top level menu items there are and then determine what position each menu item is in. So, if it is the first, it will append the class of that item to "first" and if it is the last, it will append the class to "last."

I did the same thing with other variables and it worked. For example, I have the code set to append the class to "parent" if there are subitems and also "active" if it is the current link. However, I am running into difficulty with first/last. I've been through several attempts. My first incarnation was:

PHP
<?php
/*
    Class: MenuDefault
        Menu base class
*/
class MenuDefault extends Menu {
    /*
        Function: process
        Returns:
            Object
    */   
    public function process($module, $element) {
        self::_process($module, $element->first('ul:first'));
        return $element;
    }
    /*
        Function: _process
        Returns:
            Void
    */
    protected static function _process($module, $element, $level = 0) {
        if ($level == 0) {
            $element->attr('class', 'menu '.$module->menu_style);
        } else {
            $element->addClass('level'.($level + 1));
        }
        foreach ($element->children('li') as $li) {
             // is active ?
            if ($active = $li->attr('data-menu-active')) {
                $active = $active == 2 ? ' active current' : ' active';
            }
             // is parent ?
            $ul = $li->children('ul');
            $parent = $ul->length ? ' parent' : null;
            // is first or last ? -- PROBLEM AREA
            $lis = $element->children("li");
             for($forl=0,$imax=$lis;
             $forl<$imax;$forl++){
             if ($forl==0) $position_n = 'first';
             elseif ($forl==$imax-1) $position_n = 'last';
             else $position_n = null;}
              // set class in li
            $li->attr('class', sprintf('level%d item%s '. $position_n .$parent.$active, $level + 1, $li->attr('data-id')));
            // set class in a/span
            foreach ($li->children('a,span') as $child) {
                // get title
                $title = $child->first('span:first');
                // set subtile
                $subtitle = $title ? explode('||', $title->text()) : array();
                if (count($subtitle) == 2) {
                    $li->addClass('hassubtitle');
                    $title->html(sprintf('%s%s', trim($subtitle[0]), trim($subtitle[1])));
                }
                // set image
                if ($image = $li->attr('data-menu-image')) {
                    $title->prepend(sprintf(' ', $image));
                }
                $child->addClass(sprintf('level%d'.$parent.$active, $level + 1));
            }
            // process submenu
            if ($ul->length) {
                self::_process($module, $ul->item(0), $level + 1);
            }
        }
   }
}

However, that generated "first" for every menu item. See below for example:
HTML
<ul class="menu menu-dropdown">
  <li class="level1 item24 first">...Subcode...</li>
  <li class="level1 item22 first parent">...Subcode...</li>
  <li class="level1 item23 first active current">...Subcode...</li>
  <li class="level1 item20 first">...Subcode...</li>
  <li class="level1 item19 first">...Subcode...</li>
</ul>

What I wanted was:
HTML
<ul class="menu menu-dropdown">
  <li class="level1 item24 first">...Subcode...</li>
  <li class="level1 item22 parent">...Subcode...</li>
  <li class="level1 item23 active current">...Subcode...</li>
  <li class="level1 item20">...Subcode...</li>
  <li class="level1 item19 last">...Subcode...</li>
</ul>

Then in another forum, it was suggested to me that " Your basic logic for determining first and last is correct, but you should not add a new loop to do it; instead you need to make it part of the loop that is already looping over the li's (ie: the foreach loop at the top of the snippet I posted). Because of the use of the foreach statement, you need to do a little extra work in order to obtain numerically useful index values.
PHP
// is first or last ? -- PROBLEM AREA
$children_elements = array_values($element->children('li')); [/U]
$last_child_index = count($children_elements)-1; 
foreach($children_elements as $child_index => $li) {
    if ($forl==0) $position_n = 'first';
    elseif ($forl==$imax-1) $position_n = 'last';
    else $position_n = null;}

That brought a bunch of errors
Warning: array_values() [
function.array-values
]: The argument should be an array in ... on line 59

Warning: Invalid argument supplied for foreach() in ... on line 61

Warning: array_values() [
function.array-values
]: The argument should be an array in ... on line 59

Warning: Invalid argument supplied for foreach() in ... on line 61

Warning: array_values() [
function.array-values
]: The argument should be an array in ... on line 59

Warning: Invalid argument supplied for foreach() in ... on line 61

Warning: array_values() [
function.array-values
]: The argument should be an array in ... on line 59

Warning: Invalid argument supplied for foreach() in ... on line 61

Warning: array_values() [
function.array-values
]: The argument should be an array in ... on line 59

Warning: Invalid argument supplied for foreach() in ... on line 61

Warning: array_values() [
function.array-values
]: The argument should be an array in ... on line 59

Warning: Invalid argument supplied for foreach() in ... on line 61

Warning: array_values() [
function.array-values
]: The argument should be an array in ... on line 59

Line 59 being $children_elements = array_value($element->children('li'));

So it seemed like $children_elements was either empty or not an array so I tested with this:
PHP
if(empty($children_elements)) {
    echo hi;
} 

Hi was generated. So it seems empty.

Sorry if this was long but I had no idea how to explain quicker. Please help. I'm stumped and if I can't get this working, a large portion of my time will go to waste as I'll have to scrap the theme design completely.

Thanks in advance.
AnswerGot It Working Pin
NickBogi10-Jun-13 8:29
NickBogi10-Jun-13 8:29 
QuestionUrgent !!! Looking for the best solution Pin
MacAriel15-May-13 8:43
MacAriel15-May-13 8:43 
AnswerRe: Urgent !!! Looking for the best solution Pin
herlon21423-Jul-13 9:59
herlon21423-Jul-13 9:59 
QuestionVideo Gallery for wordpress site. Pin
Neha Pandal12-May-13 10:03
professionalNeha Pandal12-May-13 10:03 
AnswerRe: Video Gallery for wordpress site. Pin
Archie22is30-Jun-13 21:07
Archie22is30-Jun-13 21:07 
Questionhow to store QR Code image in mysql with php Pin
blertaaa2-May-13 22:43
blertaaa2-May-13 22:43 
AnswerRe: how to store QR Code image in mysql with php Pin
Prasad Khandekar21-May-13 4:56
professionalPrasad Khandekar21-May-13 4:56 
AnswerRe: how to store QR Code image in mysql with php Pin
Resalat Haque Prodhan27-Jun-13 2:02
Resalat Haque Prodhan27-Jun-13 2:02 
AnswerRe: how to store QR Code image in mysql with php Pin
Rony Sur2-Jul-13 0:04
professionalRony Sur2-Jul-13 0:04 
QuestionHow to install xampp or Lamp in Ubuntu 8 Pin
Member 992947820-Mar-13 20:55
Member 992947820-Mar-13 20:55 
AnswerRe: How to install xampp or Lamp in Ubuntu 8 Pin
Richard MacCutchan20-Mar-13 22:54
mveRichard MacCutchan20-Mar-13 22:54 
AnswerRe: How to install xampp or Lamp in Ubuntu 8 Pin
Mohibur Rashid30-Apr-13 17:19
professionalMohibur Rashid30-Apr-13 17:19 
GeneralRe: How to install xampp or Lamp in Ubuntu 8 Pin
Rony Sur2-Jul-13 0:09
professionalRony Sur2-Jul-13 0:09 
AnswerRe: How to install xampp or Lamp in Ubuntu 8 Pin
Tushar Guru8-Sep-14 22:17
Tushar Guru8-Sep-14 22:17 
QuestionHow to get data from the other website in php Pin
surendra4u20-Mar-13 1:04
surendra4u20-Mar-13 1:04 
AnswerRe: How to get data from the other website in php Pin
dusty_dex20-Mar-13 3:21
dusty_dex20-Mar-13 3:21 
QuestionIRDA without IR Pin
lukeer25-Feb-13 22:30
lukeer25-Feb-13 22:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.