草庐IT

php - 在codeigniter php中根据性别显示不同的前缀

coder 2023-10-06 原文

$all_categories = get_cats($cat);
echo "&nbsp&nbsp"."Sons";

for ($i=0;$i<sizeof($all_categories);$i++) {     
    $arr = get_gender($cat);

    if ($arr[$i]=='0') {
        echo "&nbsp&nbsp".$all_categories[$i].",";
    }  
} 

echo "&nbsp&nbsp"."Daughters";

for ($i=0;$i<sizeof($all_categories);$i++) {     
    $arr = get_gender($cat);

    if ($arr[$i]=='1') {
        echo "&nbsp&nbsp".$all_categories[$i].",";
    }
} 

$all_categories 中,我正在获取给定 ID 的所有子项,get_catsget_gender 是函数。

0 代表男性,1 代表女性。我想先显示“儿子”这个词,然后是他们的名字,然后是儿子的数量,然后是“女儿”这个词以及他们的名字和女儿的数量。

现在我显示“儿子”这个词,然后是他们的名字,然后是“女儿”这个词和他们的名字,但是即使给定的 id 没有 child ,也会显示“儿子”和“女儿”这两个词。

最佳答案

添加了更多未测试代码的未测试代码:-) 因为你不知道你是否有任何“儿子”,直到你进入“for”循环,而且可能没有。您必须在“第一次”打印任何“儿子”时打印标题。 las,这意味着您当前代码的“first_time”标志。

经过编辑以显示 child 数量。那么我希望这个答案能够被采纳。

添加了在一行中打印所有名称,然后是计数。

添加了单独存储计数并显示计数的简单“与”测试。

$all_categories = get_cats($cat);

$headingPrinted = false;
$sonCount = 0;
$outputLine = '';

for ($i=0;$i<sizeof($all_categories);$i++) {     
    $arr = get_gender($cat);

    if ($arr[$i]=='0') {

        if (!$headingPrinted) {
          $outputLine .= "&nbsp&nbsp"."Sons";
          $headingPrinted = true;
        }    

        // append to the current outputLine...  
        $outputLine .= "&nbsp&nbsp".$all_categories[$i].",";
        $sonCount++;
    }  
} 
// print $outputline and child count if at least one was found. show plural if more than one 
if ($sonCount >= 1) {
   echo $outputLine, $childCount, $childCount == 1 ? 'son': 'sons', ' found';  
} 
else {
  // you may want to do something if none found
}


// repeat for the other heading

$headingPrinted = false;
$daughterCount = 0;
$outputLine = '';

for ($i=0;$i<sizeof($all_categories);$i++) {     
    $arr = get_gender($cat);

    if ($arr[$i]=='1') {

      if (!$headingPrinted) {
        $outputLine .= "&nbsp&nbsp"."Daughters";
        $headingPrinted = true;
      }    


      // append to the current outputLine...  
      $outputLine .= "&nbsp&nbsp".$all_categories[$i].",";
      $daughterCount++;
    }
}  

// print child count if at least one was found 
if ($daughterCount >= 1) {
   echo $outputLine, $childCount, $childCount == 1 ? 'daughter': 'daughters', ' found';  
} 
else {
  // you may want to do something if none found
}

// test combined totals of children...
if ($sonCount == 1 &&  daughterCount == 1) {
   echo 'wow - one of each';   
}

关于php - 在codeigniter php中根据性别显示不同的前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22268971/

有关php - 在codeigniter php中根据性别显示不同的前缀的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  4. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  5. ruby - 如何根据特征实现 FactoryGirl 的条件行为 - 2

    我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden

  6. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  7. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  8. ruby-on-rails - 在 Flash 警报 Rails 3 中显示错误消息 - 2

    如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]

  9. java - 为什么 ruby​​ modulo 与 java/other lang 不同? - 2

    我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.

  10. ruby-on-rails - Rails 4 WYSIWYG Bootsy 不显示格式 - 2

    我刚刚按照thebootsygempage上的安装说明进行操作在我保存并查看帖子内容之前,一切看起来都不错。这是输出在View中的样子:HeaderSubhead:似乎没有呈现任何html格式,因为它被引号或类似的东西转义了-其他人有这个问题吗?我没有在github页面或SO上看到任何问题来指出我正确的方向。除了遵循gem安装说明之外,我还没有做任何事情,但也许我错过了什么或者只是犯了一个愚蠢的错误。如果你还有什么想知道的,请尽管问。干杯 最佳答案 你需要有这样的东西,转义html: 关

随机推荐