$all_categories = get_cats($cat);
echo "  "."Sons";
for ($i=0;$i<sizeof($all_categories);$i++) {
$arr = get_gender($cat);
if ($arr[$i]=='0') {
echo "  ".$all_categories[$i].",";
}
}
echo "  "."Daughters";
for ($i=0;$i<sizeof($all_categories);$i++) {
$arr = get_gender($cat);
if ($arr[$i]=='1') {
echo "  ".$all_categories[$i].",";
}
}
在 $all_categories
中,我正在获取给定 ID 的所有子项,get_cats
和 get_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 .= "  "."Sons";
$headingPrinted = true;
}
// append to the current outputLine...
$outputLine .= "  ".$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 .= "  "."Daughters";
$headingPrinted = true;
}
// append to the current outputLine...
$outputLine .= "  ".$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/