dongtuo1482 2015-10-03 22:22
浏览 51
已采纳

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

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 2015-10-04 21:43
    关注

    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'
            )
        )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题