problem beim array umstricken ...

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • problem beim array umstricken ...

    Tach erstmal ...

    Folgendes Problem hab ich ...

    ich hab eine tabelle für kategorien

    id | parentid | kategorie
    1 0 top1
    2 1 sub1
    3 2 subsub1

    nun les ich die tabelle (alle einträge (brauche auch alle)) mit
    ORDER BY parentid ASC
    aus und erhalten nen schickes array mit

    PHP-Code:
    Array
    (
        [
    0] => Array
            (
                [
    id] => 1
                
    [parentid] => 0
                
    [kategorie] => top1
            
    )
        [
    1] => Array
            (
                [
    id] => 2
                
    [parentid] => 1
                
    [kategorie] => sub1
            
    )
        [
    2] => Array
            (
                [
    id] => 3
                
    [parentid] => 2
                
    [kategorie] => subsub1
            
    )

    nun hätt ich das array gern

    PHP-Code:
    Array
    (
        [
    0] => Array
            (
                [
    id] => 1
                
    [parentid] => 0
                
    [kategorie] => top1
                
    [sub] => Array
                    (
                      [
    id] => 2
                      
    [parentid] => 1
                      
    [kategorie] => sub1
                      
    [sub] => Array
                          (
                            [
    id] => 3
                            
    [parentid] => 2
                            
    [kategorie] => subsub1
                          
    )

                    )

            )

    komm aber nicht dahinter wie das hinbekomme ...

    Sorry vielleicht denk ich auch nur falsch ???

    das ergebniss könnte dann ein string in diesem format sein

    top1/sub1
    top1/sub1/subsub1
    Zuletzt geändert von BetaSux; 25.07.2003, 22:57.
    Splinter Cell Base

  • #2
    Willste nen Baum bauen?

    http://www.php-resource.de/forum/sho...threadid=23059
    Konrad

    In a world without walls and fences, who needs Windows and Gates ?
    (Sun Microsystems)

    Kommentar


    • #3
      nicht wirklich ...

      ich will halt nur alle subkategorien eingeordnet in das array der hauptkategorie, will nicht wirklich nen tree haben nur eben Ordnung im system und vorallem verstehen wie ich das prob löse , weil irgendwie hackt es da oben im stübchen und ich komm nicht auf die lösung ...

      wie kann ich denn in einem mehrdimensionialen array überprüfen ob die parentid des aktuelen arrays gleich der id eines eintrages in diesem mehrdimensionialen array ist ???
      Splinter Cell Base

      Kommentar


      • #4
        dann musste dir wohl ne kleine funktion basteln

        PHP-Code:
        $temp mysql_query("SELECT * FROM tabelle");
        while(
        $bla mysql_fetch_array($temp)) {
        if(
        $bla['parentid']) {
        $array[$bla['parentid']] = array('id'=>$bla['id'],'parentid'=>$bla['parentid'],'kategorie'=>$bla['kategorie']);
        }
        else {

        $array[] = array('id'=>$bla['id'],'parentid'=>$bla['parentid'],'kategorie'=>$bla['kategorie']);
        }

        ich glaub kaum das das funktioniert jetzt
        aber da haste schon mal einen ansatz!
        musst ma bisl weiter überlegen
        mfg

        Kommentar


        • #5
          Danke Trashar ...

          vorallem tat nicht mal weh die Kopfnuss :/

          fertige funktion

          PHP-Code:
          function get_categorie () {
              
              global 
          $db_host$db_name$db_user$db_pass$db_tab;
              
              
          $dbcon = new db_bsx($db_host$db_name$db_user$db_pass);
              
          $result $dbcon->db_fetch_multi("SELECT * FROM ".$db_tab[kats]." ORDER BY parentid ASC");
              
          $close $dbcon->close();
              if(
          $result) {
                  foreach(
          $result as $query_detail) {
                      foreach(
          $query_detail as $key=>$elem) {
                          if(
          $elem[parentid] == 0) {
                              
          $array_topcat["".$elem[id].""] = $elem;
                          }
                          if(
          $elem[parentid] > 0) {
                              
          $array_topcat["".$elem[parentid].""][sub][] = $elem;
                          }
                      }
                  }
              }
              return 
          $array_topcat;

          ergibt folgendes array

          PHP-Code:
          Array
          (
              [
          2] => Array
                  (
                      [
          0] => 2
                      
          [id] => 2
                      
          [1] => 0
                      
          [parentid] => 0
                      
          [2] => media
                      
          [kategorie] => media
                      
          [sub] => Array
                          (
                              [
          0] => Array
                                  (
                                      [
          0] => 1
                                      
          [id] => 1
                                      
          [1] => 2
                                      
          [parentid] => 2
                                      
          [2] => artworks
                                      
          [kategorie] => artworks
                                  
          )

                              [
          1] => Array
                                  (
                                      [
          0] => 9
                                      
          [id] => 9
                                      
          [1] => 2
                                      
          [parentid] => 2
                                      
          [2] => test->1
                                      
          [kategorie] => test->1
                                  
          )

                          )

                  )

              [
          3] => Array
                  (
                      [
          0] => 3
                      
          [id] => 3
                      
          [1] => 0
                      
          [parentid] => 0
                      
          [2] => gamefaq
                      
          [kategorie] => gamefaq
                      
          [sub] => Array
                          (
                              [
          0] => Array
                                  (
                                      [
          0] => 4
                                      
          [id] => 4
                                      
          [1] => 3
                                      
          [parentid] => 3
                                      
          [2] => general
                                      
          [kategorie] => general
                                  
          )

                              [
          1] => Array
                                  (
                                      [
          0] => 5
                                      
          [id] => 5
                                      
          [1] => 3
                                      
          [parentid] => 3
                                      
          [2] => story
                                      
          [kategorie] => story
                                  
          )

                          )

                  )

              [
          10] => Array
                  (
                      [
          0] => 10
                      
          [id] => 10
                      
          [1] => 0
                      
          [parentid] => 0
                      
          [2] => test-top-ohne-sub
                      
          [kategorie] => test-top-ohne-sub
                  
          )



          ist es eigentlich sinnvoll am ende einer funktion alle variablen/arrays zu unseten ???
          Zuletzt geändert von BetaSux; 26.07.2003, 13:51.
          Splinter Cell Base

          Kommentar

          Lädt...
          X