$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].",";
}
}
In $all_categories
I'm getting all child of a given id and get_cats
and get_gender
are functions.
0
is for male and 1
is for female. I want to display the word "Sons" first then their names and then count of sons and then word "Daughters" and their names and count of daughters.
Now I am displaying the word "sons" then their names and then the word "daughters" and their names but the words "sons" and "daughters" are being displayed even if given id has no children.
Untested code with more untested code added :-) As you do not know if you have any 'sons' until you are in the 'for' loop, and there may be none. You have to print the heading the 'first time' you print any 'sons'. Alas, that means a 'first_time' flag with your current code.
Edited to display child counts. Then i hope the answer will be accepted.
Added printing all the names on one line followed by the count.
Added storing counts separately and show simple 'and' test on the counts.
$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';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With