0

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?

RiaanV
  • 260
  • 2
  • 14
  • you are getting image name.. you will have to convert it into image path so that browser can fetch it – N Kumar Oct 03 '15 at 22:25
  • you can do like this e.g `` – Shehary Oct 03 '15 at 22:29
  • Okay each entry has more then one image though so how would display the images associated with the property? – RiaanV Oct 03 '15 at 22:34
  • @ventechit you should probably make a hash table with a unique identifier so that way you can easily map which image path goes to which property – John Ruddell Oct 04 '15 at 03:14
  • @JohnRuddell Thanks for the reply but when i do my upload the property info returns the $insert_id and that is added to the images table so now when i retrieve info the join adds the images to the query but now my how do i display all the images? I don't know hash tables could you maybe point me in the direction of a good tutorial? – RiaanV Oct 04 '15 at 07:43
  • @ventechit so a hash table has nothing to do with a database... all it is is an object with a unique key with a value for that key... so in php it would just be an array.. but it would look more like this. `$your_array['property_id'] = $image_name` or something on those lines... then when you are going to display the property all you would have to do is access the data by your property id... so like if its a for loop on all of the properties... you should have the property id from the database for that particular property and then just access the value for that particular property by its id – John Ruddell Oct 04 '15 at 07:53
  • look at this link... I think it can help you understand it better.. http://stackoverflow.com/questions/3134296/hash-tables-vs-associative-arrays – John Ruddell Oct 04 '15 at 07:55
  • Could i show you my model, view, controller? I'm lost hahahah – RiaanV Oct 04 '15 at 08:00
  • Yea probably.. Before you do you are pulling out the property data from your database right? You could store all of the related data for that particular property in a hash table and that way all the html you display can be related to each property – John Ruddell Oct 04 '15 at 08:10
  • Let's say for example you have property x and property y.... You query the database and now you have that data in a php array... Transforming the data to a hash table is really useful.. So lets say both properties (x and y) have a unique id. You can make an array where the key is the id for x in one slot and y for another.. and the value for each key is the corresponding property object.. Does that make sense? – John Ruddell Oct 04 '15 at 08:15
  • Yeah i have two tables, property and property images, so when i upload a property the property info gets added to the property table and the insert id is returned then the insert_id of that is added to the image_id field in the property_images table – RiaanV Oct 04 '15 at 08:16
  • Hash table is just a data structure in all programming languages... It has nothing to do with a database... So like in php you have integers.. Arrays.. Hashtables are the same type of thing.. It's a data type/data structure.. Just like an integer is or a string is.. Make sense? – John Ruddell Oct 04 '15 at 08:19
  • So my query in my model is as above and then my controller i have $data['prop'] = $this->Property_m->get_residential(); and i just pass that to the view, then in the view i just say so how or where would i create this associative array? – RiaanV Oct 04 '15 at 08:21
  • Does `echo $prop['image_name']` not work? If not then you would have to change the data model back before it gets to your view... Try just doing that echo in a h1 html element... To see if you have the value or not – John Ruddell Oct 04 '15 at 08:25
  • it does work but only returns one image, i need it to return all images associated with the particular $id as in my property_image table – RiaanV Oct 04 '15 at 08:30
  • See what i want to do is echo those images out in a carousel [link](http://hollowaypropertiesdolphincoast.co.za/property/view/salt-rock-splendour) this is my page to help you understand better – RiaanV Oct 04 '15 at 08:33
  • Ohhh.. Well your problem is with the MySQL query... You have a group by with no aggregate function.. So that is only going to return one image. You should download a SQL query ide to test your queries first – John Ruddell Oct 04 '15 at 08:40
  • So how would i fix this? I am such a noob when it comes to this stuff....0.o Really appreciate the time you are giving I will compensate you if you like – RiaanV Oct 04 '15 at 08:41
  • I would move this to two queries.. And then use a hash table to add all of your property images – John Ruddell Oct 04 '15 at 12:45
  • can you please show me how? I'm so desperate – RiaanV Oct 04 '15 at 13:47
  • I can try but I don't know php lol.. Can you show me how you loop through the database results? – John Ruddell Oct 04 '15 at 15:47
  • @johnRuddell I use MVC approach with codeiginter so the query above is in my model and then in my view i just use $data['prop'] = $this->Property_m->get_property(); that then passed the info retrieved from the model to the view – RiaanV Oct 04 '15 at 16:45
  • So $data['prop'] has all the rows of properties from the db? – John Ruddell Oct 04 '15 at 16:48
  • then in the view i just do $prop['property_name'] and it shows the result – RiaanV Oct 04 '15 at 16:50
  • Ok so make another query that pulls out all of the property images... Then loop through all of the results for your properties.. Make a hash table off of that.. Then loop through the property images and add them to the hash table by the property id.. If you can't figure it out I'll write up the pseudo code for it later when I have time (in like 5 hours) – John Ruddell Oct 04 '15 at 17:35
  • Thank please do write the code and i will try in the meantime. – RiaanV Oct 04 '15 at 18:00

1 Answers1

0

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'
        )
    )
)
John Ruddell
  • 25,283
  • 6
  • 57
  • 86
  • You are my hero, thank you so much John will let you know once i am done – RiaanV Oct 05 '15 at 07:18
  • @ventechit did what I did make sense? you should use this type of programming whenever you need to map two objects together like that. it is efficient and the best way to do it – John Ruddell Oct 07 '15 at 02:22
  • I tried but i could not make sense of it but took notes thanks, i did manage to figure it out though, my sql was wrong and i needed a foreach and not a while loop....but thanks for the input – RiaanV Oct 08 '15 at 20:49