How can I use getResoruce to build a grid

Hi,
I spent really a long time to find a solution to build this with getresource

<div class="container well3">
	<h3 class="wow fadeIn" data-wow-delay="0.2s">Sponsors</h3>
	<div class="row">
    	<div class="grid_4 wow fadeIn" data-wow-delay="0.4s">
            <ul class='marked-list'>
            	<li data-marker="A.">
                	<a href='#'>Aliquam pellentesque orci</a>
                </li>
                <li data-marker="B.">
                            <a href='#'>Elis, sed semper sapien</a>
                </li>
                <li data-marker="C.">
                            <a href='#'>Malesuada eget</a>
                </li>
                <li data-marker="D.">
                            <a href='#'>Nam vel aliquet justo at</a>
                </li>
                <li data-marker="E.">
                            <a href='#'>Morbi non nisl</a>
                </li>
            </ul>
		</div>
        <div class="grid_4 wow fadeIn" data-wow-delay="0.6s">

                    <ul class='marked-list'>
                        <li data-marker="A.">
                            <a href='#'>Elis, sed semper sapien</a>
                        </li>
                        <li data-marker="B.">
                            <a href='#'>Malesuada eget</a>
                        </li>
                        <li data-marker="C.">
                            <a href='#'>Nam vel aliquet justo at</a>
                        </li>
                        <li data-marker="D.">
                            <a href='#'>Morbi non nisl</a>
                        </li>
                        <li data-marker="E.">
                            <a href='#'>Varius quam moles</a>
                        </li>
                    </ul>
		</div>
        <div class="grid_4 wow fadeIn" data-wow-delay="0.8s">

                    <ul class='marked-list'>
                        <li data-marker="A.">
                            <a href='#'>Nam vel aliquet justo at</a>
                        </li>
                        <li data-marker="B.">
                            <a href='#'>Morbi non nisl</a>
                        </li>
                        <li data-marker="C.">
                            <a href='#'>Semper, varius quam</a>
                        </li>
                        <li data-marker="D.">
                            <a href='#'>Quisque blandit mauris</a>
                        </li>
                        <li data-marker="E.">
                            <a href='#'>Vitae mi auctor</a>
                        </li>
                    </ul>
		</div>
	</div>
</div>

The difficulty is to build the HTML grid as I have to a balance of <li>sponsor</li> within the colomn.
Actually, the aossiciation has 31 sponsors but the amount of sponsors must be flexible. In a couple of mounth, we can have 20 sponsors or 45 sponsors. For example, if later we have 45 spensors, I would like to have the list of 15 sponsors in each column.

I can start filling the first column, but how can I know the balance? **With getResource, can we calculate the amount of published resources?**and then, calculating the 1/3, 2/3, 3/3. In that way, I could build the first column with the 1/3 of the sponsors, then the second column with the 2/3 and the third column with the rest?

How would you fill a such grid? (I have to respect the HTML coce)

If you use pdoResources (instead of getResources), you can set the &return property to data to get the raw data back. When you call pdoResources in a snippet, you can then format the returned data however you like. Something like this:

<?php
// Run pdoResources and return raw data
$results = $modx->runSnippet('pdoResources', [
    'parents' => 11,
    'limit' => 0,
    'return' => 'data'
]);
$result_amount = count($results); // Determine the amount of results
$amount_per_column = ceil($result_amount / 3); // Calculate the amount of results in a column

$counter = 0;
$output = ''; // The output string
//$output .= '...'; // Add some HTML to the output to start the first column
foreach ($results as $result){
    $output .= $modx->getChunk('some_chunk', $result);
    $counter++;
    if ($counter >= $amount_per_column){
        $counter = 0;
        //$output .= '...'; // Add some HTML to the output to finish the column and start a new one
    }
}
//$output .= '...'; // Add some HTML to the output to complete the last column
return $output;

Hi,
but pdoResources can be installed via the extension like getResource? I can not find it

pdoResources is part of the extra pdoTools.

Yes, I just found that right know. I was ready to update my post but you were faster

Hello,
I could success it, but I have some minor issue :slight_smile:
Here is how it’s look: Nos sponsors - Société de développement d'Arzier-Le Muids

First, I would like to have the numer incrementing before each row.

