Forum for discussing general topics related to Couch.
5 posts Page 1 of 1
Hello,

This question involves some PHP coding. I have a website which has content that repeats on a couple of pages. The website has no database but I have a 'data' directory in which I have a few .inc files which contain hardcoded PHP arrays. I then have some PHP code which iterates through those arrays and echos the data on the page with the HTML formatting that I need.

Here is an example of a data file called countries.inc
<?php
/* This is an array of data used to dynamically create a list of countries, it contains country-code, name and flag */
$countries = array (
array("US","United States of America", "usa.png"),
array("UK","United Kingdom", "gb.png"),
array("FR","France", "france.png"),
array("ES","Spain", "spain.png"),
);
?>

Now on my PHP page I use the above file like this:
<?php
include('data/countries.inc'); // Load the data from the external inc file
$countries_count = sizeof($countries);
foreach($countries as $country){ // Dynamically create each item in the loop
echo "<div class='col-md-6 service-box'>
<div class='classic-testimonials item'>
<span>$country[1]</span>
<span>$country[2]</span>
<span><img src='images/countries/". $country[3] ."' /></span>
</div>
</div>";
}
?>

I am wondering if it is possible to extend the CouchCMS code to handle these arrays, to somehow get them controlled by the CMS. It might require writing some new PHP code into the CouchCMS stuff, or maybe there is an easier way.

I'd love your input on this, I think it would be a useful feature for the CMS.

Regards,
Lucas
Hello Lucas,

As it happens, I had been working on precisely this very feature (i.e. incorporating Arrays as Couch variables) for the past few days :)
Your post made me commit the changes to GitHub to release the feature. I have also done a post describing how things work -
viewtopic.php?f=5&t=10892&p=27841#p27841

Please download Couch from GitHub (https://github.com/CouchCMS/CouchCMS) to try the solution below.

In the aforementioned post, we use JSON from the front-end template to input the arrays. Internally, the JSON object is converted into native PHP array so we can bypass the <cms:set> or <cms:capture> setter statements and directly use PHP to set an array into a Couch variable.

I'll use your use-case to explain the process.
Please place the following in your template to include the 'countries.inc' file defining the array and then setting that array as a global Couch variable named 'my_countries' -
Code: Select all
<cms:php>
    global $CTX;

    include( K_SITE_DIR.'data/countries.inc' ); // Load the data from the external inc file
    $CTX->set( 'my_countries', $countries, 'global', 1 ); // set array as a global Couch variable named 'my_countries'
</cms:php>

Please notice that I have placed the 'countries.inc' file into a folder named 'data' in the site's root.

OK, with that done, we can now use the 'my_countries' Couch variable the regular way.
First to test if our variable indeed holds an array, try the following -
Code: Select all
<cms:show my_countries as_json='1' />

It should output a JSON representation of the array.

Next, use the following to iterate through the array and output your HTML -
Code: Select all
<cms:each my_countries as='country'>
    <div class='col-md-6 service-box'>
        <div class='classic-testimonials item'>
            <span><cms:show country.0 /></span>
            <span><cms:show country.1 /></span>
            <span><img src='images/countries/<cms:show country.2 />' /></span>
        </div>
    </div>
</cms:each>

Hope it helps.
Hi KK,

Many thanks for this. It will definitely be useful and I appreciate you using my specific example to explain it.

It solves one half of the problem that I am trying to deal with but not the other. What I hope to achieve is 'CMSify' the arrays. By this I mean use the Couch Admin area to allow users to edit the content of those arrays. To do that I would be getting rid of the hardcoded arrays and moving the data to the database. There would be no more arrays actually, just a series of repeating rows from the database, each row representing a Country.

To solve this problem might be very different from what you posted above actually because the most important part is to be able to use the Couch CMS Admin to control the data, to have it in the CMS.

I think I could do it using globals and repeatable elements somehow but I can't figure out how.

Do you have any recommendations for this?

Thanks again!
Lucas.
In the admin-panel, the closest thing to arrays that we have got are the 'repeatable-regions' (http://docs.couchcms.com/concepts/repea ... gions.html).

For your use-case, you may define a repeatable-region named 'countries' with two text regions e.g. 'name', 'code' and one 'image' region for the flag.

You can the easily iterate through the rows to output the kind of markup you want.

Hope it helps.
That sounds good KK, I think that could solve my problem, thank you!

I hope to try it out at the weekend and I'll let you know how it goes.

Regards,
Lucas
5 posts Page 1 of 1

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 1 guest

cron