我正在尝试弄清楚如何每查询8个MySQL行就创建一个新的DIV。我正在将jPagination集成到我的站点中,因此我需要每8行从数据库中接收一个新的DIV容器。有什么想法吗?
发布于 2012-12-08 21:11:40
你需要这个:%
不确定你的代码到底是如何运行的,但在每一行的循环中,你会生成一个count++,然后像这样的东西,这将是C:
if(!count%8) {
print DIV eccc
}这样你就明白这个%是做什么的:它给你一个除法的余数。例如,如果你的行数是20,此时count等于20,20%8等于4。这是因为如果你除以20/8,你会得到2。**一些东西,然后乘2*8就会得到16。取20-16 = 4。所以20%8是4。只有当count++中的数字完全可以被数字8整除时,你才会得到0。所以您的IF语句说:如果没有剩余数除以8,则执行以下操作
格言
发布于 2012-12-08 21:10:29
i = 1
while( gettingRows )
{
doWhateverYouDoHere()
if ( i%8 === 0 )
print "div"
++i
// or put increment right into the if statement like ( if i++ % 8 === 0 )
}发布于 2012-12-08 21:18:28
<?php
// previous code.....
$counter = 1;
echo '<div class="outercssclass">';
echo '<div class="innercssclass">';
// fetch mysql query data into $results....
// you can do validations with mysql_num_rows to check the number of rows the query returned
foreach($results as $result) {
$counter++;
if($counter % 8 == 0) {
echo '</div><div class="innercssclass">';
}
// other logic...
// rest of the code
}
echo '</div>'; // for closing the inner div
echo '</div>'; // for closing the outer wrapper div
// rest of the program logic...https://stackoverflow.com/questions/13777738
复制相似问题