2010-07-15 29 views
2

說我有一個查詢「從用戶選擇用戶名」。我想將這個查詢輸出到PHP頁面上,但是在每隔10行之後我想顯示自己的自定義文本。有任何想法嗎?並將從第11條記錄繼續。如何在每10行SQL查詢之後顯示文本?

+0

後,每10行......而不是10排對不起 – Jonathan 2010-07-15 17:20:42

回答

8
$count = 0; 
while (false !== ($row = mysql_fetch_array($result))) { 
    //output your row 

    ++$count; 
    if (($count % 10) == 0) { 
    //output your special row 
    } 
} 
+0

+1的人來說,你的代碼會更容易理解,但這對於模運算符來說是一個完美的例子 – STW 2010-07-15 17:55:43

1

最簡單的方法是將數據表拉回,循環遍歷各行,在記錄每行的同時記錄下所在的行。然後每進行一次迭代,寫出你的自定義信息。

+0

我的圍欄 - 最簡單的方法是使用模運算符,如Maerlyn的答案。儘管對於不太熟悉並且不熟悉'%' – STW 2010-07-15 17:57:13

0

如果您使用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. 

?> 
0
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 
相關問題