-4

HTML で行ごとに 4 つのセルのみを許可する方法があるかどうかを知りたい..テーブルで許可されるのは 4 つだけです.4 つ以上の tds の場合、別の行が作成されます..

HTMLでそれをどのように制御できますか?

4

2 に答える 2

1

いいえ、あなたは何を求めているのか理解していないようです。htmlだけでこれを自動的に行うことはできません。あなたの質問は、Html と Php の両方に関するものです。

これは簡単な厄介な仕事です:

$pdo = new PDO("mysql:host=$host;dbname=$dbName", $userName, $pwd);

$query = $pdo->prepare('select name from products order by id asc');
$query->execute();

echo '<table>';
echo '<tbody>';
$numCellsInRow = 0;
while ($curRow = $query->fetch())
{
    if ($numCellsInRow == 4)
    {
        printf("</tr>");
        $numCellsInRow = 0;
    }
    if ($numCellsInRow == 0)
    {
        printf("<tr>");
    }
    printf("<td>%s</td>", $curRow[0]);

    $numCellsInRow++;
}
echo '</tbody>';
echo '</table>';

結果

<table>
  <tbody>
    <tr>
      <td>1" tweeter</td>
      <td>6" sub</td>
      <td>Sony Headphones</td>
      <td>Magnifier</td>
    </tr>
    <tr>
      <td>Red Led</td>
      <td>Blue LED</td>
      <td>7 segment LED</td>
      <td>Lalique stained glass</td>
    </tr>
    <tr>
      <td>LED downlight</td>
      <td>BiPole light-switch</td>
      <td>DC, 10amp</td>
      <td>Monster Cable</td>
    </tr>
    <tr>
      <td>Sata 2.0 Cable</td>
      <td>Speaker Wire</td>
      <td>USB 2.0 A to B Cable</td>
    </tr>
  </tbody>
</table>
于 2013-04-04T05:48:57.117 に答える
0

これを試してください..テストしていません..

 $count = 0; 
    echo("<table><tr>");
    foreach ($products as $product) { 
    if(($count % 4) == 0){

    echo("</tr><tr>\n");
    $count++;

    }if
    else{
    $count++;
    }

    echo("<td>$product<td>");

    }


    echo("</tr></table>"); 
于 2013-04-04T05:49:44.973 に答える