发布的新闻或介绍里有图片,图片存的是Url地址,通过yii的cgridview,默认是数据库里存的啥就显示啥,如何把url地址转为图片?直接上代码

<?php
	$data = $model->search();//全部数据 P.S 分页设置在News.php里 aptionSearch方法实现
	//分页显示数据,如下所写,分页有效,筛选无效
	/*$data = new CActiveDataProvider('News', array(
    	'pagination' => array(
        	'pageSize' => 2,
        ),
    ));*/
	$filter = Category::model()->findAll();
	$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'news-grid',
	'dataProvider' => $data,
	'filter'=>$model,
	'columns'=>array(
		// 'n_image',
		array(
            'name'=>'n_image',
            'type'=>'html',
            'value'=>'(!empty($data->n_image))?CHtml::image("/".$data->n_image,"",array("style"=>"width:100px;height:100px;")):"暂无图片"',
        ),
		// 'n_content',
		array(
			'class'=>'CButtonColumn',
			//自定义删除提示
			'deleteConfirmation'=>"js:'ID为 '+$(this).parent().parent().children(':first-child').text()+' 的记录将被删除,确定删除?'",
		),
	),
)); ?>
'value'=>'(!empty($data->n_image))?CHtml::image("/".$data->n_image,"",array("style"=>"width:100px;height:100px;")):"暂无图片"', 
这里是通过CHtml的方法image来把url转成图片,写的时候要注意'' 这里的内容,必须是能够执行的php代码,在内部yii是通过 eval() 这个函数把内容转换为了php代码来执行的
记录一下,方便查看,也方便大家解决问题

相关文章:

  • 2021-12-02
  • 2021-04-24
  • 2021-09-16
  • 2021-09-09
  • 2021-06-22
  • 2021-12-03
  • 2022-12-23
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-03-10
  • 2021-07-15
  • 2021-12-30
  • 2021-11-09
相关资源
相似解决方案