我在MySQL数据库中有超过100条记录。我没有遇到任何问题。但我想以不同的方式展示它们
-----------------------------------------------------
first data here | second data here
-----------------------------------------------------
third data here | fourth data here | fifth data here
-----------------------------------------------------
sixth data here | seventh data here | eight data here
------------------------------------------------------
ninth data here | tenth data here
------------------------------------------------------
11th data here | 12th data here | 13th data here
-----------------------------------------------------
14th data here | 15th data here | 16th data here
----------------------------------------------------
17th data here | 18th data here
所以你可以在第一行看到我只有两列。第二列我有3列,然后在第3列我有3列。然后在第4行再次有2列。所以基本上这就像一个循环。那么有人可以告诉我如何获得这种结构。 到目前为止,我已经获得了这样的代码,用于从数据库中获取数据
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$i = 0;
while($row = $result->fetch_assoc()) {
$i++;
if( $i%2 == 0) {
echo '<div class="1st-column">'. $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].'</div>';
}
if ($i%3 ==0) {
echo '<div class="2nd-column">'. $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].'</div>';
}
if ($i%3 ==5) {
echo '<div class="3rd-column">'. $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].'</div>';
}
}
} else {
echo "0 results";
}
$conn->close();
?>
所以有人可以告诉我如何让这个工作。任何帮助和建议都会非常明显。感谢
更新 我希望我的标记像这样
<div class="one-half">1st content</div>
<div class="one-half last">2nd content</div>
<div class="clear"></div>
<div class="one-third-content">3rd content</div>
<div class="one-third-content">4th content</div>
<div class="one-third-content last">5th content</div>
<div class="clear"></div>
<div class="one-third-content">6th content</div>
<div class="one-third-content">7th content</div>
<div class="one-third-content last">8th content</div>
<div class="clear"></div>
<div class="one-half">9th content</div>
<div class="one-half last">10th content</div>
<div class="clear"></div>
有人可以通过这种标记告诉我如何实现这一点。
答案 0 :(得分:3)
您的模式每8个结果重复一次。由于您希望每个分组的前2个偏移,因此您需要$i%8==1
和$i%8==2
,或缩短为in_array($i%8, array(1,2))
。
if ($result->num_rows > 0) {
$i = 1;
while($row = $result->fetch_assoc()) {
if(in_array($i%8, array(1,2))) {
$class = 'double'; // 2 wide
}
else {
$class = 'triple'; // 3 wide
}
echo '<div class="'.$class.'">'. $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].'</div>';
$i++;
}
}
使用以下CSS
<style>
.double {float:left;width:50%;text-align:center;}
.triple {float:left;width:33%;text-align:center;}
</style>
修改强>
为了获得编辑中的格式,您的代码看起来像这样 -
if ($result->num_rows > 0) {
$i = 1;
while($row = $result->fetch_assoc()) {
// set class to one-half if position 1 or 2, else one-third-content
$class = (in_array($i%8, array(1,2))) ? "one-half" : "one-third-content";
// add last class if last in row, ie. 2, 5, or 8 (8th position is 0)
$class .= (in_array($i%8, array(0,2,5))) ? " last" : "";
// echo div
echo '<div class="'.$class.'">'. $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].'</div>'."\n";
// echo div with clear class if last in row, ie. 2, 5, or 8 (8th position is 0)
echo (in_array($i%8, array(0,2,5))) ? '<div class="clear"></div>'."\n": "";
$i++;
}
}
答案 1 :(得分:0)
以下是关于肖恩回答的建议。
首先,我构建了分裂的概念,这是为了在适应之前简化事物,因此我做了一个for loop
来测试并且它有效:
$lineBr1 = 0;
$lineBr2 = 0;
$lineBr3 = 0;
for ($i = 1; $i <= 100; $i ++)
{
echo $i;
$lineBr1 ++;
$lineBr2 ++;
$lineBr3 ++;
if ($lineBr1 == 2) // 1st 2 names splitter
{
echo "<br />";
}
if ($lineBr2 == 5) // 2 + 3 = 5 (2nd 3 names splitter)
{
echo "<br />";
}
if ($lineBr3 == 8) // 2 + 3 + 3 = 8 (3rd 3 names splitter)
{
echo "<br />";
$lineBr1 = 0;
$lineBr2 = 0;
$lineBr3 = 0;
}
}
由于我们的for loop
有效,我们可以在您的代码中实现它,以下是您需要添加到代码中的内容:
$lineBr1 = 0;
$lineBr2 = 0;
$lineBr3 = 0;
if ($result->num_rows > 0)
{
while ($row = $result->fetch_assoc())
{
$lineBr1 ++;
$lineBr2 ++;
$lineBr3 ++;
echo "<div style='float: left; width: 250px;'>" . $row["id"] . " - Name: " . $row["firstname"] . " " . $row["lastname"] . "</div>";
if ($lineBr1 == 2)
echo "<br />";
if ($lineBr2 == 5)
echo "<br />";
if ($lineBr3 == 8)
{
echo "<br />";
$lineBr1 = 0;
$lineBr2 = 0;
$lineBr3 = 0;
}
}
}
关于设计和布局,我会根据您的喜好留下它。
我非常确定这项任务可以采取不同的方式,但这就是我所取得的成就。