【问题标题】:show information from mysql in 4*4 table in html在 html 的 4*4 表中显示来自 mysql 的信息
【发布时间】:2016-03-24 02:18:10
【问题描述】:

我有一个包含 16 条记录的字段的表。我想在 4*4 的桌子上展示它们,但我不知道该怎么做。我的代码将它们显示在一行中。

<table class="table">
    <thead>
        <tr class="mybg">
            <?php   
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "SELECT * FROM tutors_students where tutor_id = ? and ok=1 ORDER BY date DESC ";
            $q = $pdo->prepare($sql);
            $q->execute(array($userid));
            $data =  $q->setFetchMode(PDO::FETCH_ASSOC);
            foreach($q->fetchAll() as $k=>$v) {  ?>
            <th><?php echo $v['date'];?> </th>
            <?php   }
            Database::disconnect();
            ?>
        </tr>
    </thead>
    <tbody class="mybg1">

【问题讨论】:

  • 添加一个计数器,在每个循环中增加,一旦它为 3,在输出中添加一个"&lt;/tr&gt;&lt;tr&gt;",将计数器重置为 0
  • 4 列和 4 行。也许我的行会更多

标签: php html mysql pdo


【解决方案1】:

添加一个计数器,在每个循环中增加一个;
一旦你有 4 列打印一个新行并重置计数器:

$cnt=0;
foreach($q->fetchAll() as $k=>$v) {
    $cnt++;
    echo '<th>'. $v['date'].'</th>';

    if($cnt===4) {
        echo "</tr><tr>";
        $cnt=0;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多