How to create html list from such array ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lp

    How to create html list from such array ?

    I've got an example array like this:

    $myArr = array(
    array("jj", "0", "jjj"),
    array("ee", "0", "eee"),
    array("bb", "ee", "bbb"),
    array("ll", "ee", "lll"),
    array("ff", "0", "fff"),
    )

    Where each row is an array with columns: id of list element, id of parent
    list element ("0" means main node) and content of the element. So, from my
    array I'd like to create a list like this:

    <ul>
    <li>jjj</li>
    <li>eee
    <ul>
    <li>bbb</li>
    <li>eee</li>
    </ul>
    </li>
    <li>fff</li>
    <ul>

    Could anybody help me ?

    Best regards.
    LP


  • Ivan Omelchenko 608308824

    #2
    Re: How to create html list from such array ?

    lp ÐÉÛÅÔ:[color=blue]
    > I've got an example array like this:
    >
    > $myArr = array(
    > array("jj", "0", "jjj"),
    > array("ee", "0", "eee"),
    > array("bb", "ee", "bbb"),
    > array("ll", "ee", "lll"),
    > array("ff", "0", "fff"),
    > )
    >
    > Where each row is an array with columns: id of list element, id of parent
    > list element ("0" means main node) and content of the element. So, from my
    > array I'd like to create a list like this:
    >
    > <ul>
    > <li>jjj</li>
    > <li>eee
    > <ul>
    > <li>bbb</li>
    > <li>eee</li>
    > </ul>
    > </li>
    > <li>fff</li>
    > <ul>
    >
    > Could anybody help me ?
    >
    > Best regards.
    > LP
    >
    >[/color]

    Look's like a usual tree. Try to use not array, but SQL DB, it's easy

    Comment

    • lp

      #3
      Re: How to create html list from such array ?

      Ivan Omelchenko 608308824 wrote:[color=blue]
      > Look's like a usual tree. Try to use not array, but SQL DB, it's easy[/color]

      Sorry, but I can't use SQL here, the data deas not come from database. I've
      got only array like that which will be passed to my class.

      LP


      Comment

      • Ivan Omelchenko 608308824

        #4
        Re: How to create html list from such array ?

        lp ÐÉÛÅÔ:[color=blue]
        > Ivan Omelchenko 608308824 wrote:
        >[color=green]
        >>Look's like a usual tree. Try to use not array, but SQL DB, it's easy[/color]
        >
        >
        > Sorry, but I can't use SQL here, the data deas not come from database. I've
        > got only array like that which will be passed to my class.
        >
        > LP
        >
        >[/color]
        okay, it's worstly, but also real
        you have to use 'usort' function that
        looking for a $array[2] - (keys parent or not) and sorting an array.
        It's only idea, not realization.

        P.s sorry for my bad english

        Comment

        • Ivan Omelchenko 608308824

          #5
          Re: How to create html list from such array ?

          lp ÐÉÛÅÔ:[color=blue]
          > Ivan Omelchenko 608308824 wrote:
          >[color=green]
          >>Look's like a usual tree. Try to use not array, but SQL DB, it's easy[/color]
          >
          >
          > Sorry, but I can't use SQL here, the data deas not come from database. I've
          > got only array like that which will be passed to my class.
          >
          > LP
          >
          >[/color]
          more little question: is you array sorted correctly?
          if your array has
          $myArr = array(
          array("jj", "0", "jjj"),
          array("bb", "ee", "bbb"),
          array("aa", "0", "fff"),
          )

          then HTML should be

          <ul>
          <li>jjj</li>
          <ul>
          <li>bbb</li>
          </ul>
          </li>
          <li>fff</li>
          <ul>

          or it have to be

          <ul>
          <li>fff</li>
          <li>jjj</li>
          <ul>
          <li>bbb</li>
          </ul>
          <ul>

          (sorted by name of parent keys)

          Comment

          • lp

            #6
            Re: How to create html list from such array ?

            Ivan Omelchenko 608308824 wrote:[color=blue]
            > more little question: is you array sorted correctly?
            > if your array has
            > $myArr = array(
            > array("jj", "0", "jjj"),
            > array("bb", "ee", "bbb"),
            > array("aa", "0", "fff"),
            > )[/color]

            This array ^ is wrong. There can't be any row with idParent = 'ee' if
            there's no row with id = 'ee'.

            Regards,
            LB


            Comment

            • Ivan Omelchenko 608308824

              #7
              Re: How to create html list from such array ?

              lp ÐÉÛÅÔ:[color=blue]
              > Ivan Omelchenko 608308824 wrote:
              >[color=green]
              >>more little question: is you array sorted correctly?
              >>if your array has
              >>$myArr = array(
              >> array("jj", "0", "jjj"),
              >> array("bb", "ee", "bbb"),
              >> array("aa", "0", "fff"),
              >>)[/color]
              >
              >
              > This array ^ is wrong. There can't be any row with idParent = 'ee' if
              > there's no row with id = 'ee'.
              >
              > Regards,
              > LB
              >
              >[/color]
              I mean
              $myArr = array([color=blue]
              > array("jj", "0", "jjj"),
              > array("bb", "jj", "bbb"),
              > array("aa", "0", "fff"),
              > )[/color]

              Comment

              • Ewoud Dronkert

                #8
                Re: How to create html list from such array ?

                On Mon, 30 May 2005 13:29:31 +0200, lp wrote:[color=blue]
                > $myArr = array(
                > array("jj", "0", "jjj"),
                > array("ee", "0", "eee"),
                > array("bb", "ee", "bbb"),
                > array("ll", "ee", "lll"),
                > array("ff", "0", "fff"),
                > )
                >
                > <ul>
                > <li>jjj</li>
                > <li>eee
                > <ul>
                > <li>bbb</li>
                > <li>eee</li>
                > </ul>
                > </li>
                > <li>fff</li>
                > <ul>[/color]

                How about

                $html = "<ul>\n";
                $prv = -1;

                foreach ( $myArr as $a )
                {
                $cur = $a[1];
                $dat = "\t<li>{$a[2]}";

                if ( $prv !== -1 )
                if ( strcmp( $cur, '0' ) == 0 )
                {
                $dat = "</li>\n$dat";
                if ( strcmp( $prv, '0' ) != 0 )
                $dat = "\t\t</ul>\n\t$dat";
                }
                else
                {
                $dat = "\t\t$dat</li>\n";
                if ( strcmp( $prv, '0' ) == 0 )
                $dat = "\n\t\t<ul>\n$d at";
                }

                $html .= $dat;
                $prv = $cur;
                }

                if ( strcmp( $prv, '0' ) != 0 )
                $html .= "\t\t</ul>\n\t";
                $html .= "</li>\n</ul>\n";


                --
                Firefox Web Browser - Rediscover the web - http://getffox.com/
                Thunderbird E-mail and Newsgroups - http://gettbird.com/

                Comment

                Working...