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

Ked*_*r B 5 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].",";
    }
} 
Run Code Online (Sandbox Code Playgroud)

$all_categories我得到一个给定身份的所有孩子,get_cats并且get_gender是功能.

0适合男性,1适合女性.我想首先显示"儿子"这个词然后再显示他们的名字,然后再计算儿子的数量,然后再写上"女儿"及其名字和女儿的数量.

现在我正在显示"儿子"这个词然后他们的名字,然后是"女儿"和他们的名字,但"儿子"和"女儿"这两个词正在显示,即使给定的身份证没有孩子.

Rya*_*ent 2

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

编辑以显示子计数。那么我希望答案能被接受。

添加了将所有姓名打印在一行上,然后是计数。

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

$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';   
}
Run Code Online (Sandbox Code Playgroud)