dongtuo1482
dongtuo1482
2015-10-03 22:22

如何从已连接的表格中显示我的图像

已采纳

Hi I have two tables property and property_images I join these tables with the following query:

        function get_residential(){
        $query = $this->db->query("SELECT
                    property.ID,
                    property.property_name,
                    property.property_slug,
                    property.property_size,
                    property.property_beds,
                    property.property_bath,
                    property.property_garage,
                    property.property_type,
                    property.property_state,
                    property.property_price,
                    property.property_address,
                    property.property_description,
                    property.date_created,
                    property.active,
                    property_image.image_name
                    FROM
                    property AS property
                    INNER JOIN property_images AS property_image ON property.ID = property_image.image_id
                    WHERE property.property_type = 'Residential'
                    GROUP BY property.ID
                    ORDER BY property.ID");
        return $query->result('array');
    }

At the moment i am able to output all the property description information but not the images, how would i go about outputing the images related to the property in the view?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dsxpt62448 dsxpt62448 6年前

    note this is basically pseudo code (im using what I remember about php). I am not proficient in php so adjust accordingly

    lets assume this is the property data you have from your database.

    $property_data = //the following array
    array(
        [0] => array ('property_id' => 1, 'property_name' => 'mansion'),
        [1] => array ('property_id' => 2, 'property_name' => 'brick house')
    )
    

    and lets assume this is the property_image data from your database

    $property_image_data = //the following array
    array(
        [0] => array ('image_property_id' => 1, 'image_url' => 'some url 1'),
        [1] => array ('image_property_id' => 1, 'image_url' => 'some url 2'),
        [2] => array ('image_property_id' => 1, 'image_url' => 'some url 3'),
        [3] => array ('image_property_id' => 2, 'image_url' => 'some url 4'),
        [4] => array ('image_property_id' => 2, 'image_url' => 'some url 5'),
        [5] => array ('image_property_id' => 2, 'image_url' => 'some url 6'),
    )
    

    what you want to do is something like this

    $property_hash_table = array();
    for ($i = 0; $i < count($property_data); ++$i) {
        $property_id = $property_data[$i]['property_id'];
        $property_data[$i]['image_urls'] = array(); //we want an array for all of the associated urls for this property
        $property_hash_table[$property_id] = $property_data[$i];
    }
    

    now that we have a hash table for each property by their unique id. we can then loop over the images and add them to the correct property

    for ($i = 0; $i < count($property_data); ++$i) {
        $image_property_id = $property_image_data[$i]['image_property_id'];
        $image_url = $property_image_data[$i]['image_url'];
        if(array_key_exists($image_property_id, $property_hash_table)){
            //we have a product in our hash table that this image belongs to
            array_push($property_data[$image_property_id]['image_urls'], $image_url);
    
        }
    }
    

    the final outcome (the data in the hash table) will look like this

    array(
        // notice the key here ([1]) is actually the property id from the database
        [1] => array (
            'property_id' => 1, 
            'property_name' => 'mansion', 
            'image_urls' => array(
                [0] => 'some url 1',
                [1] => 'some url 2',
                [2] => 'some url 3'
            )
        ),
        [2] => array (
            'property_id' => 2, 
            'property_name' => 'brick house', 
            'image_urls' => array(
                [0] => 'some url 4',
                [1] => 'some url 5',
                [2] => 'some url 6'
            )
        )
    )
    
    点赞 评论 复制链接分享

相关推荐