If I can have letter, that better, but the number is fine.
I tried to add idx and to get the incrementing number in my CHUNK with [[+idx]], but it does not work

<?php
// Run pdoResources and return raw data
$results = $modx->runSnippet('pdoResources', [
    'parents' => 8,
	'idx' => 1,
'sortby'=> '{"longtitle":"ASC", "pagetitle":"ASC"}',
	'showUnpublished' => false,
    'limit' => 0,
    'return' => 'data'
]);
$result_amount = count($results); // Determine the amount of results
$amount_per_column = ceil($result_amount / 3); // Calculate the amount of results in a column

$counter = 0;
$output = ''; // The output string
$output .= '<div class="container well3">
	<h3 class="wow fadeIn" data-wow-delay="0.2s">Nos sponsors</h3>
	<div class="row">
    	<div class="grid_4 wow fadeIn" data-wow-delay="0.4s"><ul class="marked-list">
            '; // Add some HTML to the output to start the first column
foreach ($results as $result){
    $output .= $modx->getChunk('sponsorsTpl', $result);
    $counter++;
    if ($counter >= $amount_per_column){
        $counter = 0;
        $output .= '</ul></div>
        <div class="grid_4 wow fadeIn" data-wow-delay="0.6s"><ul class="marked-list">'; // Add some HTML to the output to finish the column and start a new one
    }
}
$output .= '</ul></div>
	</div>
</div>'; // Add some HTML to the output to complete the last column
return $output;

I saw in the doc, there is that option but I should badly use it.

Second question.
Each of my sponsor, I create a resource and the type is link

In the link field, I saved a URL and I am tried to get it into the href=""
To retreive retreive the page or long title, I can use [[+pagetitle]], but it does not work with [[+link]] or [[+content]]. Is there a different way to call it?

In getResourcem there is a properties ‘includeContent’. With pdo resources, is there something similar that I need to activate? I will need to test the link field, because if no URL is saved, I will remove the a attribute

You have to add the index yourself to the $result array before you call $modx->getChunk().
Something like this:

foreach ($results as $result){
    $result['idx'] = $counter + 1; // Placeholder [[+idx]] in your template
    $result['idx_char'] = mb_chr($counter + 65); // Placeholder [[+idx_char]] in your template
    $output .= $modx->getChunk('sponsorsTpl', $result);
    ...
}

The same property exists for pdoResources. If you set it to 1, then [[+content]] should output the link of the Weblink resource.

Great, thank a lot for your help, I make the following changes.
I used idx variable to return letter and I used $nb instead of $couter, because counter get back the value of 0 at each column.

Thanks!!


<?php
// Run pdoResources and return raw data
$alpha = array('A','B','C','D','E','F','G','H','I','J','K', 'L','M','N','O','P','Q','R','S','T','U','V','W','X ','Y','Z');
$results = $modx->runSnippet('pdoResources', [
    'parents' => 8,
	//'idx' => 1,
	'includeContent' => '1',
	'sortby'=> '{"longtitle":"ASC", "pagetitle":"ASC"}',
	'showUnpublished' => false,
    'limit' => 0,
    'return' => 'data'
]);
$result_amount = count($results); // Determine the amount of results
$amount_per_column = ceil($result_amount / 3); // Calculate the amount of results in a column

$nb = 0;
$counter = 0;
$output = ''; // The output string
$output .= '<div class="container well3">
	<h3 class="wow fadeIn" data-wow-delay="0.2s">Nos sponsors</h3>
	<div class="row">
    	<div class="grid_4 wow fadeIn" data-wow-delay="0.4s"><ul class="marked-list">
            '; // Add some HTML to the output to start the first column
foreach ($results as $result){
	$result['idx'] = $alpha[$nb];
    $output .= $modx->getChunk('sponsorsTpl', $result);
	$nb++;
	$counter++;
    if ($counter >= $amount_per_column){
        $counter = 0;
        $output .= '</ul></div>
        <div class="grid_4 wow fadeIn" data-wow-delay="0.6s"><ul class="marked-list">'; // Add some HTML to the output to finish the column and start a new one
    }
}
$output .= '</ul></div>
	</div>
</div>'; // Add some HTML to the output to complete the last column
return $output;

Hello @halftrainedharry
Thanks a lot for your help, all work fine know :+1: