Say i have a query "select username from users". I want to output this query onto a PHP page but after every 10th row i want to display my own custom text. Any ideas? and it will continue from the 11th record.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
$count = 0;
while (false !== ($row = mysql_fetch_array($result))) {
//output your row
++$count;
if (($count % 10) == 0) {
//output your special row
}
}
回答2:
The simplest way would be to pull the data table back, loop through the rows, writing each one out while keeping track of the row you are on. Then every tenth iteration, write out your custom message.
回答3:
If you are using PDO to run your mysql query, you can create a variable in PHP and then limit your query by that variable.
here's an incomplete example, but you may get the idea.
<?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.
?>
回答4:
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