我试图从数据库加载图像,我可以获得完整的字符串,并且没有显示在图像上,当我使用完整的数组时,我得到一个数组消息。
这是模型:
public function get_webstore($webstore_id = '') {
/* get video */
if (empty($webstore_id)) {
$webstores = $this->get('webstore');
} else {
$webstores = $this->get('webstore', $webstore_id);
}
$webstore_array = array();
foreach ($webstores as $webstore) {
$singleWebstorePair = array();
if (!empty($webstore['images'])) {
$allWebstores = unserialize($webstore['images']);
foreach ($allWebstores as $allWestores) {
$singleWebstorePair[] = str_replace("./", "", base_url($allWebstores));
}
}
$webstore_array[] = array(
'id' => $webstore['id'],
'title' => $webstore['title'],
'content' => $webstore['content'],
'images' => $singleWebstorePair,
'created' => $webstore['created']
);
}
return $webstore_array;
}
这是前端代码
<div class="col-md-12" style="height: 225px;">
<div class="col-md-4 text-center thumbnail123">
<img class="thumbnail" width="225px" src="<?php echo $webstore['images']?>.jpg"/>
<h3><?php echo $webstore['title']; ?></h3>
<p>Date Added: <?php echo date('d-m-y', strtotime($webstore['created'])) ?></p>
<hr>
<hr>
</div>
如果需要,这是控制器:
public function __construct() {
parent::__construct();
$this->load->model("post");
$this->load->database();
$this->load->helper("url");
}
public function index() {
$this->data['posts'] = $this->post->get_webstore($limit=null, $offset=null); // calling Post model method getPosts()
$this->data['page_title'] = 'Store';
$this->layout("pages/webstore", $this->data);
和错误:
Severity: Notice
Message: Array to string conversion
Filename: pages/webstore.php
Line Number: 45
谢谢你们
答案 0 :(得分:3)
填充数组时,设置'images' => $singleWebstorePair,˙
这是一个数组。
在您的模板中,您的回应是:
<img class="thumbnail" width="225px" src="<?php echo $webstore['images']?>.jpg"/>
但是这里$webstore['images']
是一个数组。因此,如果$webstore['images']
包含多个图像源,则循环遍历它:
<?php foreach($webstore['images'] as $image) : ?>
<img class="thumbnail" width="225px" src="<?php echo $image ?>.jpg"/>
<?php endforeach; ?>