Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display different prefix according gender in codeigniter php

$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].",";
    }
} 

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.

like image 614
Kedar B Avatar asked Nov 02 '22 03:11

Kedar B


1 Answers

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 .= "&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';   
}
like image 73
Ryan Vincent Avatar answered Nov 09 '22 06:11

Ryan Vincent