Php 把桌子分成三行

Php 把桌子分成三行,php,html-table,Php,Html Table,我有一个PHP页面,其中一些类似于: if(mysql_num_rows($raw_results) > 0){ echo "<table border='1'> <tr> <th>C1</th> </tr>"; while($row = mysql_fetch_array($raw_results)) { echo "<tr>"; echo "<td>" . $row['C1'] . "&l

我有一个PHP页面,其中一些类似于:

if(mysql_num_rows($raw_results) > 0){ 

    echo "<table border='1'>
<tr>
<th>C1</th>
</tr>";

while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
echo "</tr>";
}
echo "</table>";
$cpt=1;
echo "<tr>";
while($row = mysql_fetch_array($raw_results))
{
  echo "<td>" . $row['C1'] . "</td>";
  if ($cpt%3==0)
    echo "</tr><tr>";
  $cpt++;
}
echo "</tr>";
if(mysql_num_rows($raw_results) > 0){ 
    echo "<table border='1'>
    <tr>
    <th>C1</th>
    <th>C1</th>
    <th>C1</th>
    </tr><tr>";
    $i = 0;
    while($row = mysql_fetch_array($raw_results))
    {
        if ($i == 2) {
            $i = 0;
            echo "</tr>";
            echo "<tr>";
        }
        echo "<td>" . $row['C1'] . "</td>";
        $i++;
    }
    if ($i > 0) {
        echo "</tr>";
    }
    echo "</table>";
if(mysql\u num\u行($raw\u结果)>0){
回声“
C1
";
while($row=mysql\u fetch\u数组($raw\u results))
{
回声“;
回显“$row['C1']”;
回声“;
}
回声“;
我怎样才能改变它,使表格变成C1

因此,如果我有以下一组值-123456789-表返回以下值

C1 C1

1 2 3

4 5 6

7 8 9

我如何才能做到这一点?

将其更改为

if(mysql_num_rows($raw_results) > 0){ 
  echo "<table border='1'>
  <tr>
     <th>C1</th><th>C1</th><th>C1</th>";

$x = 0;
while($row = mysql_fetch_array($raw_results))
{
   if ($x % 3 == 0) {
     echo "</tr><tr>";
   }
   echo "<td>" . $row['C1'] . "</td>";
   $x++;
}
echo "</table>";
if(mysql\u num\u行($raw\u结果)>0){
回声“
C1C1”;
$x=0;
while($row=mysql\u fetch\u数组($raw\u results))
{
如果($x%3==0){
回声“;
}
回显“$row['C1']”;
$x++;
}
回声“;
我假设您只有3列,并且结果的计数是3的次倍数
注意

将其更改为

if(mysql_num_rows($raw_results) > 0){ 
  echo "<table border='1'>
  <tr>
     <th>C1</th><th>C1</th><th>C1</th>";

$x = 0;
while($row = mysql_fetch_array($raw_results))
{
   if ($x % 3 == 0) {
     echo "</tr><tr>";
   }
   echo "<td>" . $row['C1'] . "</td>";
   $x++;
}
echo "</table>";
if(mysql\u num\u行($raw\u结果)>0){
回声“
C1C1”;
$x=0;
while($row=mysql\u fetch\u数组($raw\u results))
{
如果($x%3==0){
回声“;
}
回显“$row['C1']”;
$x++;
}
回声“;
我假设您只有3列,并且结果的计数是3的次倍数
请注意如下内容:

if(mysql_num_rows($raw_results) > 0){ 

    echo "<table border='1'>
<tr>
<th>C1</th>
</tr>";

while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
echo "</tr>";
}
echo "</table>";
$cpt=1;
echo "<tr>";
while($row = mysql_fetch_array($raw_results))
{
  echo "<td>" . $row['C1'] . "</td>";
  if ($cpt%3==0)
    echo "</tr><tr>";
  $cpt++;
}
echo "</tr>";
if(mysql_num_rows($raw_results) > 0){ 
    echo "<table border='1'>
    <tr>
    <th>C1</th>
    <th>C1</th>
    <th>C1</th>
    </tr><tr>";
    $i = 0;
    while($row = mysql_fetch_array($raw_results))
    {
        if ($i == 2) {
            $i = 0;
            echo "</tr>";
            echo "<tr>";
        }
        echo "<td>" . $row['C1'] . "</td>";
        $i++;
    }
    if ($i > 0) {
        echo "</tr>";
    }
    echo "</table>";
$cpt=1;
回声“;
while($row=mysql\u fetch\u数组($raw\u results))
{
回显“$row['C1']”;
如果($cpt%3==0)
回声“;
$cpt++;
}
回声“;
将在每3个结果后创建一个新行,如下所示:

if(mysql_num_rows($raw_results) > 0){ 

    echo "<table border='1'>
<tr>
<th>C1</th>
</tr>";

while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
echo "</tr>";
}
echo "</table>";
$cpt=1;
echo "<tr>";
while($row = mysql_fetch_array($raw_results))
{
  echo "<td>" . $row['C1'] . "</td>";
  if ($cpt%3==0)
    echo "</tr><tr>";
  $cpt++;
}
echo "</tr>";
if(mysql_num_rows($raw_results) > 0){ 
    echo "<table border='1'>
    <tr>
    <th>C1</th>
    <th>C1</th>
    <th>C1</th>
    </tr><tr>";
    $i = 0;
    while($row = mysql_fetch_array($raw_results))
    {
        if ($i == 2) {
            $i = 0;
            echo "</tr>";
            echo "<tr>";
        }
        echo "<td>" . $row['C1'] . "</td>";
        $i++;
    }
    if ($i > 0) {
        echo "</tr>";
    }
    echo "</table>";
$cpt=1;
回声“;
while($row=mysql\u fetch\u数组($raw\u results))
{
回显“$row['C1']”;
如果($cpt%3==0)
回声“;
$cpt++;
}
回声“;

将在每3个结果后创建一个新行

无测试,因此可能存在语法错误,但您可以执行以下操作:

if(mysql_num_rows($raw_results) > 0){ 

    echo "<table border='1'>
<tr>
<th>C1</th>
</tr>";

while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
echo "</tr>";
}
echo "</table>";
$cpt=1;
echo "<tr>";
while($row = mysql_fetch_array($raw_results))
{
  echo "<td>" . $row['C1'] . "</td>";
  if ($cpt%3==0)
    echo "</tr><tr>";
  $cpt++;
}
echo "</tr>";
if(mysql_num_rows($raw_results) > 0){ 
    echo "<table border='1'>
    <tr>
    <th>C1</th>
    <th>C1</th>
    <th>C1</th>
    </tr><tr>";
    $i = 0;
    while($row = mysql_fetch_array($raw_results))
    {
        if ($i == 2) {
            $i = 0;
            echo "</tr>";
            echo "<tr>";
        }
        echo "<td>" . $row['C1'] . "</td>";
        $i++;
    }
    if ($i > 0) {
        echo "</tr>";
    }
    echo "</table>";
if(mysql\u num\u行($raw\u结果)>0){
回声“
C1
C1
C1
";
$i=0;
while($row=mysql\u fetch\u数组($raw\u results))
{
如果($i==2){
$i=0;
回声“;
回声“;
}
回显“$row['C1']”;
$i++;
}
如果($i>0){
回声“;
}
回声“;

无测试,因此可能是语法错误,但您可以执行以下操作:

if(mysql_num_rows($raw_results) > 0){ 

    echo "<table border='1'>
<tr>
<th>C1</th>
</tr>";

while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
echo "</tr>";
}
echo "</table>";
$cpt=1;
echo "<tr>";
while($row = mysql_fetch_array($raw_results))
{
  echo "<td>" . $row['C1'] . "</td>";
  if ($cpt%3==0)
    echo "</tr><tr>";
  $cpt++;
}
echo "</tr>";
if(mysql_num_rows($raw_results) > 0){ 
    echo "<table border='1'>
    <tr>
    <th>C1</th>
    <th>C1</th>
    <th>C1</th>
    </tr><tr>";
    $i = 0;
    while($row = mysql_fetch_array($raw_results))
    {
        if ($i == 2) {
            $i = 0;
            echo "</tr>";
            echo "<tr>";
        }
        echo "<td>" . $row['C1'] . "</td>";
        $i++;
    }
    if ($i > 0) {
        echo "</tr>";
    }
    echo "</table>";
if(mysql\u num\u行($raw\u结果)>0){
回声“
C1
C1
C1
";
$i=0;
while($row=mysql\u fetch\u数组($raw\u results))
{
如果($i==2){
$i=0;
回声“;
回声“;
}
回显“$row['C1']”;
$i++;
}
如果($i>0){
回声“;
}
回声“;

$N
中定义您需要多少Col

$N=3;
$col=0;
while($row = mysql_fetch_array($raw_results))
{
    $col++;
    if ($col==1) echo "<tr>";
    echo "<td>" . $row['C1'] . "</td>";
    if ($col==$N)
    {
        $col=0;
        echo "</tr>";
    }
}
if ($col>0) //if there is an open <tr>...
    echo "</tr>";
$N=3;
$col=0;
while($row=mysql\u fetch\u数组($raw\u results))
{
$col++;
如果($col==1)回显“”;
回显“$row['C1']”;
如果($col==$N)
{
$col=0;
回声“;
}
}
if($col>0)//如果存在打开的。。。
回声“;

$N
中定义您需要多少Col

$N=3;
$col=0;
while($row = mysql_fetch_array($raw_results))
{
    $col++;
    if ($col==1) echo "<tr>";
    echo "<td>" . $row['C1'] . "</td>";
    if ($col==$N)
    {
        $col=0;
        echo "</tr>";
    }
}
if ($col>0) //if there is an open <tr>...
    echo "</tr>";
$N=3;
$col=0;
while($row=mysql\u fetch\u数组($raw\u results))
{
$col++;
如果($col==1)回显“”;
回显“$row['C1']”;
如果($col==$N)
{
$col=0;
回声“;
}
}
if($col>0)//如果存在打开的。。。
回声“;
if(mysql\u num\u行($raw\u结果)>0){
回声“
C1C1”;
$col=0;
while($row=mysql\u fetch\u数组($raw\u results)){
如果($col==0){echo”“;}
回显“$row['C1']”;
如果($col==2){echo”“;}
$col=($col+1)%3
}
回声“;
}
if(mysql\u num\u行($raw\u结果)>0){
回声“
C1C1”;
$col=0;
while($row=mysql\u fetch\u数组($raw\u results)){
如果($col==0){echo”“;}
回显“$row['C1']”;
如果($col==2){echo”“;}
$col=($col+1)%3
}
回声“;
}
这也在起作用(稍微短一点)

$a=1;
while($row=mysql\u fetch\u数组($raw\u results))
{
回显“$row['c1']”(($a%3==0)?“”:“”);
$a++;
}
这也在起作用(稍微短一点)

$a=1;
while($row=mysql\u fetch\u数组($raw\u results))
{
回显“$row['c1']”(($a%3==0)?“”:“”);
$a++;
}

一个简要说明:我看到您正在使用
mysql\uucode>函数。这一系列函数在PHP中已弃用,在未来的版本中将不可用。强烈建议新代码开始使用未弃用的API进行数据库访问,如
mysqli
或PDO。有关详细信息,请参阅文档:True-谢谢-我还没有看到。这是我正在更新的一个旧页面…一个简短的提示:我看到你正在使用
mysql\uuu
函数。这一系列函数在PHP中已弃用,在将来的版本中将不再可用。强烈建议新代码开始使用未弃用的API进行数据库访问,如
mysqli
或P请查看文档了解更多信息:真的-谢谢-我没有看到。这是一个旧页面,我正在更新。。。