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?
+1
A:
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.
George
2010-07-15 17:07:22
I'm on the fence--the simplest way is to use the modulo operator, as in Maerlyn's answer. Although yours would be easier to understand for someone who doesn't code much and isn't familiar with `%`
STW
2010-07-15 17:57:13
A:
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.
?>
Catfish
2010-07-15 17:11:12
A:
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
Muhammad Kashif Nadeem
2010-07-15 17:14:49