![](/img/trans.png)
[英]easiest way to fetch specific column data from mysql in php (special case)
[英]Easiest way to Format MySQL Data to Next Column in PHP
我當前正在將數據輸出到一個范圍...這是它的外觀:
現在,當我刪除跨度並放置一個div時,將得到以下輸出:
這是理想的,但是我想在頁面上設置一個高度並使數據顯示在最少3列中。 我該怎么做? 我在網上到處搜索,但似乎找不到任何能解決問題的方法。
我確實讀過一些使用javascript的格式,但是即使這個選項我仍然一無所知。
我想要的輸出看起來像這樣:
如果知道一列中要包含多少個項目,則可以將其分離為各個div,然后將這些div浮動到左側以使它們彼此相鄰。
<div style='float:left'>
//Items go here
</div>
<div style='float:left'>
//Items go here
</div>
等等。如果您弄清楚查詢返回了多少項目,請說使用mysql_num_rows()並除以3,就可以知道每列中要放入多少項目。 另外,請確保之后清除浮標,如下所示:
<div style="clear:both"></div>
有時這是必要的,因為如果不將其放置在那里,將會出現隨機問題。
您所描述的內容只能通過樣式解決。 您必須在列中顯示幾個div。 最簡單的方法是將它們浮動到左側,並將寬度設置為父級的1/3。 如果要4列,請將with設置為父級的1/4,依此類推。
<div class='sqlResult' style="float:left;width:33%;">
<a href='#'>$key</a>
</div>
另外,正如提到的其他答案一樣,請勿使用重復的ID。 始終使用類。 如果您需要分別定位每個div,請為其指定一個唯一的ID,例如“ category_1”,“ category_2”等。
這應該工作
<table><tr>
<?php $count=0; $total=mysqli_stmt_num_rows($sql)-1; $idxcount=0; $limit=10; while($row = mysqli_fetch_array($sql)): $key = $row['Keyword_Name']; ?>
<?php if($count == 0){ echo '<td>';} ?>
<span>
<a href="<?php echo $key; ?>" id="category"><?php echo $key; ?></a>
</span>
<?php if($total == $idxcount): ?>
</td>
<?php elseif($count == $limit): ?>
</td>
<?php $count=0; else: $count++; ?>
<?php endif; $idxcount++; ?>
<?php endwhile; ?>
</tr></table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.