使用我的代码段而不是外部的getresources调用来优化我的Modx片段?

时间:2014-09-26 21:43:36

标签: php code-snippets modx

所以我有一个getResources调用,通过这个调用给我一个整数值:

[[!getResources? &parents=`[[*id]]` &totalVar=`totalLinks`]]

输出到[[+ totalLinks]]并使用它输入到我的代码段

[[!ChangeNumberToWord? &input=`[[+totalLinks]]`]]

我的片段:

$input = '';


function converttoword($total){
    if ($total=="1"){
        $word = "one";
    } elseif($total=="2") {
        $word = "two";
    } elseif($total=="3") {
        $word = "three";
    } elseif($total=="4") {
        $word = "four";
    } elseif($total=="5") {
        $word = "five";
    } elseif($total=="6") {
        $word= "six";
    } elseif($total=="7") {
        $word ="seven";
    } elseif($total=="8") {
        $word = "eight";
    } else{
        $word = "$total";
    }
return $word;                          
}

$output = converttoword($input);

return $output;

我的问题是如何将这些2粘在一起所以我只需要调用我的代码片段?

1 个答案:

答案 0 :(得分:2)

完全摆脱getResources调用,在你的代码片段中使用:getChildIds:

http://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.getchildids

类似的东西:

<?php
// current resource ID
$id = $modx->resource->get('id');

// get all child ids
$array_child_ids = $modx->getChildIds($id);

//so you would count that array 
$num_children = count($array_child_ids);

// get rid of the ifs to find the word
$words = array('zero','one','two','three','four','five','six');

// do something if no results
if ($num_children + 1 > count($words)){

    return 'out of range';

}

// return the string
return $words[$num_children];

因此,根据您的应用,您可能需要查看其他问题:

  • 如果没有孩子怎么办?
  • 资源状态或类型[已发布与未发布,符号链接等]
  • 如果孩子的数量是3033 [三千三百三十三]会怎样?

[提示:你可以google&#34; php将数字转换为它的字符串名称&#34;并提出几个选项]