Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-从mysql列表创建列_Php_Mysql - Fatal编程技术网

php-从mysql列表创建列

php-从mysql列表创建列,php,mysql,Php,Mysql,我有一个由简单的mysql select查询生成的长列表。 目前(如下面的代码所示),我只是用每条记录创建表行列表。没什么复杂的 但是,我想根据返回结果的数量将其划分为多个列。我一直在绞尽脑汁研究如何在php中计算这些,但我没有得到我需要的结果 <table> <? $query = mysql_query("SELECT * FROM `sometable`"); while($rows = mysql_fetch_arr

我有一个由简单的mysql select查询生成的长列表。
目前(如下面的代码所示),我只是用每条记录创建表行列表。没什么复杂的

但是,我想根据返回结果的数量将其划分为多个列。我一直在绞尽脑汁研究如何在php中计算这些,但我没有得到我需要的结果

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


显然,生成了一列。因此,如果返回的记录达到10,那么我想创建一个新列。换句话说,如果返回的结果是12,则有2列。如果我有22个结果,我将有3列,以此类推。

有趣的问题;我的前两次尝试是错误的(如下面的评论所示)。我假设您不希望将元素合并在一起,而是希望创建一个适当的表。如果concat适合您,请务必使用其他解决方案,因为它更简单、更优雅

<?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>";   
}
?>

有趣的问题;我的前两次尝试是错误的(如下面的评论所示)。我假设您不希望将元素合并在一起,而是希望创建一个适当的表。如果concat适合您,请务必使用其他解决方案,因为它更简单、更优雅

<?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>";   
}
?>

可能有一种更简单的方法,但我尝试了一下:

<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>

可能有一种更简单的方法,但我尝试了一下:

<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>

简单地做

<?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";
}
只要做

<?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";
}

我把詹姆斯·威廉的版本缩短了一点:

    $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";
    }
$maxRows=10;
$tableArray=array();
$rownum=0;
$result=mysql\u查询(“从`sometable`中选择*”;
while($row=mysql\u fetch\u assoc($result))
{
$rownum=$rownum%$maxRows;
$tableArray[$rownum][]=$row['record'];
$rownum++;
}
//如果需要空表单元格,只需完成循环,否则跳过:
对于(;$rownum<$maxRows;$rownum++)
{   
$tableArray[$rownum][]=“”;
}
foreach($tableArray作为$rowArray)
{
回声“;
回波内爆(“,$rowArray);
回音“\n”;
}

我把詹姆斯·威廉的版本缩短了一点:

    $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";
    }
$maxRows=10;
$tableArray=array();
$rownum=0;
$result=mysql\u查询(“从`sometable`中选择*”;
while($row=mysql\u fetch\u assoc($result))
{
$rownum=$rownum%$maxRows;
$tableArray[$rownum][]=$row['record'];
$rownum++;
}
//如果需要空表单元格,只需完成循环,否则跳过:
对于(;$rownum<$maxRows;$rownum++)
{   
$tableArray[$rownum][]=“”;
}
foreach($tableArray作为$rowArray)
{
回声“;
回波内爆(“,$rowArray);
回音“\n”;
}

这还不能回答问题!说得好。。读得太快了。对我的答案进行了编辑,现在可以了。现在每10个元素创建一行,而不是一列。我认为我的解决方案是正确的。这并不能回答问题!说得好。。读得太快了。对我的答案进行了编辑,现在可以了。现在每10个元素创建一行,而不是一列。我认为我的解决办法是把它做好。