2017-12-25 2 views
0

3 개의 열을 포함하는 테이블이 있습니다. Sql1 쿼리는 데이터베이스에서 상점 이름을 얻고 있고 모든 상점을 1 열에 표시하고 있지만 3 열의 각 행에 3 개의 레코드를 표시하려고합니다. 즉, 1 행의 3 번째 레코드, 2 번째의 다음 3 레코드 등입니다. 나는 그것을 상쇄 사용하려고하면3 열로 테이블의 각 행에 3 개의 레코드를 표시하고 싶습니다.

<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'; 
$retreval=mysql_query($sql,$con); 
if(!$retreval){ 
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($retreval, MYSQL_ASSOC)) { 
} 
echo "</div>"; 

$sql1="Select name from info limit 3 offset 1"; 
$retreval1=mysql_query($sql1,$con); 
if(!$retreval1){ 
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($retreval1, MYSQL_ASSOC)) { 
?> 
<tbody><tr> 
<td><a href="#"><?php echo $row1['name']; ?></a></td> 
     </tr></tbody>       
    </div> 
<?php}?> 
</table> 
+2

** ** 경고 :이 경우 단지 PHP를 배우기 위해서는 쓸모없는 [mysql_query'] (http://php.net/manual/en/function.mysql-query.php) 인터페이스를 배우지 마십시오. 그것은 끔찍 하 고 PHP 7에서 제거되었습니다. [PDO 배우는 것은 어렵지 않다] (http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo- for-database-access /)와 [PHP The Right Way] (http://www.phptherightway.com/)와 같은 가이드가 모범 사례를 설명하는 데 도움이됩니다. ** 확실하게 ** 귀하의 사용자 매개 변수는 [제대로 이스케이프] (http://bobby-tables.com/php) 또는 심각한 [SQL 주입 버그]로 끝날 것입니다 (http://bobby-tables.com/). – tadman

답변

0

이 시도가 사전에 help.Thanks을 help.Please하지 않도록도 수를 해결 있습니다

<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($retreval1, 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

고마워. –

관련 문제