SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Correct way to make a List from an Array?!

    Hi all,

    I've got a table that determines a category/sub-category relationship using id and parentId.

    Using MySQL I have been able to output the order of the Category/Sub-Category relationship into an Array:

    Code PHP:
    Array
    (
        [1] => Array
            (
                [0] => 
                [1] => Test Category
                [2] => 1
            )
     
        [4] => Array
            (
                [0] => 1
                [1] => Sub Category 1
                [2] => 2
            )
     
        [5] => Array
            (
                [0] => 1
                [1] => Sub Category 3
                [2] => 2
            )
     
        [6] => Array
            (
                [0] => 1
                [1] => Sub Category 4
                [2] => 2
            )
     
        [2] => Array
            (
                [0] => 
                [1] => Test Category 2
                [2] => 1
            )
     
        [3] => Array
            (
                [0] => 2
                [1] => Sub Category 2
                [2] => 2
            )
     
    )

    and using the following I am generating a List which when put into my page, css styles it into a menu.

    Code PHP:
            function RenderCategoryMenu() {
                $data = dataAccess::getCartCategories(null, array(), 1);
                if(isset($data) && count($data) > 0) {
                    $rv = "<ul id=\"leftMenu\">\n\t";
                    foreach($data as $i => $value) {
                        if(isset($value[0]))
                            $rv .= "<ul>\n\t";
                        $rv .= "<li><a href=\"#" . $i . "\">" . $value[1] . "</a>\n\t";
                        if(isset($value[0]))
                            $rv .= "</ul>";
                        $rv .= "</li>";
                    }
                    return $rv . "</ul>";
                } else
                    return "<ul id=\"leftMenu\"><li><a>No Categories</a></li></ul>";
            }

    Using the above Array and function, I get

    Code HTML4Strict:
    <ul id="leftMenu">
        <li><a href="#1">Test Category</a></li>
            <ul>
                <li><a href="#4">Sub Category 1</a>
            </ul>
        </li>
            <ul>
                <li><a href="#5">Sub Category 3</a>
            </ul>
        </li>
            <ul>
                <li><a href="#6">Sub Category 4</a>
            </ul>
        </li>
        <li><a href="#2">Test Category 2</a></li>
            <ul>
                <li><a href="#3">Sub Category 2</a>
            </ul>
        </li>
    </ul>


    But my problem is that each subcategory item has its own UL where it should be all subcategories in one UL.

    ie

    Code HTML4Strict:
    <ul id="leftMenu">
        <li><a href="#1">Test Category</a>
            <ul>
                <li><a href="#4">Sub Category 1</a></li>
                <li><a href="#5">Sub Category 3</a></li>
                <li><a href="#6">Sub Category 4</a></li>
            </ul>
        </li>
        <li><a href="#2">Test Category 2</a>
            <ul>
                <li><a href="#3">Sub Category 2</a></li>
            </ul>
        </li>
    </ul>

    Can anyone help me achieve this?


  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've managed to get around the initial issue, but since the function that provides the data can be told to return more than the first level of sub categories, I would like to be able to handle this.

    Any suggestions?

    Code php:
     
            function RenderCategoryMenu() {
                $data = dataAccess::getCartCategories(null, array(), 1);
                if(isset($data) && count($data) > 0) {
                    $rv = "<ul id=\"leftMenu\">\n\t";
                    for($i = 0; $i < count($data); $i++) {
                        $rv .= "<li><a href=\"#" . $data[$i][0] . "\">" . $data[$i][2] . "</a>";
                        $tmp = $this->getNextCategory($data, $i);
                        if(isset($tmp)) {
                            if(isset($tmp[1]) && !isset($data[$i][1])) {
                                $rv .= "<ul>";
                            } else if (isset($tmp[1]) && isset($data[$i][1])) {
                                $rv .= "</li>";
                            } else if (!isset($tmp[1]) && isset($data[$i][1])) {
                                $rv .= "</li></ul></li>";
                            }
                        } else
                            $rv .= "</li></ul></li>";
                    }
                    return $rv . "</ul>";
                } else
                    return "<ul id=\"leftMenu\"><li><a>No Categories</a></li></ul>";
            }
            function getNextCategory($arr, $val) {
                if(($val+1) > count($arr))
                    return null;
                else
                    return $arr[$val+1];
            }

    which put out

    Code HTML4Strict:
    <ul id="leftMenu">
        <li><a href="#1">Test Category</a>
            <ul>
                <li><a href="#4">Sub Category 1</a></li>
                <li><a href="#5">Sub Category 3</a></li>
                <li><a href="#6">Sub Category 4</a></li>
            </ul>
        </li>
        <li><a href="#2">Test Category 2</a>
            <ul>
                <li><a href="#3">Sub Category 2</a></li>
            </ul>
        </li>
    </ul>


  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Recursion is when a function can call itself. That's what you need for this.

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my array doesn't contain the properties to benefit from a recursion. Because the way it is read, I only have an array of arrays. The values do not contain arrays as children.

    Unless, you are able to help with my mysql query?

    As I can't guarentee the depth of the recursion, are you able to provide a query that will all the categories and link them to their parent categories.

    All categories are stored in the same table.


  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    header
    ('content-type: text/plain');

    $a = array(
        array(
    'Test Category''url', array(
            array(
    'Sub Category 1''url'),
            array(
    'Sub Category 2''url'),
            array(
    'Sub Category 3''url', array(
                array(
    'Sub Sub Category 1''url'),
            )),
            array(
    'Sub Category 4''url'),
        )),
        array(
    'Test Category 2''url', array(
            array(
    'Sub Category 1''url')
        ))
    );

    function 
    create_list ($data) {

        
    $l count($data);
        
    $r '';

        for (
    $i 0$i $l$i++) {

            
    $r .= '<li><a href="' $data[$i][1] . '">' $data[$i][0] . '</a>';

            if (isset(
    $data[$i][2])) {
                
    $r .= create_list($data[$i][2]);
            }

            
    $r .= '</li>';

        }

        return 
    '<ul>' $r '</ul>';

    }
    //print_r($a);
    print create_list($a);
    Return:
    Code:
    <ul>
        <li><a href="url">Test Category</a>
            <ul>
                <li><a href="url">Sub Category 1</a></li>
                <li><a href="url">Sub Category 2</a></li>
                <li><a href="url">Sub Category 3</a>
                    <ul>
                        <li><a href="url">Sub Sub Category 1</a></li>
                    </ul>
                </li>
                <li><a href="url">Sub Category 4</a></li>
            </ul>
        </li>
        <li><a href="url">Test Category 2</a>
            <ul>
                <li><a href="url">Sub Category 1</a></li>
            </ul>
        </li>
    </ul>
    You will have to parse the return value to from the database to get into a use able format.
    http://www.sitepoint.com/article/hie...-data-database
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •