Forum

Thread tagged as: Question

Importing Data into Collections

Hi All ,

I am absolutely new to perch runway . Right now i have a CRM which has listings of real estates . This CRM can output data as an XML .

<Listing><count>1</count><Ad_Type>Rent</Ad_Type><Unit_Type>Warehouse</Unit_Type><Unit_Model>Covered Shed</Unit_Model><Primary_View>Asphalted Road</Primary_View><Unit_Builtup_Area>2939.00</Unit_Builtup_Area><No_of_Bathroom/><Property_Title>Direct From the Owner Brand New Warehouse for Rent </Property_Title><Web_Remarks><ul><li>Rate /Sq.ft : 23-27 AED .</li><li>Area: 3,000 to 53,000 sq. ft.</li><li>Utilities & Sewerage: To be borne by Tenant.</li><li>Power connection:3 Phaseand Single Phase (4500 - 7000 watt).</li><li>Renewal Terms: 3 year fixed rate contract & annual renewal thereafter.</li><li>Payment Terms: (4) Chqs in Favor of (Emirates for Industrial Cities Co.).</li><li>10 % Deposit Dated starting of the Contract + 2 Months Guarantee: Open Dated Chq.</li><li>Service Charges: Related to maintenance & Garbage removal etc. is borne by ECIC.</li><li>All warehouses insured by the owner, warehouses contents to be insured by the tenant.</li><li>No commission; No annual Admin fees; fixed rate for next 3 years.</li><li>Documents Required: Valid T.L, Bank Statement, partners Passport Copy & Emirates ID, main office tenancy contract.</li><li>Warehouses subjected to Sharjah Emirate Authorities (Economic Dept; Sharjah Municipality; SEWA; Civil Defense).</li><li>NOCs ready upon request free of charge for Decore and modification inside Warehouses i.e (Racking System; offices; increasing electricity load; adding Exhuast fans; etc ).</li><li>All waerhouses provided with the latest standards of civil Defense & firefighting system i.e ( sprinklers; smoke & heat detectors; Fire hose; diesel & electrical pump connected by Etisalat to the civil defense department. Water Lines & Fittings,Doors, Electrical Fittings & Lightings,Floor Base,Painting Internal & External,Emergency Door & Toilet Keys.</li></ul> <br /> PROPERTY FEATURES:  <br />-24 hours Maintenance <br />-Bank/ATM Facility <br />-Bus services <br />-Business Center <br />-Mosque</Web_Remarks><Emirate>Sharjah</Emirate><Community>Al Sajaa</Community><Exclusive>1</Exclusive><Cheques>4</Cheques><Plot_Area>0</Plot_Area><Property_Name>Emirates Industrial City</Property_Name><Property_Ref_No>EMT-R-1002</Property_Ref_No><Listing_Agent>Muhannad Ahmed</Listing_Agent><Listing_Agent_Phone>0544588988</Listing_Agent_Phone><Listing_Date>2018-08-14 11:59:17 am</Listing_Date><Last_Updated>2018-11-11 2:54:28 pm</Last_Updated><Bedrooms>ST</Bedrooms><Listing_Agent_Email>muhannad.d@ei-city.net</Listing_Agent_Email><Price>70536</Price><Frequency>per year</Frequency><Unit_Reference_No>EMT-R-1002</Unit_Reference_No><No_of_Rooms>ST</No_of_Rooms><Latitude>25.3073140</Latitude><Longitude>55.6037920</Longitude><unit_measure>Sq.Ft.</unit_measure><Featured>1</Featured><Fitted>Semi-Fitted</Fitted><Images><image>https://watermark.propspace.com/watermark?c_id=3158&l_id=1576257625901031&aid=1470166&id=15342311358342284&wmt=1533213441&image=14_08_2018-12_17_46-3158-134b116ecafd3f6eb5dba00611961025.jpeg</image><image>https://watermark.propspace.com/watermark?c_id=3158&l_id=1576257625901031&aid=1470166&id=15342311358342284&wmt=1533213441&image=14_08_2018-12_17_46-3158-ea71f17b3c0e0ac0a2328028b52deec9.jpeg</image><image>https://watermark.propspace.com/watermark?c_id=3158&l_id=1576257625901031&aid=1470166&id=15342311358342284&wmt=1533213441&image=14_08_2018-12_17_46-3158-60a5abb8cb864d2add5a5957497c82e4.jpeg</image><image>https://watermark.propspace.com/watermark?c_id=3158&l_id=1576257625901031&aid=1470166&id=15342311358342284&wmt=1533213441&image=14_08_2018-12_17_46-3158-34385b9b1f5ef8a86fc22343b982f552.jpeg</image><image>https://watermark.propspace.com/watermark?c_id=3158&l_id=1576257625901031&aid=1470166&id=15342311358342284&wmt=1533213441&image=14_08_2018-12_17_46-3158-b1915f35db7bd917166f76e18f2b63a4.jpeg</image><image>https://watermark.propspace.com/watermark?c_id=3158&l_id=1576257625901031&aid=1470166&id=15342311358342284&wmt=1533213441&image=14_08_2018-12_17_46-3158-abc2a54be77025f009eac20dcb9ee46d.jpeg</image><image>https://watermark.propspace.com/watermark?c_id=3158&l_id=1576257625901031&aid=1470166&id=15342311358342284&wmt=1533213441&image=14_08_2018-12_17_46-3158-ba62449ef1932475a9e0893b83c2e013.jpeg</image></Images><Facilities><facility>24 hours Maintenance</facility><facility>Bank/ATM Facility</facility><facility>Bus services</facility><facility>Business Center</facility><facility>Mosque</facility></Facilities><company_name>Emirates Industrial City</company_name><Web_Tour>https://youtu.be/_92oK5-odbk</Web_Tour><Threesixty_Tour/><Audio_Tour/><Virtual_Tour/><QR_Code/><company_logo>https://crm.propspace.com/application/views/pictures/logos/31581533213435.png</company_logo><Parking>3</Parking><Strno/><PreviewLink>https://crm.propspace.com/preview/index/15342311358342284/3158/?l_id=1576257625901031</PreviewLink><price_on_application>No</price_on_application><off_plan>0</off_plan><permit_number/><completion_status/></Listing>

I am trying to create a website which can do the below .

  1. Take the feed and store it as items in perch .
  2. Display the items as required based on html templates

I do understand that the import is only possible by collections . The only question here is do i treat <Listing> as one type of content and all that comes below as attributes of the content ?

Titus Saju

Titus Saju 0 points

  • 2 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that sounds sensible.

It will be better to take help from the technical team https://babasupport.org/windows/fix-windows-10-error-code-0x8024a105/ which will able to provide the best suggestion regarding this topic.

Just one question , can we perform a search on collections ?

Just one question , can we perform a search on collections ?

Yes. You can perform simple search within admin panel of collections.

No the search is for the users landing on the page . For eg lets say i have a collection of property listings . Is there a way the user( page visitor) can perform a search ( just like the one you see in an e-commerce website with filters ) ?

https://docs.grabaperch.com/functions/search/

Official apps also register themselves with search so their content will be included.

Search Functions Search is part of Perch Core functionality and enables you to add a simple text search to your site.

Search can only search content that has been added through Perch and not static pages that you have added. Official apps also register themselves with search so their content will be included.

Perch comes complete with an example search page in the root of the download folder called search.php.

Also in the Perch download is a template in perch/templates/search. This template has all of the search template fields in it, including paging and can be used as is or you can edit the mark-up as with any Perch template.

To get search working make sure you have the template in your site in perch/templates/search then visit search.php in your browser and search for things that appear in your content.

ALSO SEE: perch_collection() as you can formulate a filter based on search and out put the filtered response...

<?php
perch_collection('Properties', [
    'filter' => [
        [
            'filter' => 'bedrooms',
            'match'  => 'gte',
            'value'  => 3,
        ],
        [
            'filter' => 'price',
            'match'  => 'lte',
            'value'  => 500000,
        ],
    ]
]);
?>

Hi Robert ,

I tried writing a php script for importing the collections from the xml .

<?php
include('perch/runtime.php');
$API      = new PerchAPI(1.0, 'my_importer');
$Importer = $API->get('CollectionImporter');
$Importer->set_collection('property');
$Template = $API->get('Template');
$Template->set('/shades/perch/templates/content', 'property');

$Importer->set_template($Template);
$Importer->empty_collection();

$xml=simplexml_load_file("https://shades.mycrm.com/feed/privatesite/0744a0be5fac33c3b3e69c6b26e97d1e") or die("Error: Cannot create object");

foreach($xml->children() as $propertylistings) { 
echo $propertylistings->reference_number;
    try {
    $Importer->add_item([
        'property'    => $propertylisting->reference_number,
    ]);    
} catch (Exception $e) {
    die('Error: '.$e->getMessage());
}
} 
?> 

And my template was


<perch:content id="property" type="text" label="Title" required>

unfortunately , i get the warning :

NA-1166143
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\shades\perch\core\runway\lib\PerchCollectionImporter.class.php on line 54
NA -1057859
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\shades\perch\core\runway\lib\PerchCollectionImporter.class.php on line 54
YE-1174881
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\shades\perch\core\runway\lib\PerchCollectionImporter.class.php on line 54

I could see that it is looping through the xml , but what did i do wrong here ? i can also see that the collections still has 0 items in it .

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Titus,

Your template path and namespace don't look right:

$Template->set('/shades/perch/templates/content', 'property');

If your template is /perch/templates/content/property.html, then you would use

$Template->set('content/property', 'content');

Perfect , that helped out !

Just one last question , have a look at the below xml

<property>
<sample>
<image>
<url></url>
<url></url>
</image>
</sample>
</property>

how do i iterate through nested childrens ? for eg: here in images , there are multiple urls . how do i store them in collections ?

 'property_image'    =>  [$propertylisting->image->url]  ,

I tried the above it doesn't work out .

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

how do i iterate through nested childrens ? for eg: here in images , there are multiple urls .

This is not a Perch-specific question. You need to look at how you parse your XML. If you expect $propertylisting->image to have children, you can iterate over it the same way you're iterating over $xml:

$images = array();

foreach($propertylisting->image->children() as $image_url) {
$images[] = $image_url->__toString(); 
}

// now array $images contain your image URLs

how do i store them in collections?

Do you only want to store the URLs? Or do you want to import the images as Perch Assets?

I need to import them as perch assets . Thanks a lot for the effort btw I'm kind of new into PHP .

I tried this out , but how do I input multiple images to one item ?

This is what confuses me .


<?php include('perch/runtime.php'); $API = new PerchAPI(1.0, 'my_importer'); $Importer = $API->get('CollectionImporter'); $Importer->set_collection('property'); $Template = $API->get('Template'); $Template->set('content/text', 'content'); $Importer->set_template($Template); $Importer->empty_collection(); $xml=simplexml_load_file("feed.xml") or die("Error: Cannot create object"); foreach($xml->children() as $propertylisting) { $images = array(); foreach($propertylisting->photo->children() as $image_url) { $images[] = $image_url->__toString(); } try { $Importer->add_item([ 'permit_number' => $propertylisting->reference_number , 'reference_number' => $propertylisting->permit_number , 'property_image' => $images[0] ]); } catch (Exception $e) { die('Error: '.$e->getMessage()); } } ?>

Never mind , i found out a solutions for it . Do let me know if i can do something better .

<?php
include('perch/runtime.php');
$API      = new PerchAPI(1.0, 'my_importer');
$Importer = $API->get('CollectionImporter');
$Importer->set_collection('property');
$Template = $API->get('Template');
$Template->set('content/text', 'content');
$Importer->set_template($Template);
$Importer->empty_collection();
$xml=simplexml_load_file("feed.xml") or die("Error: Cannot create object");
foreach($xml->children() as $propertylisting) { 
$images = array();
foreach($propertylisting->photo->children() as $image_url) {
$images[] = $image_url->__toString(); 
}
    try {
    $Importer->add_item([
        'permit_number'    =>  $propertylisting->reference_number  ,
        'reference_number'    =>  $propertylisting->permit_number  ,
        'property_image_1'    =>  $images[0],
         'property_image_2'    =>  $images[1],
        'property_image_3'    =>  $images[2],
        'property_image_4'    =>  $images[3],
        'property_image_5'    =>  $images[4],
        'property_image_6'    =>  $images[5],

    ]);    
} catch (Exception $e) {
    die('Error: '.$e->getMessage());
}
} 
?> 
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

how do I input multiple images to one item ?

Normally you would use a repeater:

<perch:repeater id="images" label="images">
<perch:content id="image_url" type="text" label="Image URL">
</perch:repeater>

But the API doesn't support importing content into repeaters at the moment; so you can't use repeaters for storing content.


I need to import them as perch assets

To simplify things, I would import the URLs as text instead.

  1. It would be more resource intensive to import lots of images as Perch Assets
  2. The API doesn't support importing into a repeater (you have a variable number of images)

A workaround would be to store all the URLs in a single text field separated by a character (e.g. ;).

Add a text field to your template:

<perch:content id="image_urls" type="text" label="Image URLs" help="Separated by semicolon" no-index>

Modify your importer accordingly:

foreach ($xml->children() as $propertylisting) {
    // array to hold the data you want to import
    $property_data = array();

    // add any data to the array $property_data
    $property_data = [
        'permit_number' => $propertylisting->reference_number,
        'reference_number' => $propertylisting->permit_number,
    ];



    // grab the images
    $images = array();
    foreach ($propertylisting->image->children() as $image_url) {
        $images[] = $image_url->__toString(); 
    }

    // add the images to your data array
    $property_data['image_urls'] = implode(';', $images);


    // import
    try {
        $Importer->add_item($property_data);
    } catch (Exception $e) {
        die('Error: ' . $e->getMessage());
    }
}

Then wherever you display the properties using perch_collection() use the each option to prepare the image URLs in an array so you can display them using a repeater:

perch_collection('properties', [
    'template' => 'properties/list',
    'each' => function($item) {
        if(isset($item['image_urls'])) {
            $images = explode(';', $item['image_urls']);
            $repeater_images = array();
            foreach($images as $image) {
                $repeater_images[] = ['image' => $image];
            }

            $item['images'] = $repeater_images;
        }

        return $item;
    }
]);

Then in your display template, you can display the images in a repeater:

<perch:repeater id="images">
<img src="<perch:content id="image">" >
</perch:repeater>

p.s. don't add the repeater to the edit template. Only add it to templates you use for displaying the content on the site.