Codeigniter:显示来自数据库的多个图像

时间:2013-11-27 13:40:30

标签: codeigniter

我正在经营一个房地产网站。我正在尝试在数据库中列出其各自页面上特定列表的所有图像。我有以下代码,但它只是重复相同的默认图像,而不是显示该列表的所有图像。

型号:

    public function getDetails($listing_id)
{
    $this->db->select(
        'listings.listing_id AS id,
        listings.account_id AS account_id,
        listings.nbrhood_id AS nbrhood_id,
        listings.price AS price,
        listings.convertible AS convertible,
        listings.available AS available,
        images.file_name AS file_name,
        listings.sqft AS sqft,
        listings.db_tag AS db_tag,
        listings.web_id AS web_id,
        listings.description AS description,


        bedrooms.beds_num AS beds_val,
        bedrooms.beds_name AS beds_text,

        bathrooms.baths_num AS baths_val,
        bathrooms.baths_name AS baths_text,

        nbrhoods.nbrhood_name AS nbrhood,
        nbrhoods.image AS nbrhood_image,

        types_listings.type_name AS type,

        pets.pet_name AS pets'
        );


    $this->db->join('images', 'images.image_id = listings.image_id', 'left');
    $this->db->join('bedrooms', 'bedrooms.beds_id = listings.beds_id');
    $this->db->join('bathrooms', 'bathrooms.baths_id = listings.baths_id');
    $this->db->join('nbrhoods', 'nbrhoods.nbrhood_id = listings.nbrhood_id');
    $this->db->join('types_listings', 'types_listings.type_id = listings.type_id');
    $this->db->join('pets', 'pets.pet_id = listings.pet_id');

    $this->db->where('listings.listing_id', $listing_id);
    $this->db->limit(1);

    return $this->db->get($this->table_name)->row();
}

PHP:

<?php foreach ($listing AS $listing_id): ?>
<li data-image-id="<?=$image -> image_id ?>" class="uploaded-image" id="image_<?=$image -> image_id ?>">
<div class="sortableimagewrapper">
<!-- <span class="label label-inverse image-name">&nbsp;</span> -->
<img src="<?=images_url('110x68/' . $listing -> file_name) ?>" alt="" width="110px" height="68px" class="img-polaroid" />
</div>
</li>                   
<?php endforeach; ?>    

