2016-10-10 45 views
1

我需要统计每个特定类别中有多少个单词。举例来说,就我而言,我现在正在做这个工作来计算特定列表中有多少个单词。Group按字数统计转换表Laravel 5.3

@if (str_word_count($stats->description) < 250) 
    0 - 249 
@elseif(str_word_count($stats->description) >= 250 && str_word_count($stats->description) <= 500) 
    250 - 500 
@elseif(str_word_count($stats->description) >= 501 && str_word_count($stats->description) <= 750) 
    500 - 750 
@else 
    750+ 
@endif 

最后,我有一个表,我需要按每个类别计数。像这样:

<table class="table table-condensed table-responsive"> 
         <thead> 
          <tr> 
           <th class="text-center" colspan="6">Conversion Rate For Word Count</th> 
          </tr> 
          <tr> 
           <th>0 - 249 words</th> 
           <th>250 - 500 words</th> 
           <th>500 - 750 words</th> 
           <th>750 + words</th> 
          </tr> 
         </thead> 
         <tbody> 
          <tr> 
           <td></td> 
           <td></td> 
           <td></td> 
           <td></td> 
          </tr> 
         </tbody> 
        </table> 

我该如何计算所有有0到249个单词的列表等等?

回答

1

会这样的工作吗?

PHP

$counts = ['0-249'=>0, '250-499'=>0, '500-749'=>0, '750+'=>0]; 
@if (str_word_count($stats->description) < 250) 
    $counts['0-249']++; 
@elseif(str_word_count($stats->description) >= 250 && str_word_count($stats->description) <= 500) 
    $counts['250-499']++; 
@elseif(str_word_count($stats->description) >= 501 && str_word_count($stats->description) <= 750) 
    $counts['500-749']++; 
@else 
    $counts['750+']++; 
@endif 

HTML

   <table class="table table-condensed table-responsive"> 
        <thead> 
         <tr> 
          <th class="text-center" colspan="6">Conversion Rate For Word Count</th> 
         </tr> 
         <tr> 
          <th>0 - 249 words</th> 
          <th>250 - 499 words</th> 
          <th>500 - 749 words</th> 
          <th>750 + words</th> 
         </tr> 
        </thead> 
        <tbody> 
         <tr> 
          <td><?php echo $counts['0-249']; ?></td> 
          <td><?php echo $counts['250-499']; ?></td> 
          <td><?php echo $counts['500-749']; ?></td> 
          <td><?php echo $counts['750+']; ?></td> 
         </tr> 
        </tbody> 
       </table> 
+0

是的,这工作 – David