Merged Columns In Cgridview

Hi all,

I am displaying a table of data using the CGridView widget and I have got a column that is combing the data from several database fields and displaying them in the one column. This is used to display a full address where in the database they are entered as line 1, line 2, town, postcode etc.

I have managed to get the combined fields working and I am separating each field with a comma but I have found that many of the fields are empty and so on some records I just get the commas. For example:

Record 1 - Line1, Line2, Town, Postcode

Record 2 - Line1, Town, Postcode

Record 3 - , Postcode

My code for this is as follows:


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'properties-grid',

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		'pr_id',

		array(

			'name'=>'Address',

			'value'=>'$data->pr_location_line1 . ", " . $data->pr_location_line2 . ", " . $data->pr_location_line3 . ", " . $data->pr_location_line4 . ", " . $data->pr_location_line5 . ", " . $data->pr_location_post_town . ", " . $data->pr_location_postcode',

        ),

		'pr_status',

		'pr_updated',

		'pr_administrator',

		'pr_updated',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>

Ideally what I would like is some way of checking that if the field is empty (or null) then the comma is not displayed. I have tried a few if statements but they don’t work.

Does anyone know how this can be achieved?

I am looking to display the records as demonstrated above as:

Record 1 - Line1, Line2, Town, Postcode

Record 2 - Line1, Town, Postcode

Record 3 - Postcode

Many thanks

You might want to encapsulate this into a method in your model:




    public function getAddressCommaDelim()

    {

        $addressArray = array();


        if ($this->pr_location_line1)

            $addressArray[] = $this->pr_location_line1;


        if ($this->pr_location_line2)

            $addressArray[] = $this->pr_location_line2;


        if ($this->pr_location_line3)

            $addressArray[] = $this->pr_location_line3;


        if ($this->pr_location_line4)

            $addressArray[] = $this->pr_location_line4;


        if ($this->pr_location_line5)

            $addressArray[] = $this->pr_location_line5;


        if ($this->pr_location_post_town)

            $addressArray[] = $this->pr_location_post_town;


        if ($this->pr_location_postcode)

            $addressArray[] = $this->pr_location_postcode;


        return implode(',', $addressArray);

    }



Then, in your grid view just do this:




    array(

        'header'=>'Address',

        'value'=>'$data->addressCommaDelim',

    ),



Alternatively, you could create a method to return the completed address lines as an array and do the implosion in the grid code. The method header might be getCompletedAddressLines().

Thank you Keith.

I have just tried your first suggestion and it has worked perfectly. I should have thought about putting something in the model and not trying to do it in the view. I guess my non-MVC bad habits are going to take a while to shed. Let this be a lesson to me!

Many thanks