class Listing_Model扩展了CW_Model {

protected $table_name = 'listings';
protected $order_by = 'listing_id';
protected $primary_key = 'listing_id';
protected $display_val = '';

public $defaults = array(
    'status_id' => 1
    );

public $qualifiers = array('listing_id', 'account_id', 'status_id', 'image_id', 'file_name', 'featured', 'feature_id', 'deal_id', 'nbrhood_id', 'beds_id', 'baths_id', 'pet_id', 'type_id', 'owner_id', 'web_id', 'price', 'convertible', 'available', 'sqft', 'address', 'unit', 'zip', 'description', 'notes');

public $rules = array(
            'new' => array(
                    'listing_id' => array(
                        'field' => 'listing_id',
                        'label' => '',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'account_id' => array(
                        'field' => 'account_id',
                        'label' => '',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'status_id' => array(
                        'field' => 'status_id',
                        'label' => '',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'image_id' => array(
                        'field' => 'image_id',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'file_name' => array(
                        'field' => 'file_name',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'featured' => array(
                        'field' => 'featured',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'feature_id' => array(
                        'field' => 'feature_id',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'deal_id' => array(
                        'field' => 'deal_id',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'nbrhood_id' => array(
                        'field' => 'nbrhood_id',
                        'label' => 'Neighborhood',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'beds_id' => array(
                        'field' => 'beds_id',
                        'label' => 'Bedrooms',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'baths_id' => array(
                        'field' => 'baths_id',
                        'label' => 'Bathrooms',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'pet_id' => array(
                        'field' => 'pet_id',
                        'label' => 'Pets',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'type_id' => array(
                        'field' => 'type_id',
                        'label' => 'Listing Type',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'owner_id' => array(
                        'field' => 'owner_id',
                        'label' => 'Owner/Landlord',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'web_id' => array(
                        'field' => 'web_id',
                        'label' => 'Web ID',
                        'rules' => 'trim|alpha_dash|xss_clean'
                        ),
                    'price' => array(
                        'field' => 'price',
                        'label' => 'Price',
                        'rules' => 'trim|required|min_length[3]|integer|xss_clean'
                        ),                      
                    'convertible' => array(
                        'field' => 'convertible',
                        'label' => 'Convertible',
                        'rules' => 'trim|is_natural|xss_clean'
                        ),                          
                    'dateAvailable' => array(
                        'field' => 'dateAvailable',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'available' => array(
                        'field' => 'available',
                        'label' => 'Date Available',
                        'rules' => 'trim|required|xss_clean'
                        ),                      
                    'sqft' => array(
                        'field' => 'sqft',
                        'label' => 'Square Feet',
                        'rules' => 'trim|min_length[3]|integer|xss_clean'
                        ),
                    'address' => array(
                        'field' => 'address',
                        'label' => 'Street Address',
                        'rules' => 'trim|required|min_length[12]|xss_clean'
                        ),
                    'unit' => array(
                        'field' => 'unit',
                        'label' => 'Unit Number',
                        'rules' => 'trim|required|max_length[5]|alpha_dash|xss_clean'
                        ),
                    'zip' => array(
                        'field' => 'zip',
                        'label' => 'Zip Code',
                        'rules' => 'trim|required|exact_length[5]|integer|xss_clean'
                        ),                  
                    'description' => array(
                        'field' => 'description',
                        'label' => 'Description',
                        'rules' => 'trim|required|min_length[175]|xss_clean'
                        ),
                    'notes' => array(
                        'field' => 'notes',
                        'label' => 'Notes',
                        'rules' => 'trim|xss_clean'
                        ),
                    'amenity_ids[unit][]' => array(
                        'field' => 'amenity_ids[unit][]',
                        'label' => 'Unit Amenities',
                        'rules' => 'trim|xss_clean'
                        ),
                    'amenity_ids[property][]' => array(
                        'field' => 'amenity_ids[property][]',
                        'label' => 'Property Amenities',
                        'rules' => 'trim|xss_clean'
                        ),                          
                    'image_ids[]' => array(
                        'field' => 'image_ids[]',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        )

                    ),
            'edit' => array(
                    'listing_id' => array(
                        'field' => 'listing_id',
                        'label' => '',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'account_id' => array(
                        'field' => 'account_id',
                        'label' => '',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'status_id' => array(
                        'field' => 'status_id',
                        'label' => '',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'image_id' => array(
                        'field' => 'image_id',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'file_name' => array(
                        'field' => 'file_name',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'featured' => array(
                        'field' => 'featured',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'feature_id' => array(
                        'field' => 'feature_id',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'deal_id' => array(
                        'field' => 'deal_id',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'nbrhood_id' => array(
                        'field' => 'nbrhood_id',
                        'label' => 'Neighborhood',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'price' => array(
                        'field' => 'price',
                        'label' => 'Price',
                        'rules' => 'trim|required|min_length[3]|integer|xss_clean'
                        ),
                    'beds_id' => array(
                        'field' => 'beds_id',
                        'label' => 'Bedrooms',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'convertible' => array(
                        'field' => 'convertible',
                        'label' => 'Convertible',
                        'rules' => 'trim|is_natural|xss_clean'
                        ),
                    'baths_id' => array(
                        'field' => 'baths_id',
                        'label' => 'Bathrooms',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'dateAvailable' => array(
                        'field' => 'dateAvailable',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'available' => array(
                        'field' => 'available',
                        'label' => 'Date Available',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'pet_id' => array(
                        'field' => 'pet_id',
                        'label' => 'Pets',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'sqft' => array(
                        'field' => 'sqft',
                        'label' => 'Square Feet',
                        'rules' => 'trim|min_length[3]|integer|xss_clean'
                        ),
                    'type_id' => array(
                        'field' => 'type_id',
                        'label' => 'Listing Type',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'description' => array(
                        'field' => 'description',
                        'label' => 'Description',
                        'rules' => 'trim|required|min_length[175]|xss_clean'
                        ),
                    'amenity_ids[unit][]' => array(
                        'field' => 'amenity_ids[unit][]',
                        'label' => 'Unit Amenities',
                        'rules' => 'trim|xss_clean'
                        ),
                    'amenity_ids[property][]' => array(
                        'field' => 'amenity_ids[property][]',
                        'label' => 'Property Amenities',
                        'rules' => 'trim|xss_clean'
                        ),
                    'owner_id' => array(
                        'field' => 'owner_id',
                        'label' => 'Owner/Landlord',
                        'rules' => 'trim|required|xss_clean'
                        ),
                    'address' => array(
                        'field' => 'address',
                        'label' => 'Street Address',
                        'rules' => 'trim|required|min_length[12]|xss_clean'
                        ),
                    'unit' => array(
                        'field' => 'unit',
                        'label' => 'Unit Number',
                        'rules' => 'trim|required|max_length[5]|alpha_dash|xss_clean'
                        ),
                    'zip' => array(
                        'field' => 'zip',
                        'label' => 'Zip Code',
                        'rules' => 'trim|required|exact_length[5]|integer|xss_clean'
                        ),
                    'notes' => array(
                        'field' => 'notes',
                        'label' => 'Notes',
                        'rules' => 'trim|xss_clean'
                        ),
                    'image_ids[]' => array(
                        'field' => 'image_ids[]',
                        'label' => '',
                        'rules' => 'trim|xss_clean'
                        ),
                    'web_id' => array(
                        'field' => 'web_id',
                        'label' => 'Web ID',
                        'rules' => 'trim|alpha_dash|xss_clean'
                        )
                    )
                );

public function __construct() {

    parent::__construct();

}

public function getNew() {

    // Remember everytime you add a field you have to add it to form validation...

    $listing = new stdClass();

    $listing->listing_id = $this->_get_identifier();
    $listing->account_id = $this->session->userdata('account_id');
    $listing->status_id = 0;
    $listing->feature_id = '';
    $listing->image_id = 0;
    $listing->nbrhood_id = ''; // Dropdown
    $listing->beds_id = ''; // Dropdown
    $listing->baths_id = ''; // Dropdown
    $listing->pet_id = ''; // Dropdown
    $listing->type_id = ''; // Dropdown
    $listing->owner_id = ''; // Dropdown
    $listing->web_id = 'CWA' . random_string('numeric', 7); // varchar
    $listing->featured = ''; // bool
    $listing->deal_id = '0'; // bool
    $listing->price = ''; // int
    $listing->convertible = ''; // bool
    $listing->dateAvailable = ''; // Only to populate visible jQuery datapicker
    $listing->available = ''; // date
    $listing->sqft = ''; // int
    $listing->address = ''; // varchar
    $listing->unit = ''; // varchar
    $listing->description = ''; // text
    $listing->zip = ''; // int
    $listing->notes = ''; // text
    $listing->amenity_ids['unit'] = array(); // Dropdown
    $listing->amenity_ids['property'] = array(); // Dropdown
    $listing->image_ids = array();

    return $listing;

}

2 个答案:

答案 0 :(得分:0)

我不了解您的数据模型。你应该在这里发布。如果您的数据模型已正确完成,那么这就是可以更改的行:

$this->db->join('images', 'images.image_id = listings.image_id', 'left');

$this->db->join('images', 'images.listing_id = listings.id');

您应该将商家信息ID存储在Images表中,而不是反过来。

编辑: 从数据模型,我的意思是数据库结构。如果每个“列表”都有n个图像,那么您需要在“图像”表中存储对“列表ID”的引用。

答案 1 :(得分:0)

从别人那里接过这个项目。我将在哪里向图像表添加字段。现在在CMS中,它显示了列表的多个图像。代码是:

                <?php foreach ($images as $image): ?>

                <li data-image-id="<?=$image -> image_id ?>" class="uploaded-image" id="image_<?=$image -> image_id ?>">
                    <div class="sortableimagewrapper">
                        <!-- <span class="label label-inverse image-name">&nbsp;</span> -->
                        <img src="<?=images_url('110x68/' . $image -> file_name) ?>" alt="" width="110px" height="68px" class="img-polaroid" />
                        <div class="btn-group">
                            <button class="btn <?php
                            if ($image -> image_id == $listing -> image_id)
                                echo 'btn-warning';
                        ?> image-default"><span class="icon-star <?php
                        if ($image -> image_id == $listing -> image_id)
                            echo 'icon-white';
                        ?>"></span></button>
                            <button class="btn image-remove"><span class="icon-remove"></span></button>
                        </div>
                    </div>
                </li>

                <?php endforeach; ?>
            </ol>