display row number

I have this:

$top=new CActiveDataProvider('ritmo',array(


        'select'=>'idritmo,count(*) top,artist,title',

        'join'=>'inner join encomenda_linha on (t.idritmo=encomenda_linha.style)',


		'order'=>'top DESC'  



How to include the row number on select??

what do you mean with row number?

in oracle you have ROWNUM function and mysql?

I use this to create a top10 rank

do you need that number in your grid?


Yes i do.No mysql function to return this?

It’s probably best to do this in your application code because there’s no built in MySQL implementation. You could use something like this though:


Ok it’s cool but how to do in the CActiveDataProvider?

How is the row number going to be used?

For most uses, you should really just calculate it as you build the output. The row number is part of the view because it’s not tied to a specific row of the database; it’s purely for display purposes.

i have this output.Instead of foreach can i do a normal for to have a index??

<div class="top10-lista-container">


                <?php foreach ($top->data as $ritmotop) { ?>


                    <div class="posicao"></div>

                    <div class="descricao">

                        <p class="artist"><?php echo $ritmotop->artist; ?></p>



               <?php  } ?>



I’d do it like this:


                <? $rowNum = 1; ?>

                <? foreach ($top->data as $ritmotop): ?>


                    <div class="posicao"></div>

                    <div class="descricao">

                        <p class="artist"><?= $ritmotop->artist; ?></p>


                    <? $rowNum++; ?>


               <? endforeach; ?>


Of course, only use the short tags if they’re available on your target platforms.

You could try like this example:

SELECT  t.id, 



        @curRow := @curRow + 1 AS row_number

FROM    table t

JOIN    (SELECT @curRow := 0) r;

I saw this on stackoverflow :)

thank you