2014-09-26 95 views
0

所以我有一個getResources打電話給我這個電話的整數值:優化我的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個,所以我只需要調用我的代碼段?

回答

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]; 

所以您有其他問題,您可能需要看視在您的應用程序上:

  • 如果有zer o孩子?
  • 怎麼樣的資源狀態或類型[已發佈與未發佈,符號鏈接等]
  • 如果孩子的數量是3033 [三千三百三十三],會發生什麼?

[提示:你可以谷歌「的PHP將數字轉換到它的字符串名稱」,並拿出幾個選項]

+0

感謝這個,這將讓我去! – TVD 2014-09-27 17:38:47

+0

如果它有效,您應該將其標記爲答案。 – 2014-09-27 17:57:19