0

我对数据库进行查询,并希望在 3 列 4 行的表中获取数据。什么给出了 12 个元素。但是让我们想象一下,在数据库中,有 13 个元素,我将创建另一个表。

我的代码:

$result = mysql_query("SELECT * FROM gallery");

if(mysql_num_rows($result) > 0)
{
     while($row = mysql_fetch_array($result))
     {
          echo $row['name_file']; //print what is inside the array
     }
}

我知道我必须做两个周期,但我遇到了麻烦。请问是打印数据的最佳方式吗?谢谢

4

5 回答 5

2

给你:

$i = 0;

if (mysql_num_rows($result) > 0) {
  while ($row = mysql_fetch_array($result)) {
    if ($i % 12 == 0) {
      // Table ID dont need to be rounded, because $i % 12 == 0, so $i / 12 is integer, not float...
      echo '<table id="Table'.($i / 12 + 1).'">'."\n";
    }

    if ($i % 3 == 0) {
      echo '<tr>'."\n";
    }

    echo '<td>'.$row['name_file'].'</td>'."\n";

    if ($i % 3 == 2) {
      echo '</tr>'."\n";
    }

    if ($i % 12 == 11) {
      echo '</table>'."\n";
    }

    $i++;
  }

  if ($i % 3 > 0) {
    echo '</tr>'."\n";
  }

  if ($i % 12 > 0) {
    echo '</table>'."\n";
  }
}

Fe 可以说,我们有 name_file = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19; 然后它会输出这个:

<table id="Table1">
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>4</td>
    <td>5</td>
    <td>6</td>
  </tr>
  <tr>
    <td>7</td>
    <td>8</td>
    <td>9</td>
  </tr>
  <tr>
    <td>10</td>
    <td>11</td>
    <td>12</td>
  </tr>
</table>
<table id="Table2">
  <tr>
    <td>13</td>
    <td>14</td>
    <td>15</td>
  </tr>
  <tr>
    <td>16</td>
    <td>17</td>
    <td>18</td>
  </tr>
  <tr>
    <td>19</td>
  </tr>
</table>
于 2013-10-24T08:44:12.230 回答
1

处理数据库的推荐方法是使用PDO

假设您将数据放入一个包含 15 个项目的数组中

$data = array_chunk($items, 12, true); 
// $data contains array(12 items), array(3 items), preserving array keys

foreach($data as $table)
{
    echo '<table>';
    foreach($table as $row)
    {
        printf('<tr><td>%s</td><td>%s</td></tr>', $row['field_name1'], $row['field_name2']);    
    }
    echo '</table>';
}
于 2013-10-24T09:04:26.940 回答
1

这将连续打印三个结果并根据需要继续执行。

if(mysql_num_rows($result) > 0)
{
 $vertical_rows = 1;
 echo "<table>";
 while($row = mysql_fetch_array($result))
 {
      if($vertical_rows == 1)echo "<tr>";
      echo "<td>".$row['name_file']."</td>";

      if($vertical_rows == 3){
         echo "</tr>";
         $vertical_rows = 1;
        }
 }
 echo "</table>";
}
于 2013-10-24T08:50:41.833 回答
1
$rows = 4; $cells = 3;
$result = mysql_query("SELECT * FROM gallery limit ".($rows * $cells));
$i = 0;
if (mysql_num_rows($result) > 0) {
    while($row = mysql_fetch_array($result)) {
        $i++;
        echo $row['name_file'];
        if ($i % $cells == 0) {
            echo "<br/>";
        } else {
            echo " ";
        }
    }
}

是你想要的吗?

于 2013-10-24T08:45:51.553 回答
-1

您的代码只打印没有单元格的行。不确定我是否理解正确,但如果你想打印一张表格,试试这个:

while($row = mysql_fetch_array($result)) {
    echo $row['name_file'];
    $i++;
    if($i >= 4) {
        echo "<br>";
        $i = 0;
    }
}

太愚蠢太阅读 - 未经测试,但应该工作。^^

echo "<table><tr>";
while($row = mysql_fetch_array($result)) {
    echo "<td>".$row['name_file']."</td>";
    $i++;
    if($i >= 4) {
        echo "</tr><tr>";
        $i = 0;
    }
}
echo "</table>"
于 2013-10-24T08:41:45.723 回答