How update ajax response in loop?

Hello,

How to update a div with the result of sql update? renderPartial is showing all results together.

Controller




    public function actionfetchLinks(){

        $Links=Yii::app()->db->createCommand()->select( '*')->from('rssqueue')->queryAll();    

        $i=0;

        foreach($Links as $link){

            $i++;

            echo CHtml::script(" $('#rssResult').append(\"<p><span class='label label-info a$link[id]'> $i </span> $link[title] <span class=$link[id]></span></p>\")");


            $linkStatus = Rss::insertLinks($link);

            $this->renderPartial("//rssqueue/_ajax", array(

                                                        "title"=> $link['title'],

                                                        "lastID"=>$lastID,

                                                        "id"=>$link['id'],

                                                        "seq"=>$i,

                                                        "stat"=>$linkStatus['stat']), 

                                                      false, true);


            if($i == 5) exit; //stop at 5

        }        

    }



model




    public static function insertLinks($link){

        if(!Article::isArticleUnique($link[title]) or 1 == 1){  //REMOVE 1==1 after testing

                Rss::RemoveTags($link[Rss_id]);

                Rss::UrlArticle($link[RssLink], $link[Rss_id]);

                $data = array(

                    'cid'=> self::getRss($link[Rss_id])->Category,

                    'title'=>$link[title],

                    'body'=> self::$ArticleText,

                    'uid'=>uniqid(time(), true)."-".time(),

                    'status'=>1,

                    'main_image'=>self::$articleImage,

                    'created'=>$link[Date],

                    'slug'=>UrlGenerator::doSlug($link[title]),

                    'RssId'=>$link[Rss_id],

                    'SourceLink'=>$link[RssLink],

                );

                if(Yii::app()->db->createCommand()->insert('article', $data)){ // if insert is successful

                    $lastID = Yii::app()->db->lastInsertID;    

                    $stat = true;

                }else{

                    $lastID = '';

                    $stat = false;

                }



template




echo CHtml::ajaxLink(

    'Update',

    array('fetchLinks'), 

    array('update' => '#rssResult',

          'beforeSend' => 'function() { 

            $("#loading").append("Starting ...");

            $("#rssqueue-grid").fadeOut("5000")

            $(".search-button").fadeOut("3000")

            $("#categories").fadeOut("2000")

            $("#startAjax").fadeOut("500")


          }',

          'complete' => 'function() { $("#loading").hide();}',

          //'success' => ''

         ), // jQuery selector

    array("id"=>"startAjax", 'class'=>'btn btn-success')

);




_ajax




if($stat == true){

    echo CHtml::script(" $('.a$id').delay(1000).removeClass('label-info').addClass('label-success')");

    echo CHtml::script(" $('.$id').delay(2000).append(' [ID: $lastID] ')");

    echo "($stat)";

}

elseif($stat == false){

    echo CHtml::script(" $('.a$id').delay(1000).removeClass('label-info').addClass('label-inverse').css('color','red')");

    echo "[$stat]";

}




[size="5"]Thanks in advanced[/size]

Disable layout in this action like below:


$this->layout = false;

BTW: Instead of increment $i inside foreach loop, you could use more elegant approach:


$Links = array_slice($Links, 0, 5);

Hello,

The problem remain the same thing, so I changed to send an ajax request for each checked box:

admin.php




echo CHtml::script("

            function doThis(){

                var i = 0

                checkedCount = $('input:checkbox:checked').length

                $('input:checkbox:checked').each(function( index ) {

                    if($(this).val()){

                        //console.log('process: '+ $(this).val())

                        ajaxCall($(this).val())

                    }else{

                        //console.log('skip')

                    }

                })

                $('#loading').append('Starting ...')

                $('#rssqueue-grid').fadeOut('5000')

                $('.search-button').fadeOut('3000')

                $('#categories').fadeOut('2000')

                $('#startAjax').fadeOut('500')

                

            }

                

            function ajaxCall(id){

                

            $.ajax({

                url : 'pushAjax',

                type: 'POST',

                data: {id},

                dataType: 'html',

                beforeSend: function(){},

                success: function(response,status,data){

                        $('#rssResult').append(response)

                        var parentWidth = $('.bar').parent().width();

                        var barWidth = $('.bar').width()

                        var pct = parentWidth / $( 'input:checkbox:checked' ).length 

                        if(barWidth <= 0)  $('.bar').css('width', pct)

                        

                        barProcess = pct+barWidth

                        

                        $('.bar').css('width', barProcess)

                        console.log('barWidth: '+barWidth)    

                        console.log('pct: '+pct)  

                        console.log('total: '+barProcess)  

                }

            })

        }




");


echo CHtml::link("test", '#',array("onClick"=>"doThis();"));



Now, this works fine, but what I’m facing the progress bar … i need to calculate the completion and showing on the bar … any idea ?

Thanks