在PHP中获取和显示数据时出现循环问题

时间:2017-02-03 11:09:07

标签: php mysql

我在数据库表中有24条记录。
我想在每个特定列表中显示6条记录。所以,我基本上需要4个记录列表。

我尝试过这种方式,请参阅下面的代码段以获取更多详细信息。

from django.shortcuts import render
def form(request):
    return render('prueba2.html')

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您只需要执行一个查询,并将结果保存在数组中:

<!-- To Buy List -->
    <div class="col-md-6" ng-controller='ToBuyController as buyItem'>
     <h2>To Buy:</h2>
     <ul>
       <li ng-repeat="item in buyItem.items">
         Buy {{item.quantity }} of {{ item.name }}
         <button ng-click="buyItem.removeItem($index)" class="btn btn-default"><span class="glyphicon glyphicon-ok">Bought</button>
       </li>
     </ul>
     <div class="emptyMessage">Everything is bought!</div>

    </div>

    <!-- Already Bought List -->
    <div class="col-md-6" ng-controller='AlreadyBoughtController as boughtItem'>
     <h2>Already Bought:</h2>
     <ul>
       <li ng-repeat="item in boughtItem.items">
         Bought {{ item.quantity }} of {{ item.name }}
       </li>
     </ul>
     <div class="emptyMessage">Nothing bought yet.</div>
    </div>

然后使用循环迭代结果(这是你在案例中缺少的循环):

$homeBrands =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.HOME_LOGO,b.STATUS,m.ID,m.TITLE,m.LOGO FROM cms as b
                      INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' ORDER BY b.POSITION LIMIT 6");
$homeResult =mysql_fetch_array($homeBrands);

这就是您需要的所有代码,无论您想要显示多少结果。希望你明白这个主意。代码未经过测试