Display records per second in an HTML table

[b]Hello for all,

Im a new in yii FrameWork and i need to your hepls if you want.

I want to display 5 records per second in an HTML table. These records are stored in the database.

Here is the code that displays all the records:[/b]


<? php $ Company = Company:: model () -> findAll ();

foreach ($ companies as $ company) {

?>

<tr style="text-align:center;"> <td style="font-size:small; font-weight: bolder; white-space: nowrap;"> <? php echo $ company-> name;?> </ td>

</ tr>

<? php}?>

[b]But I want to display only 5 record each seconde. example if I have 20 records should display 5 records per second.

Please help me and thank you …[/b]

What do you understand with "per second"?

The page loads and then, in each second the number of shown records grows with 5 ?

What’s the logic behind this, what do you try to achieve ?

If you want to show only the last 5 records, that’s simple:




$Company = Company::model()->findAll(array('limit'=>5,'order'=>'company_id DESC'));



If you want to display the latest 5 records in each second, you will need to do it in ajax like:

1)Have a separate controller that will serve you the latest 5 records from your table:




class SomeController extends Controller{


   public function actionIndex()

   {

      $company = Company::model()->findAll(array('limit'=>5,'order'=>'company_id DESC'));

      $this->renderPartial('someView',array('company'=>$company));

   }


}



2)The view file of the separate controller




<div>

   <php if(!empty($company)): foreach($company AS $c):?>

   <div><?php echo $c->name;?></div>

   <?php endforeach; endif;?>

</div>



Now, in the page where you will display these records, you will need a javascript function that will load the above records on every second, something like:




setTimeout(function(){

  $('#result').load('<?php echo Yii::app()->createUrl('the-/controller/we previously/built');?>', function() {

    console.log('Load was performed.');

  });

},1000);



This javascript code, will query your controller every second, and will get the latest 5 results and put them in the current page in the <div id="result"></div> container.

Thanks twisted1919 for your reply.

But i need to show 5 records by second like the site : http://fr.smsbox.net/

Look under of Ils nous fait confiance ther’s the names of companies and every secondes show 5 names. i want like that.

Can you help me ? i don’t know using Ajax

Thanks

Well what that site does is way too simple.

Here is how you do it:

  1. In your controller, query the database and get the latest, let’s say 50 companies



$company = Company::model()->findAll(array('limit'=>50,'order'=>'company_id DESC'));



Now $company has 50 companies (or less) in it so you pass this object to your view file:




$this->renderPartial('someView',array('company'=>$company));



Now, someView.php needs to loop through your $company object and also needs to contain some javascript to manage

the show / hide effect , so basically you will have:




<div class="companies-list">

    <?php if(!empty($company)): $i=0; ?>

       <div class="companiesBox">

        <?php foreach($company AS $c): ++$i;?>

        <div class="companyName"><?php echo $c->name;?></div>


        <?php if($i % 5 == 0):?>

        </div><div class="companiesBox" style="display:none">

        <?php endif;?>


        <?php endforeach;?>

      </div>

   <?php endif;?>

</div>



The above code will create a visible div and it will put 5 companies in it.

Then after the first 5 companies, will create a hidden div and will put other 5 companies in it and so on .

Now, you need some javascript to hide/show those divs.




$(function(){

   setInterval(function(){

   $('.companiesBox').each(function(){

     if($(this).is(':hidden')){

       $('.companiesBox').fadeOut('slow');

       var nextIdx=$(this).index()+1;

       if($('.companiesBox').eq(nextIdx).length > 0){

         $('.companiesBox').eq(nextIdx).fadeIn('slow');// go to next

       }else{

         $('.companiesBox').eq(0).fadeIn('slow');//go to first

       }

       return;

     }

   });

   },1000);

});



The code is not tested at all, it’s from the top of my mind, so it might not be working, but you will get the point .

Also, you will need to have jquery included in order for this to work.

You want a jQuery slideshow plugin.

Thank you twisted1919 :)

now i have just problem in view

i have in database just 3 companies so i changed


<?php if($i % 5 == 0):?>

by


<?php if($i % 2 == 0):?>

for i see the result but is display all companies in one time and hiden all in one time.


<div class="companies-list">

    <?php if(!empty($company)): $i=0; ?>

       <div class="companiesBox">

        <?php foreach($company AS $c): ++$i;?>

        <div class="companyName"><?php echo $c->name;?></div>


        <?php if($i % 5 == 0):?>

        </div><div class="companiesBox" style="display:none">

        <?php endif;?>


        <?php endforeach;?>

      </div>

   <?php endif;?>

</div>

may be the problem in javascript.

i wait your help if you can.

Thanks friend

This is the revised code, which is tested and works, small bug in the previous one :)




$(function(){

           setInterval(function(){

           $('.companiesBox').each(function(){

             if($(this).is(':hidden')==false){

               $('.companiesBox').hide(1);

               var nextIdx=$(this).index()+1;

               if($('.companiesBox').eq(nextIdx).length > 0){

                 $('.companiesBox').eq(nextIdx).show(1);// go to next

               }else{

                 $('.companiesBox').eq(0).show(1);//go to first

               }

               return false;

             }

           });

           },1000);

        });