Jump to content

Missing Piece in Array


maxudaskin

Recommended Posts

Hi guys,

I apologize for the number of posts regarding this particular class that I am writing (really, at this point, you've probably done more in it), but I have another issue. I've decided to re-write the pieces that were giving me trouble, and instead of using a for loop, I went to a foreach loop.

 

Here's what it in the database for the particular query:

kJnDy.png

 

Here's what I get when I dump the array with print_r():

Array (

    [0] => Array (

        [0] => 4

        [id] => 4

        [1] => Test

        [label] => Test

        [2] => ?r=test

        [link] => ?r=test

        [3] =>

        [relation] =>

        [4] => 0

        [parent] => 0

        [5] => 5

        [sort] => 5

        [6] => 1

        [active] => 1 )

    [1] => Array (

        [0] => 2

        [id] => 2

        [1] => About Us

        [label] => About Us

        [2] => ?r=about

        [link] => ?r=about

        [3] =>

        [relation] =>

        [4] => 0

        [parent] => 0

        [5] => 10

        [sort] => 10 [6] => 0

        [active] => 0 )

    [2] => Array (

        [0] => 3

        [id] => 3 [1] => Fleet

        [label] => Fleet

        [2] => ?r=about/fleet

        [link] => ?r=about/fleet

        [3] =>

        [relation] =>

        [4] => 2

        [parent] => 2

        [5] => 0

        [sort] => 0

        [6] => 0

        [active] => 0 ) )

 

And the SQL statement:

SELECT * FROM `menu` ORDER BY `parent` ASC, `sort` ASC

 

With the corresponding PHP code (The issue starts in formatMenuItems()):

<?php
/**
* Menu Class
*
* This file is used to create a dynamic menu based on the user's permissions and status
* @author Max Udaskin <[email protected]>
* @version 1.0
* @package navigation
*/

/**
* @ignore
*/
if(!defined('ALLOW_ACCESS')) { // Do not allow the file to be accessed by itself
    die('Restricted File.');
}

require_once("Database.php");

class Menu
{
    protected $login;
    protected $all_rows;
    protected $menu_items;
    protected $html = NULL;

    /**
     * The constructor
     * @param Login $login
     */
    function _construct()
    {
    }

    /**
     * Get All Items
     * Retrieves all database menu items
     */
    private function getAllItems()
    {
        $sql = 'SELECT * FROM `menu` ORDER BY `parent` ASC, `sort` ASC';
        $database = new Database('default');
        $database->connect();
        $database->fetchArray($sql);
        $query = $database->getLastQuery();

        $i = 0;
        while($row = mysql_fetch_array($query))
        {
            $this->all_rows[$i] = $row;
            $i++;
        }
        print_r($this->all_rows);
    }

    /*
     * Selects the menu items that are appropriate for the user
     */
    private function chooseMenuItems()
    {
        $this->menu_items = $this->all_rows;
    }

    /**
     * Formats the menu items for the HTML output
     */
    private function formatMenuItems()
    {
        $menu = array();
        $menu_imploded = '';
        
        foreach($this->menu_items as $item) {
            if($item['parent'] == 0) {
                $menu[] = $this->parseItem($item);
            }
        }

        $menu_imploded = $this->combineMultiArray('', $menu);
        
        echo '<div id="menu">';
        echo $menu_imploded;
        echo '</div>';
    }

    /**
     * Searches an array and it's sub arrays for a defined value ($for)
     * @param array $array
     * @param mixed $for
     */
    private function search(array $array, $for) {
        foreach ($array as $key => $value) {
            if ($value === $for) {
                return ($key);
            } else if (is_array($value)) {
                $found = $this->search($value, $for);
                if (is_array($found)) {
                    array_unshift($found, $key);
                    return $found;
                }
            }
        }
        return false;
    }

    /**
     * Gets the array item and returns it
     * @param array $arr
     * @param string $string
     */
    function variableArray($arr, $string)
    {
        preg_match_all('/\[([^\]]*)\]/', $string, $arr_matches, PREG_PATTERN_ORDER);

        $return = $arr;
        foreach($arr_matches[1] as $dimension)
        {
            $return = $return[$dimension];
        }

        return $return;
    }

    /**
     * Parses HTML for the item, as an array
     * @param array $item
     */
    private function parseItem($item)
    {
        if($item['parent'] == 0) {
            $h2_s = '<h2>';
            $h2_e = '</h2>';
        } else {
            $h2_s = '';
            $h2_e = '';
        }
        $return[0] = '<ul><li>' . $h2_s . '<a href="';
        $return[0] .= $item['link'] . '">';
        $return[0] .= $item['label'] . '</a>' . $h2_e;
        $return[1] = false; // Children
        $return[2] = '</li></ul>';
        $return['parent'] = $item['parent'];

        return $return;
    }

    /**
     * Puts all of the HTML in the array together
     */
    private function produceHtml()
    {
        $this->getAllItems();
        $this->chooseMenuItems();
        $this->formatMenuItems();
    }

    /**
     * Get HTML
     * @return string The HTML menu
     */
    public function getHtml()
    {
        $this->produceHtml();
        return $this->html;
    }
    
    /**
     * Combine Multi Array
     * Takes any array of any dimension and combines it into a single string
     * @param unknown_type $array
     */
    private function combineMultiArray($glue, $array)
    {   
        $return = '';
        foreach($array as $piece) {
            if(is_array($piece)) {
                $return .= $glue . $this->combineMultiArray($glue, $piece);
            }
            $return .= $glue . $piece;
        }
        
        return $return;
    }
}

Link to comment
https://forums.phpfreaks.com/topic/233749-missing-piece-in-array/
Share on other sites

Sorry, teletype apparently formats into HTML, so I've put it within [nobbc] tags

 

Array (

    [0] => Array (

        [0] => 4

        [id] => 4

        [1] => Test

        [label] => Test

        [2] => ?r=test

        [link] => ?r=test

        [3] =>

        [relation] =>

        [4] => 0

        [parent] => 0

        [5] => 5

        [sort] => 5

        [6] => 1

        [active] => 1 )

    [1] => Array (

        [0] => 2

        [id] => 2

        [1] => About Us

        [label] => About Us

        [2] => ?r=about

        [link] => ?r=about

        [3] =>

        [relation] =>

        [4] => 0

        [parent] => 0

        [5] => 10

        [sort] => 10 [6] => 0

        [active] => 0 )

    [2] => Array (

        [0] => 3

        [id] => 3 [1] => Fleet

        [label] => Fleet

        [2] => ?r=about/fleet

        [link] => ?r=about/fleet

        [3] =>

        [relation] =>

        [4] => 2

        [parent] => 2

        [5] => 0

        [sort] => 0

        [6] => 0

        [active] => 0 ) )

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.