我有一个带有bootstrap 3列的HTML结构,所有div
只重复3次,但所有div
都有一个ul
,每个ul有7 li
。
我想根据以下HTML结构显示动态列表:
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><a href="#"><i class="fa fa-angle-right"></i> Mechanical Engineering Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> BPO Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Networking Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Java Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Online Marketing Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Animation Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Design Engineer Jobs</a></li>
</ul>
</div>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><a href="#"><i class="fa fa-angle-right"></i> Analytics Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> UI/UX Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> NLP Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Marketing Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Banking Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> MBA Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Teaching Jobs</a></li>
</ul>
</div>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<li><a href="#"><i class="fa fa-angle-right"></i> Accounting Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Retail Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Travel Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Merchandiser Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Architecture Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Banking Insurance Jobs</a></li>
<li><a href="#"><i class="fa fa-angle-right"></i> Music Jobs</a></li>
</ul>
</div>
为此,我看到了以下答案并尝试使用以下代码。
我尝试使用以下代码,但它不能正常工作。
<?php
$sqlEng = mysql_query('select * from jobs_category');
$count = 1;
while($resEng = mysql_fetch_array($sqlEng)){
if ($count%3 == 1)
{
?>
<div class="col-md-4 padding-left">
<?php } ?>
<ul class="unstyled">
<li><a href="#"><i class="fa fa-angle-right"></i> <?php echo $resEng['name'];?></a></li>
</ul>
<?php if ($count%3 == 0)
{
?>
</div>
<?php } $count++; } ?>
答案 0 :(得分:0)
假设表中只有21条记录。
试试这个。
<?php
//$sqlEng = mysql_query('select * from jobs_category');
$servername = "localhost:3306";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, "stack1");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sqlEng = $conn->query('select * from jobs_category');
$count = 1;
while($resEng = $sqlEng->fetch_assoc()){
if ($count%7 == 1)
{
?>
<div class="col-md-4 padding-left">
<ul class="unstyled">
<?php } ?>
<li><a href="#"><i class="fa fa-angle-right"></i> <?php echo $resEng['name'];?></a></li>
<?php if ($count%7 == 0)
{
?>
</ul>
</div>
<?php } $count++;
} ?>
在我当地测试。
答案 1 :(得分:0)
如果我的问题正确无误,您正试图将列表项<li> A Job </li>
分散到三个列表<div> <ul> List Items </ul> </div>
<?php
$sqlEng = mysql_query('select * from jobs_category');
$count = 1;
$group_one = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
$group_two = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
$group_three = "
<div class='col-md-4 padding-left'>
<ul class='unstyled'>
";
while($resEng = mysql_fetch_array($sqlEng)){
if($count > 3) {
$count = 1;
}
switch ($count) {
case 1:
$group_one .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
break;
case 2:
$group_two .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
break;
case 3:
$group_three .= "
<li><a href='#'><i class='fa fa-angle-right'></i> " .$resEng['name'] . "</a></li>";
}
$count++;
}
$group_one .= "
</ul>
</div>";
$group_two .= "
</ul>
</div>";
$group_three .= "
</ul>
</div>";
echo $group_one . $group_two . $group_three;
?>
您想考虑改进使用MySQl mysqli
https://www.w3schools.com/php/php_ref_mysqli.asp