CSV error when implode

Hello. Please help, I’m newbie in Yii. I want to generate and export CSV-file from checked rows in CGridView. When I use static SQL-query it’s work normally, but when I use implode function in where clause - controller return an error.

My button that called controller action:




<?php

$this->widget('bootstrap.widgets.TbButtonGroup', array(

    'type' => 'primary',

    'size'=>'mini',

    'buttons' => array(

        array(

            'label' => 'Export',

            'type' => 'success',

            'buttonType'=>'ajaxLink',

            'encodeLabel'=>true,

            'icon'=> 'th white',

            'url'=>Yii::app()->createUrl('/propertyPurchaseSale/ExportChecked'),

            'ajaxOptions'=>array(

            "type" => "post",

            "data" => "js:{ids:$.fn.yiiGridView.getSelection('property-purchase-sale-grid')}",

            "update" => '#', 'success'=>"js:function(data) {window.location.assign('/propertyPurchaseSale/ExportChecked');}"),                                                

                                  array( //htmlOptions

                                        )                                    

                                    ),

                    array(

                        ...

                    ),

                    ),

                )); 

?>



My controller action:




<?php

public function actionExportchecked() {

            header('Content-type: text/csv');

            header('Content-type: multipart/form-data');

            header('Content-Disposition: attachment; filename="Export_(' . date('H-i_d.m.Y') .').csv"');

            header('Content-Transfer-Encoding: binary'); 

            header('Expires: 0');

            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

            header('Content-Description: File Transfer');

			$fp = fopen('php://output', 'w');


    	    if(Yii::app()->request->isAjaxRequest)

            {

            	if(isset($_POST['ids']))

            	{	

                    $idx = $_POST['ids'];

                    $count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM property')->queryScalar();

                    // $sql='SELECT * FROM property WHERE id  IN (981, 982, 985)';  --> when I use static values - all work

                    $sql="SELECT * FROM property WHERE id IN('".implode("', '",$idx)."')";  // --> when I use join or implode function - data exist in firebug, but page return error 500


                    $dataProvider=new CSqlDataProvider($sql, array(

                        'totalItemCount'=>$count,

                        'sort'=>array(

                            'attributes'=>array(

                                 'fullname', 'address', 'phone', 'db_number', 'created_date'

                            ),

                        ),

                        'pagination'=>false,

                    ));

                			fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));

                			if ($fp)

                			{                        

                            echo    PropertyPurchaseSale::model()->getAttributeLabel("id").";".

                            		PropertyPurchaseSale::model()->getAttributeLabel("fullname").";".

                                    PropertyPurchaseSale::model()->getAttributeLabel("address").";".

                                    PropertyPurchaseSale::model()->getAttributeLabel("phone").";".

                                    PropertyPurchaseSale::model()->getAttributeLabel("db_number").";".

                                    PropertyPurchaseSale::model()->getAttributeLabel("created_date").

                                    " \r\n";                        

                            foreach ($dataProvider->getData() as $data) {

                            	echo $data['id'] . '; ' . $data['fullname'] . '; ' . $data['address'] . '; ' . $data['phone'] . '; ' . $data['db_number'] . '; ' . $data['created_date'] . '; ' . "\r\n";

                            }

                }

                        exit;

                    }

                }

}    

?>



Please, help, what I doing incorrectly?

You have to check the values of $_POST[‘ids’] and the generated sql.

I recommend to use/install a PHP-Debugger.

Otherwise you have to struggle with echo, die($sql), Yii::log … to check these values.

on static values you just use numbers… but on implode() you use quotes around those numbers… could that be the reason?

Thanks for Your answer. But when i use implode - FireBug return needed values and page displayed error 500.

When I use print_r($sql) firebug also returned SELECT * FROM property WHERE id IN (1251, 1250, 1249, 1248)