Problem With $Data In Gridview

hi there

i dont have a problem with this :

‘value’=>’$data->item_id’

and no problem with this too:

‘value’=>’$data->item->category_id’

and this is my custom class:

class Helper {

public static function getString($inputList, $spaceCount = 3){

  return implode(str_pad(' ', $spaceCount), $inputList);

}

}

when i use ‘value’=>’$data->item_id’ like this again no problem:

‘value’=> "Helper::getString(array(

\"<a href=’".Yii::app()->createUrl(’/item/view’).’&id=$data->item_id’."’>\".

  &#092;&quot;&lt;img src='&quot;.Yii::app()-&gt;baseUrl.&quot;/image/categories/&quot;.'&#036;data-&gt;item_id'. &quot;'&gt;&#092;&quot;.

\"</a>\" ));",

but when use ‘value’=>’$data->item->category_id’ a problem occur :

‘value’=> "Helper::getString(array(

        &#092;&quot;&lt;a href='&quot;.Yii::app()-&gt;createUrl('/item/view').'&amp;id=&#036;data-&gt;item_id'.&quot;'&gt;&#092;&quot;.


           &#092;&quot;&lt;img src='&quot;.Yii::app()-&gt;baseUrl.&quot;/image/categories/&quot;.'&#036;data-&gt;item-&gt;category_id'. &quot;'&gt;&#092;&quot;.


        &#092;&quot;&lt;/a&gt;&#092;&quot;         ));&quot;,

and this is error msg:

Object of class Item could not be converted to string

thanks for thought on this

I highly recommend that you use anonymous functions rather than trying to manage so much quote escaping. Also, the CHtml::link() method can help you with your links.




    'columns'=>array(

        array(

            'name'=>'blah',

            'value'=>function($data){

                $imageTag = CHtml::image(Yii::app()->baseUrl . '/images/categories/'

                        . $data->item->category_id);

                $linkTag = CHtml::link($imageTag, array('/item/view', 'id'=>$data->item_id));

                return Helper::getString(array($linkTag));

            },

        ),

    ),



Thanks

But Because of my php version i cant use anonymous functions

Well, just create a function in your view file to serve the same purpose.

Somewhere near the top of the view file:




function getBlahColumn($data)

{

    $imageTag = CHtml::image(Yii::app()->baseUrl . '/images/categories/' . $data->item->category_id);

    $linkTag = CHtml::link($imageTag, array('/item/view', 'id'=>$data->item_id));

    return Helper::getString(array($linkTag));

}



Then your column config becomes:




    'value'=>'getBlahColumn($data)',



Feel free to wrap the function definition in a separate class or model as appropriate.

With this approach, you wrap far less of your code in a string, making it much easier to understand.

thank you

but actually i dont want to have a func definition in every view file, if i have these definition so i only process input array in each view file nd dont need that helper class

how can i use my helper class with out anything between …

thanks for you help

I don’t understand. How is a function definition worse than a string expression which is evaluated? Especially one which has become so complicated that you’re unable to debug it.

thanks for ur replay

my only problem is that i want to concat $data->item->category_id with a string

Instead of using anonymus functions to avoid much quote escaping you can use callable methods of your model, the current controller or a static method of a helper class.

See this wiki