Хотите показать 3 записи в каждой строке таблицы в виде трех столбцов

0

У меня есть таблица, которая содержит 3 столбца. Запрос Sql1 получает имя магазина из базы данных и просто показывает все магазины в 1-м столбце, но я хочу отображать 3 записи в каждой строке в 3 столбцах. То есть, первые 3 записи в строке 1, следующие 3 записи на 2-м и так далее. Если я попытаюсь использовать смещение, у него также есть номер исправления, чтобы он не помог. Пожалуйста, помогите. Спасибо заранее

<style>
.vertical-menu {
     overflow-y: auto;
     float: left;
     position: fixed;
     width: 15%;
     left: 0;
     top: 9%;
bottom: 0;}
.header {
  background-color: #327a81;
  color: white;
  font-size: 1.5em;
  padding: 1rem;
  text-align: center;
  text-transform: uppercase;}
.vertical-menu a {
    background-color: #eee;
    color: black;
    display: block;
    padding: 12px;
    text-decoration: none;}
.vertical-menu a:hover {
    background-color: #4CAF50;}
.vertical-menu a.active {
    background-color: #4CAF50;
    color: white;
}</style></head>
<body>
<?php 
$con=mysql_connect('localhost','root','')or die(mysql_error());
$db=mysql_select_db('shop',$con) or die(mysql_error());
$sql='Select id,name from info';
$retrieval = mysql_query($sql,$con);

if(!$retrieval) {
    die('Could not get data'.mysql_error());
}

$id=$_GET['name'];
 echo "<div class='vertical-menu' >


<a href='#'>Dashboard</a>
<a href='#'>ShopList</a>
 ";
while ($row=mysql_fetch_array($retrieval, MYSQL_ASSOC)) {
}
echo "</div>";

$sql1="Select name from info limit 3 offset 1";
$retrieval1 = mysql_query($sql1,$con);

if(!$retrieval1) { 
    die('Could not get data'.mysql_error());
}
?>
<div class="col-xs-5 col-sm-5 col-md-5 col-lg-9 col-lg-offset-2"  style="margin-top: 5%;left:4%; ">
            <div class="header">List</div>
    <table  class="table table-striped">
    <thead><tr>
        <th>Shop Name</th>
        <th>Shop Name</th>
        <th>Shop Name</th>
    </tr></thead>
    <?php
while ($row1=mysql_fetch_array($retrieval1, MYSQL_ASSOC)) {
?>
 <tbody><tr>
<td><a href="#"><?php echo $row1['name']; ?></a></td>
         </tr></tbody>                           
    </div>
<?php}?>
 </table>
Теги:

1 ответ

0
Лучший ответ

Попробуй это:

<table  class="table table-striped">
<tr>
    <th>Shop Name</th>
    <th>Shop Name</th>
    <th>Shop Name</th>
</tr>

<?php

    $counter = 0;
    $emptyColumns = false;

    while ($row1=mysql_fetch_array($retrieval1, MYSQL_ASSOC))
    {
        // In the beginning and at every count of 3 write a <tr>
        if($counter == 0 || $counter % 3 == 0)
        {
            echo '<tr>';
        }

        // Write <td> every time
        echo '<td><a href="#">'.$row1['name'].'</a></td>';
        // Increment counter
        $counter++;

        // At every count of 3 write a </tr>
        if($counter % 3 == 0) 
        {
            echo '</tr>';
        }
    }

    // If table is not full (some columns left empty)
    if($counter % 3 != 0) $emptyColumns = true;

    // Fill empty columns
    while($counter % 3 != 0)
    {
        $counter++;
        echo '<td><a href="#">Empty</a></td>';
    }

    // If there were empty columns that means the </tr> tag was not written
    if($emptyColumns)
    {
        echo '</tr>';
    }

    echo '</table>';
?>
  • 0
    Большое спасибо. Работало

Ещё вопросы

Сообщество Overcoder
Наверх
Меню