为codeigniter php中的每个数组元素显示不同的后缀 [英] display different suffix for each array element in codeigniter php

查看:113
本文介绍了为codeigniter php中的每个数组元素显示不同的后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$all_categories=get_cats($cat);
$headingPrinted = false;
$childCount = 0;
for($i=0;$i<sizeof($all_categories);$i++)
{    

 $arr=get_gender($cat);

 if($arr[$i]=='0')

 {
    if (!$headingPrinted) {
     echo "&nbsp&nbsp"."Sons:";
      $headingPrinted = true;
    }  
 echo "&nbsp&nbsp".$all_categories[$i].",";
 $childCount++;
 }



 } 
     if ($childCount >= 3) {
echo $childCount,  $childCount == 1 ;  
} 
else {
// you may want to do something if none found
}
$headingPrinted = false;
$childCount = 0;

for($i=0;$i<sizeof($all_categories);$i++)
{    

 $arr=get_gender($cat);



if($arr[$i]=='1'){
if (!$headingPrinted) {
     echo "&nbsp&nbsp"."Daughters:";
      $headingPrinted = true;
    }  
echo "&nbsp&nbsp".$all_categories[$i].",";
$childCount++;
  }

  } 
 if ($childCount >= 3) {
 echo $childCount,  $childCount == 1 ;  
 } 
 else {
// you may want to do something if none found
 }

我显示的单词儿子,然后他们的名字,然后他们的计数和单词女儿
然后他们的名字,然后计数。我要显示为喜欢的人
儿子:a,b,c,d:4;
女儿:p,q,r,s:4;

$ all_categories是一个数组,其中获取给定id的所有子项,get_gender和get_cats是函数。

I am displaying the word "Sons" then their names then their count and the word "daughters" then their names then count. I want to display like this
Sons: a,b,c,d:4;
Daughters:p,q,r,s:4;
$all_categories is an array in which i m getting all child of given id and get_gender and get_cats are functions.

现在显示 a,b,c,d, :4; 。我只想删除数组的最后一个元素后面的逗号

我要显示计数,只有当儿子的数量大于2对于女儿来说也是一样的。

如果男孩数是1,那么Sons:a;对于女儿来说也是一样的。

如果男孩数是2,那么单词Sons:a,b;
如果男孩数大于2,那么儿子:a,b,c:3;对于女儿也是一样的。

我想显示结果和上面一样我说的。所以,任何人都可以尝试这个???

now i m displaying a,b,c,d, :4;. I just want to remove that comma after last element of array..
I want to display count only if count of sons is greater than 2 and same for daughters.
If boys count is 1 then the word "Sons:"a; and same for daughters.
if boys count is 2 then the word "Sons:" a,b; and same for daughters.
if boys count is greater than 2 then the word "Sons:" a,b,c:3; and same for daughters.
I want to display result same like above I said. So can anyone tried this???

推荐答案

去除最后一个显示儿子和女儿的逗号,检查下一个记录是否存在,然后附加逗号,否则转义逗号。

To Remove the last comma on last of displaying the Sons and daughters, check if next record exist then append the comma, else escape the comma.

$all_categories=get_cats($cat);
$headingPrinted = false;
$childCount = 0;
for($i=0;$i<sizeof($all_categories);$i++)
{    

 $arr=get_gender($cat);

 if($arr[$i]=='0')

 {
    if (!$headingPrinted) {
     echo "&nbsp&nbsp"."Sons:";
      $headingPrinted = true;
    }  
 echo "&nbsp&nbsp".$all_categories[$i];


if(isset($all_categories[$i+1])){
      echo ",";

   }
 $childCount++;
 }



 } 
     if ($childCount >= 3) {
echo $childCount,  $childCount == 1 ;  
} 
else {
// you may want to do something if none found
}
$headingPrinted = false;
$childCount = 0;

for($i=0;$i<sizeof($all_categories);$i++)
{    

 $arr=get_gender($cat);



if($arr[$i]=='1'){
if (!$headingPrinted) {
     echo "&nbsp&nbsp"."Daughters:";
      $headingPrinted = true;
    }  
echo "&nbsp&nbsp".$all_categories[$i];
if(isset($all_categories[$i+1])){
  echo ",";



}
    $childCount++;
      }

  } 
 if ($childCount >= 3) {
 echo $childCount,  $childCount == 1 ;  
 } 
 else {
// you may want to do something if none found
 }

这篇关于为codeigniter php中的每个数组元素显示不同的后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