du0923 2014-09-26 21:43
浏览 29
已采纳

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

SO I have a getResources call giving me an integer value with this call:

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

That outputs to [[+totalLinks]] and I use that to input in to my snippet

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

My Snippet:

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

My question is how I glue these 2 together so I only need to call my snippet?

  • 写回答

1条回答 默认 最新

  • dongtang5229 2014-09-26 23:22
    关注

    Get rid of the getResources call altogether, use: getChildIds in your snippet:

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

    something like:

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

    so you have other problems you might need to look at depending on your application:

    • what if there are zero children?
    • what about resource status or type [published vs. unpublished, symlinks etc]
    • what happens if the number of children is 3033 [Three thousand, three hundred and thirty-three]?

    [hint: you can google "php convert a number to it's string name" and come up with several options]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题