Array 4-spaltig ausgeben

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

  • Array 4-spaltig ausgeben

    Hallo, versuche gerade eine Extension anzupassen und bin leider kein PHP-Profi. Ich versuche folgenden Array 4-spaltig auszugeben. Wer kann mir helfen? Vielen Dank.

    PHP-Code:
       1.
          
    function makelist($res) {
       
    2.
                          $items
    =Array();
       
    3.
                                  
    // Make list table rows
       
    4.
                          
    while($this->internal["currentRow"] = mysql_fetch_assoc($res))  {
       
    5.
                                  $items
    []=$this->makeListItem();
       
    6.
                          
    }
       
    7.
                 
       8.
                          $out 
    '<table width="540"><tr><td>
       9.
                                  '
    .implode(chr(10),$items).'
      10.
                                  </td></tr></table>'
    ;
      
    11.
                          
    return $out;
      
    12.
                  


  • #2
    Hilft Dir das hier weiter?


    // Function for reading a single dimension (column) array into a multiple dimension array
    // Copyright 2000 by J.G. Smirniotopoulos
    // Made available for non-commercial public use, provided this copyright attribution is intact
    // Requires:
    // $cols = number of columns for output array
    // $buffers = single column array with cell values separated by commas ( , )
    //
    // NOTE: You can clearly organize your data into columns this way;
    // The first row (0) can optionally be used for column headings;
    // Each entry (line) must end with a comma ( , );
    //
    // SAMPLE Array using comma [,] as separator;
    //
    $buffers="
    Numbers, RomanNumerals, English, Greek,
    1, I, one, ena,
    2, II, two, dio,
    3, III, three, tria,
    4, IV, four, tecera
    ";// end string;

    $buff = explode (",", $buffers);

    ////////////////////////////
    // Create Multidimensional Array
    //
    // requires array $buff;
    // requires number of columns for output table $cols;

    $cols=4;// define number of columns here;

    function make_table($cols,$buff){

    $rows=((count($buff))/$cols); // calculate number of rows;
    echo "Total Items = ".count($buff)."";
    echo "Columns = ".$cols."";
    echo "Total Rows = ".$rows."";
    echo "<table valign=top align=center border=2 bgcolor=blue>";
    $item=0;
    while ($item < (count($buff))):
    for ($row=0; $row < $rows; $row++){
    if (($row%2)==0){echo "<tr bgcolor=orange>";}else{echo "<tr bgcolor=yellow>";};
    if ($row>0){
    echo "<td>Row Number ". ($row)." </td>";
    }else{
    echo "<td>Column Titles ==></td>";
    }
    for ($col=0; $col < $cols; $col++){
    $myarray[$row][$col]=$buff[$item];
    echo "<td>".$myarray[$row][$col] ;// optional display;
    // if ((($col+1)%$cols)!=0) {
    echo " - is associated with - ";
    };// optional display;
    echo "</td>";
    $item++;// loop items;
    }// loop columns;
    echo "";
    echo "</tr>";
    }// loop rows;
    endwhile ;
    echo "</table>";
    }// end function make_table;

    make_table(4,$buff);// number of columns, input array;
    //////////////////////////
    // End PHP processing;
    ?>

    Kommentar


    • #3
      Danke für die Hilfe! Hab den Code angeschaut, bin allerdings nicht in der Lage diesen anzuwenden. Da reichen meine PHP-Kenntnisse effektiv nicht aus.

      Kommentar


      • #4
        Probier mal die items zu zählen, dann rechnest du aus, wieviel Zeilen sich mit je 4 Spalten ergeben werden.
        Soviel Zeilen du hast läßt du <tr> ausgeben und jedes mal dabei 4 <td>

        Will jetzt die HTML Ausgabe nicht posten, weil ich es gerad alles nciht testen kann...

        PHP-Code:
         function makelist($res
        {
               
        $items=Array();
            while(
        $this->internal["currentRow"] = mysql_fetch_assoc($res))  
            {
                
        $items[]=$this->makeListItem();
                }
            
        $anzahl_der_items count ($items);
            
        $zeilen=round($anzahl_der_items/4);
            
            for(
        $a=0;$a<$zeilen; ++$a)
            {

                
            }


        Wie man die Ausgabe formatiert, könnte dir ein Thread von mir helfen:
        http://php-resource.de/forum/showthr...threadid=60302
        Zuletzt geändert von archie; 23.11.2005, 09:46.

        Kommentar


        • #5
          Hey, vielen Dank Euch allen. Hab einen Lösungsansatz, der mir jetzt die ersten 4 in einer Tabelle ausgibt. Leider die restlichen nicht. Vielleicht könnt Ihr mir hier noch helfen:

          PHP-Code:
          function makelist($res)    {
                  
          $x 1;
                  
          $items=Array();
                      
          // Make list table rows
                  
          while($this->internal["currentRow"] = mysql_fetch_assoc($res))    {
                      
          $items[]=$this->makeListItem();
                      if(
          $x%4==0)
                        {
                        
          $out '<table><tr>'.implode(chr(10),$items).'</tr></table>';
                        return 
          $out;
                        }
                        
          $x++;
                  }
              
                  
          $out '<table><tr>
                      '
          .implode(chr(10),$items).'
                      </tr></table>'
          ;
                  return 
          $out;
              } 

          Kommentar


          • #6
            fast:
            PHP-Code:
            while...
            {
               
            $out .= '<table>';
               if(
            $x%4==0$out .= '<tr>';
               
            $out .= '<td>'.implode(chr(10),$items).'</td>';
               if(
            $x%4==0$out .= '</tr>';
               
            $out .= '</table>';

            ungetestet.

            gruß
            peter
            Nukular, das Wort ist N-u-k-u-l-a-r (Homer Simpson)
            Meine Seite

            Kommentar


            • #7
              Nein, funktioniert leider nicht

              Kommentar


              • #8
                Du willst doch sicherlich jedes Mal 4 Spalten ausgeben und die Anzahl der Zeilen soll sich dynamisch ändern, je nach dem, wieviel Datensätze du hast?

                Dann musst du eine schleife laufen lassen mit der du die Zeilen ausgibst und in der Schleife noch eine, für die Spaltenausgabe.

                Zeilen kannst du solange ausgeben, wie du "Datensätze/4" hast und spalten immer auf 4 Stück begrenzt.

                Orientiere dich an die Ausgabe: http://php-resource.de/forum/showthr...threadid=60302

                Wenn du was nicht verstehst, kannst du ruhig fragen.

                Kommentar


                • #9
                  naja, komme nicht weiter. das hirn raucht ...

                  trotzdem danke.

                  Kommentar


                  • #10
                    Hab's doch noch hingekriegt. Danke an alle die geholfen haben:

                    PHP-Code:
                    function makelist($res
                    {
                           
                    $items=Array();
                           
                        while(
                    $this->internal["currentRow"] = mysql_fetch_assoc($res))  
                        {
                            
                    $items[]=$this->makeListItem();
                            }
                        
                            
                    // wieviel haben wir denn ?
                            
                    $count count$items );
                            
                            
                    // es sollen 3 Spalten werden
                            
                    $spalten 4;
                            
                    // macht so und so viel Zeilen
                            
                    $zeilen = ( $count $spalten )?( ( $count - ( $count $spalten ) ) / $spalten ):$count $spalten;
                            
                            
                    // Tabelle zeilenweise ausgeben
                            
                    $out '<table>';
                            
                    // Zeile für Zeile
                            
                    for( $i 0;$i $zeilen; ++$i ) {
                                
                    $out.= '<tr>'
                                
                    // Spalte für Spalte
                                
                    for( $ii 0;$ii $spalten; ++$ii ) {
                                    
                    // wenns nichts mehr gibt, ein &nbsp;
                                    
                    $out.= '<td>' . ( isset( $items[$i $spalten $ii] )?$items[$i $spalten $ii]: '&nbsp;') . '</td>';
                                } 
                                
                    $out.= '</tr>';
                            } 
                            
                    $out.= '</table>';
                            
                            
                            return 
                    $out;

                    Kommentar

                    Lädt...
                    X