SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Need multi array for javascript to work while using PHP.

    I've been trying to build a website template and all is fine, except for one thing. For the menu I use javascript to display a submenu and for it to work I need to know what row I'm printing from an array. So I created a multi array to get the row number, but when I try and run it I get this error:
    Warning: Variable passed to each() is not an array or object in /template_inc.php on line 74

    I know I'm doing something wrong when I create or access the arrays, so I need some help. I could hard code everything, but then the template is pointless.

    So... in the beginning of the class I have this:
    PHP Code:
    {
        var 
    $content;
        var 
    $title 'Certell AB';
        var 
    $keywords 'body fat, kroppsfett, våg';
        var 
    $buttons = array(array('Hem' => 'index.php'), //This was to begin with a simple array and it worked then
                
    array('Vanliga frågor' => '/fragor/index.php'),
                array(
    'Butik' => '/handla/index.php'),
                array(
    'Hjälp' => 'help.php')
                ); 
    The array used to have just $name, $url pairs, but I needed to know what row it was, so I added $row.

    Then we have part of the menu. It uses three different functions.
    PHP Code:
    function DisplayMenu($buttons)
        {
            echo 
    '<div class="menuback"><div id="inline">';
            echo 
    '<ul>';
            
            for (
    $row 0$row 4$row++)
            {
                while (list(
    $name$url) = each($buttons['$row'])) //This is line 74
                
    {
                    
    $this -> DisplayButton($row$name$url, !$this->IsURLCurrentPage($url));
                }
            }
            
            echo 
    '</ul>';
    //The rest of this function is the end of the divtags etc.
            
    echo '</div><ilayer name="dep1"><layer name="dep2"></layer></ilayer>
    <div id="describe" onMouseover="clear_delayhide()" onMouseout="resetit(event)"></div></div></div>'
    ;
        } 
    The function that checks to see what button should take the "here" style.
    PHP Code:
    function IsURLCurrentPage($url)
        {
        
            if (
    strpos($_SERVER['PHP_SELF'], $url )==false)
            {
                return 
    false;
            }
            else
            {
                return 
    true;
            }
        } 
    And the actual display of each button.
    PHP Code:
    function DisplayButton($row$name$url$active true)
        {
            if (
    $active)
            {
                echo 
    '<li><a href="'.$url.'" onMouseover="showit($row)" target="_top">'.$name.'</a></li>';
            }
            else
            {
                echo 
    '<li><a href="'.$url.'" onMouseover="showit($row)" target="_top" class="here">'.$name.'</a></li>';
            }
        } 
    It's the onMouseover="showit()" that is the problem to begin with. This is where I place the number of the row, so they get numbered form 0 to 4 in this case.

    What can I do to fix it? Should I build this in a different way? I have been considering hardcoding the actual links/buttons and just check for current URL so the style is correct, but that limits the templates usability a little.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your function DisplayButton() ...

    Can you rework it so that the PHP script breaks out of HTML ? ie

    PHP Code:
    .
    .
    ?>
    <li>
    <a href="<?php echo($url); ?>" onMouseover="showit(<?php echo($row); ?>)" target="_top">'.$name.'</a></li>
    <?php
    .
    .
    Also can you post the showit() Javascript function ? I don't actually see what HTML element that uses an integer within it's ID/NAME attribute...

  3. #3
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I can break out of PHP for partsof the function, no problem.

    This is what I think is the javascript showit function.
    PHP Code:
    function showit(which){
    clear_delayhide()
    thecontent=(which==-1)? "" submenu[which]
    if (
    document.getElementById||document.all)
    menuobj.innerHTML=thecontent
    else if (document.layers){
    menuobj.document.write(thecontent)
    menuobj.document.close()
    }

    Is this helpful?

    EDIT: I did some more searching and got to think about count(). If I count the number of items in the array (4 in this case) and asign the count to another array that I use for the javascript bit, that would work wouldn't it? Not sure exactly how to do it, so if someone has an idea...
    Last edited by reptilianfeline; May 26, 2003 at 23:44.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  4. #4
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the multi array is still the key... I couldn't get any counting to work, and after reading some more I know count() will only give me the total number so...

    Back to the multi array...
    If I just add the extra array to the original I get the URL as "Array" and the name as 0 1 2 3. I'll start with that. I have to get the url to be $url and the name to be $name and the initial number to show up in my showit() javascript thingy.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  5. #5
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sigh... I'm getting tired. I've tried different things and I just cant get it to work. When I try to put in the proper reference to a value in the array I get errors, and when I don't, I get the wrong output.

    This is the array now:
    PHP Code:
    var $buttons = array('Hem' => 'index.php'
                
    'Vanliga fr&aring;gor' => '/fragor/index.php',
                
    'Butik' => '/handla/index.php',
                
    'Hj&auml;lp' => 'help.php'
                
    ); 
    These are the three functions it travels through:
    PHP Code:
    function DisplayMenu($buttons)
        {
            echo '<div class="menuback"><div id="inline">';
            echo '<ul>';

                foreach ($buttons as $name => $url)
                {
                    $row = array($buttons);//I'm creating a new array to get the numbers.
                    $this -> DisplayButton($row, $name, $url, !$this->IsURLCurrentPage($url));
                    
                }
            
            echo '</ul>';
            echo '</div><ilayer name="dep1"><layer name="dep2"></layer></ilayer>
    <div id="describe" onMouseover="clear_delayhide()" onMouseout="resetit(event)"></div></div></div>';
        }
        
        function IsURLCurrentPage($url)
        {
        
            if (strpos($_SERVER['PHP_SELF'], $url )==false)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        
        function DisplayButton($row, $name, $url, $active = true)
        {
            if ($active)
            {
            ?>
    <li><a href="<?php echo ($url);?>" onMouseover="showit(<?php echo ($row);?>)" target="_top"><?php echo ($name);?></a></li>
    <?php
            
    }
            else
            {
            
    ?>
    <li><a href="<?php echo ($url);?>" onMouseover="showit(<?php echo ($row);?>)" target="_top" class="here"><?php echo ($name);?></a></li>
    <?php
            
    }
        }
    The output is fine except for the onMouseover bit that looks like this:
    <li><a href="index.php" onMouseover="showit(Array)" target="_top" class="here">Hem</a></li>
    It shouldn't be showit(Array) it should be showit(0) etc.

    Please help me!!! What am I doing wrong here????
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  6. #6
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well... I finally got it to work without any help. This is the functions:
    PHP Code:
    function DisplayMenu($buttons)
        {
            echo '<div class="menuback"><div id="inline">';
            
            echo '<ul>';

                foreach ($buttons as $name => $url)
                {
                    $rows[] = $url; //Creates numbered array.
                    $this -> DisplayButton($rows, $name, $url, !$this->IsURLCurrentPage($url));
                    
                }
            
            echo '</ul>';
            
            echo '</div><ilayer name="dep1"><layer name="dep2"></layer></ilayer>
    <div id="describe" onMouseover="clear_delayhide()" onMouseout="resetit(event)"></div></div></div>';
        }
        
        function IsURLCurrentPage($url)
        {
        
            if (strpos($_SERVER['PHP_SELF'], $url )==false)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        
        function DisplayButton($rows, $name, $url, $active = true)
        {
    //Take out the numbers from the new array and create variable to use in showit().
            foreach ($rows as $row =>$url)
            {
                $this -> $row;
            }
            if ($active)
            {
            ?>
    <li><a href="<?php echo ($url);?>" onMouseover="showit(<?php echo ($row);?>)" target="_top"><?php echo ($name);?></a></li>
    <?php
            
    }
            else
            {
            
    ?>
    <li><a href="<?php echo ($url);?>" onMouseover="showit(<?php echo ($row);?>)" target="_top" class="here"><?php echo ($name);?></a></li>
    <?php
            
    }
        }
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •