duanlei20082008 2015-10-25 18:52
浏览 37
已采纳

从数据库php codeigniter加载图像

Im trying to load images from the database, i can get the full string and no display on the image and when i use the full array I get a array message.

here is the model :

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;
    }

and here is the front end code

<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>

here is the controller if needed:

   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);

and the error:

Severity: Notice

Message: Array to string conversion

Filename: pages/webstore.php

Line Number: 45

thanks guys

  • 写回答

1条回答 默认 最新

  • dsbfbz75185 2015-10-25 19:52
    关注

    When you populate your array, you set 'images' => $singleWebstorePair,˙ which is an array.

    In your template, your are echoing this:

    <img class="thumbnail" width="225px" src="<?php echo $webstore['images']?>.jpg"/>

    But here $webstore['images'] is an array. So if $webstore['images'] holds multiple image source, then loop through it:

    <?php foreach($webstore['images'] as $image) : ?>
       <img class="thumbnail" width="225px" src="<?php echo $image ?>.jpg"/>
    <?php endforeach; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 C++代码报错问题,c++20协程
    • ¥15 c++图Djikstra算法求最短路径
    • ¥15 Linux操作系统中的,管道通信问题
    • ¥15 ansible tower 卡住
    • ¥15 等间距平面螺旋天线方程式
    • ¥15 通过链接访问,显示514或不是私密连接
    • ¥100 系统自动弹窗,键盘一接上就会
    • ¥50 股票交易系统设计(sql语言)
    • ¥15 调制识别中这几个数据集的文献分别是什么?
    • ¥15 使用c语言对日志文件处理