开发者

php - create columns from mysql list

开发者 https://www.devze.com 2023-02-04 06:24 出处:网络
I have a long list generated from a simple mysql select query. Currently (shown in the code below) I am simply creating list of table rows with each record. So, nothing complicated.

I have a long list generated from a simple mysql select query.

Currently (shown in the code below) I am simply creating list of table rows with each record. So, nothing complicated.

However, I want to divide it into more than one column, depending on the number of returned results. I've been wrapping my brain around how to count this in the php, and I'm not getting the results I need.

<table>    
    <? 
         $query = mysql_query("SELECT * FROM `sometable`");
         while($rows = mysql_fetch_array($query)){    
    ?>开发者_开发技巧
            <tr>
              <td><?php echo $rows['someRecord']; ?></td>
            </tr>    
   <? } ?>          
</table>

Obviously there's one column generated. So if the records returned reach 10, then I want to create a new column. In other words, if the returned results are 12, I have 2 columns. If I have 22 results, I'll have 3 columns, and so on.


Simply do

<?php

while($row = mysql_fetch_row($query)) {
    print "<tr>\n";
    print "\t<td>";
    foreach($row as $i => $value) {
        if ($i >= 10 && $i % 10 === 0) {
            print "</td>\n\t<td>";
        }
        print $value;
    }
    print "</td>\n";
    print "</tr>\n";
}


There's probably an easier way to do this, but I gave it a shot:

<table>    
    <? 
         $result = mysql_query("SELECT * FROM `sometable`");
         $maxRows = 10;
         $requiredColumns = ceil($numRows/$maxRows);
         $rowArray = array();
         while($row = mysql_fetch_assoc($result)){  
            $rowArray[] = $row;
         }
         $tableArray = array();
         $rowCounter = 0;
         $columnCounter = 1;
         foreach($rowArray as $row){
            if($rowCounter % $maxRows == 0)
               $columnCounter++;
            $tableArray[$columnCounter][] = $row['someRecord'];

         }

         for($i = 0; $i < $maxRows; $i++){
            echo "<tr>"
            for($k = 1; $k <= $requiredColumns; $k++){
                $cellContent = isset($tableArray[$k][$i])? $tableArray[$k][$i] : "&nbsp;";
                echo "<td>$cellContent</td>";
                $k++;
            }
            $i++;
            echo "</tr>"
         }

    ?>       
</table>


Interesting question; my first two stabs at this were wrong (as seen in the comments below). I'm assuming you don't want to concat the elements together, but instead want to create a proper table. If concat works for you, by all means use the other solution here as it's far more simple and elegant.

<?php
$totalCount = mysql_num_rows($query);
$colMax     = 10;
$colcount   = ceil($totalCount / $colMax);

$counter = 0;
while($row = mysql_fetch_assoc($query)) {
   echo "<tr>";
   for($i = 0; $i < $colCount; $i++) {
      echo "<td>".$row['someRecord']."</td>";
   }
   echo "</tr>";   
}
?>


I shortened the James William version a bit:

    $maxRows = 10;

    $tableArray = array();
    $rownum = 0;

    $result = mysql_query("SELECT * FROM `sometable`");


    while($row = mysql_fetch_assoc($result))
    {
        $rownum = $rownum % $maxRows; 
        $tableArray[$rownum][] = $row['record'];        
        $rownum++;  
    }

    // If you want empty table cells, just finish the cycle, else skip it:
    for( ; $rownum < $maxRows; $rownum++)
    {   
        $tableArray[$rownum][] = "";        
    }

    foreach( $tableArray as $rowArray )
    {
        echo "<tr><td>";
        echo implode( "</td><td>", $rowArray);
        echo "</td></tr>\n";
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消