【发布时间】:2011-03-16 12:56:22
【问题描述】:
假设我有一个查询“从用户中选择用户名”。我想将此查询输出到 PHP 页面,但在每 10 行之后我想显示我自己的自定义文本。有任何想法吗?并且会从第 11 条记录继续。
【问题讨论】:
-
在每 10 行之后...不是第 10 行抱歉
假设我有一个查询“从用户中选择用户名”。我想将此查询输出到 PHP 页面,但在每 10 行之后我想显示我自己的自定义文本。有任何想法吗?并且会从第 11 条记录继续。
【问题讨论】:
最简单的方法是将数据表拉回,循环遍历行,写出每一行,同时跟踪您所在的行。然后每十次迭代,写出您的自定义消息。
【讨论】:
%的人来说,你的会更容易理解
如果您使用 PDO 运行 mysql 查询,您可以在 PHP 中创建一个变量,然后通过该变量限制您的查询。
这是一个不完整的示例,但您可能会明白。
<?php
$first = 0;
$second = 9;
$stmt = $db->prepare('select username from users limit :first, :second');
$stmt->bindParam(':first', $first);
$stmt->bindParam(':second', $second);
$stmt->execute();
#loop through your results here and then have a custom message,
#then change your variable values and execute the statement again.
#Repeat this until there are no more rows.
?>
【讨论】:
create table #t
(
UserName varchar(100)
)
declare @count int
declare @rows int
set @rows = 0
select @count = count(*) from users
while (@count > 0 )
begin
insert into #t
select top 10 username from users where userid > @rows
insert into #t select '******'
set @count = @count - 1
set @rows = @rows + 10
end
select * from #t
drop table #t
【讨论】:
$count = 0;
while (false !== ($row = mysql_fetch_array($result))) {
//output your row
++$count;
if (($count % 10) == 0) {
//output your special row
}
}
【讨论】: