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

标签 php mysql codeigniter

$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 - 带有 Composer 的多个应用程序

php - 如何计算不同时间跨度内总分的百分位排名?

Mysql条件: case when and LEFT join cannot make them work properly

php - 文件上的 SEO HTML 状态 301?

php - 表中每行的另一个表中的项目数

php - FCM 链接在桌面通知中不起作用

php - 同步本地数据库和现场数据库

php - 启用 PHP 扩展时出错

java - 没有合适的驱动程序连接到使用 java 的 mysql 数据库 web 主机

php - MySQL 未插入列