Tutorial :How can i display text after every 10 rows of an SQL query?


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.


$count = 0;  while (false !== ($row = mysql_fetch_array($result))) {    //output your row      ++$count;    if (($count % 10) == 0) {      //output your special row    }  }  


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.


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.       ?>  


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  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »