getResources: Way to order Resources by &resources property? - modx

I want to order resources in the order I have them in the $resources property eg:
2,6,3,8
But this seems to defualt to menu index.
Does anyone know a way to achieve this?
My getResources call:
[[!getResources?
&tpl=`featuredtpl`
&resources=`2,6,3,8`
&limit=`4`
&includeTVs=`1`
&processTVs=`1`
&includeContent=`1`
&depth=`0`
]]

You can use it like this:
&sortby=`FIELD(modResource.id, 4,7,2,5,1 )`
So your snippet call will be like:
[[!getResources?
&tpl=`featuredtpl`
&resources=`2,6,3,8`
&sortby=`FIELD(modResource.id,2,6,3,8)`
&limit=`4`
&includeTVs=`1`
&processTVs=`1`
&includeContent=`1`
&depth=`0`
]]

Related

Add value to array only if not already in, using bash

As I said the title. I tried this code:
areasArray=()
while IFS= read -r line
do
areaName="$(awk -F ";" '{print $3}')"
echo $areaName
if [[ ! " ${areasArray[#]} " =~ " $areaName " ]]; then
areasArray+=($areaName)
echo ${areasArray[*]}
fi
done < $reportFile
$reportFile refers to a CSV file that looks like this:
something;something;US
something;something;US
something;something;UK
something;something;FR
something;something;UK
something;something;FR
And the array will looks like this: US US UK FR UK FR. But I want every zone to be added only if it's not already there. So it should looks like this: US UK FR. How can I do that? Thanks.
If you need to perform lookup, use an associative array, not an indexed array. Basically, you'll use the keys of the associate array like a set.
declare -A areasArray
while IFS=";" read _ _ areaName _; do
if [[ ! -v areasArray[$areaName] ]]; then
areasArray[$areaName]=
fi
done < "$reportFile"
Unless there is a specific action you want to take only if the key isn't already present, you can skip the if; areasArray[$areaName]= is idempotent.

Shell Script that performs different functions based on input from file

I am trying to merge two very different scripts together for consolidation and ease of use purposes. I have an idea of how I want these scripts to look and operate, but I could use some help getting started. Here is the flow and look of the script:
The input file would be a standard text file with this syntax:
#Vegetables
Broccoli|Green|14
Carrot|Orange|9
Tomato|Red|7
#Fruits
Apple|Red|15
Banana|Yellow|5
Grape|Purple|10
The script would take the input of this file. It would ignore the commented portions, but use them to dictate the output. So based on the fact that it is a Vegetable, it would perform a specific function with the values listed between the delimiter (|). Then it would go to the Fruits and do something different with the values, based on that delimiter. Perhaps, I would add Vegetable/Fruit to one of the values and dependent on that value it would perform the function while in this loop to read the file. Thank you for your help in getting this started.
UPDATE:
So I am trying to implement the IFS setup and thought of a more logical arrangement. The input file will have the "categories" displayed within the parameters. So the setup will be like this:
Vegetable|Carrot|Yellow
Fruit|Apple|Red
Vegetable|Tomato|Red
From there, the script will read in the lines and perform the function. So basically this type of setup in shell:
while read -r category item color
do
if [[ $category == "Vegetable" ]] ; then
echo "The $item is $color"
elif [[ $category == "Fruit" ]] ; then
echo "The $item is $color"
else
echo "Bad input"
done < "$input_file"
Something along those lines...I am just having trouble putting it all together.
Use read to input the lines. Do a case statement on their prefix:
{
while read DATA; do
case "$DATA" in
\#*) ... switch function ...;;
*) eval "$FUNCTION";;
esac
done
} <inputfile
Dependent on your problem you might want to experiment with setting $IFS before reading and read multiple variables in 1 go.
You can redefine the processing function each time you meet a # directive:
#! /bin/bash
while read line ; do
if [[ $line == '#Vegetables' ]] ; then
process () {
echo Vegetables: "$#"
}
elif [[ $line == '#Fruits' ]] ; then
process () {
echo Fruits: "$#"
}
else
process $line
fi
done < "$1"
Note that the script does not skip empty lines.

Using Loops to Generate Dynamic Channel Field Names?

Having a bit of an issue generating channel fields based on a variable. Below is my attempt at a PHP solution:
{related_entries id="image_gallery"}
<?php for ($i = 1; $i <= 15; $i++) { ?>
{image_<?php echo $i; ?>}
<?php } ?>
{/related_entries}
Unfortunately ExpresssionEngine doesn't render these tags and just outputs them as:
{image_1} {image_2} {image_3}... etc.
I've tried some other looping plugins with no luck either, but I assume there is a plugin or method out there to help me.
Bonus question: Ideally, I will need a method that I can also check to see if the channel field actually has any data like: {if image_x}{image_x}{/if}
Make sure you have set PHP to parse on input in your template preferences. That should do the trick.

Which Programming Language is this formula

Can someone tell me what language(s) the following formula is formatted for?
IF(($A) > $B,$B, IF(($A) < $C,$C,($A)))
I work in PHP and I can interpret what it's intended to do
(if $A > $B then $B, if $A < $C then $C, ELSE $A)
just not the language that would except it.
This looks like something that came out of Excel
in php it should be somthink like this...
<?php
if($A > $B){$B;}
elseif($A < $C){$C;}
else{$A;}
?>
Just following up to close this off.
First, there was a slight typo with an unecessary set of parenthesis in the example so ($A) could've just been $A in both cases.
In any event, I discovered that this was used for something that was originally written in "ABAP".

How do you append to an already existing string?

I want append to a string so that every time I loop over it, it will add "test" to the string.
Like in PHP you would do:
$teststr = "test1\n"
$teststr .= "test2\n"
echo = "$teststr"
Returns:
test1
test2
But I need to do this in a shell script
In classic sh, you have to do something like:
s=test1
s="${s}test2"
(there are lots of variations on that theme, like s="$s""test2")
In bash, you can use +=:
s=test1
s+=test2
$ string="test"
$ string="${string}test2"
$ echo $string
testtest2
#!/bin/bash
message="some text"
message="$message add some more"
echo $message
some text add some more
teststr=$'test1\n'
teststr+=$'test2\n'
echo "$teststr"
VAR=$VAR"$VARTOADD(STRING)"
echo $VAR
thank-you Ignacio Vazquez-Abrams
i adapted slightly for better ease of use :)
placed at top of script
NEW_LINE=$'\n'
then to use easily with other variables
variable1="test1"
variable2="test2"
DESCRIPTION="$variable1$NEW_LINE$variable2$NEW_LINE"
OR
to append thank-you William Pursell
DESCRIPTION="$variable1$NEW_LINE"
DESCRIPTION+="$variable2$NEW_LINE"
echo "$DESCRIPTION"
#!/bin/bash
msg1=${1} #First Parameter
msg2=${2} #Second Parameter
concatString=$msg1"$msg2" #Concatenated String
concatString2="$msg1$msg2"
echo $concatString
echo $concatString2

Resources