如何在他们自己的cloumn中按字母顺序排列值

时间:2018-03-29 04:08:02

标签: php

我根据他们的起始字母按字母顺序排列了值     section,以A开头的值在A列标题下,和     以B开头的公司在B标题下...或者如果我没有价值     使用B的值然后跳过B.而且只显示5个值......但是     问题是值从他们的列标题中分离出来,我想在他们自己的cloumn标题中显示 ...这里是我的     代码。

  <?php
    $sql = "select company_name from companies ORDER BY company_name ";
    $result = mysqli_query($con, $sql);
    while($data = mysqli_fetch_assoc($result)){
       $company = $data['company_name'];
       creatCompanyList($company, $letter,$count);
     }
   $count = 0;  
   $letter = '';
    //echo $count;
  function creatCompanyList($company, &$letter, &$count){
    // echo $count;
    if($letter !== $company[0]) {
       if($count >0) { 
          endCompanyList();
          $count = 0;
      }
      $letter = $company[0];
      startCompanyList($letter, $count);
    }
   if($letter == $company[0] && $count <= 4) addCompanyList($company);
   if($count==4) endCompanyList();
     $count++;
  }
  function startCompanyList($letter, &$count){
  ?>
  <li class="loop-entry">
    <div class="col"> <span><?php echo $letter?></span>
      <div class="list-col">
  <?php
   }
  function endCompanyList(){
    ?>
    </div>
  </div>
  </li>         
  <?php
  }
 function addCompanyList($company){
 ?>
 <a href="#"><?php echo $company?></a>
 <?php
 }
 ?>

这是网页的屏幕截图,我想要E列标题中的E值

this is shot of webpage, and i wants the column header dont repeat and values of E show in E column header

1 个答案:

答案 0 :(得分:0)

您的代码认为字母的小写和大写不一样。修复了strtolower()中包含if statements条件的问题。

还为标题字母添加了strtoupper(),因此它总是显示为大写,以防小写字母作为标题传递。

更新代码:

  <?php

    $sql = "select company_name from companies ORDER BY company_name ";
    $result = mysqli_query($con, $sql);
    while($data = mysqli_fetch_assoc($result)){
       $company = $data['company_name'];
       creatCompanyList($company, $letter,$count);
     }
   $count = 0;  
   $letter = '';
    //echo $count;
  function creatCompanyList($company, &$letter, &$count){
    // echo $count;
    if(strtolower($letter) !== strtolower($company[0])) {
       if($count >0) { 
          endCompanyList();
          $count = 0;
      }
      $letter = $company[0];
      startCompanyList($letter, $count);
    }
   if(strtolower( $letter ) == strtolower( $company[0] ) && $count <= 4) addCompanyList($company);
   if($count==4) endCompanyList();
     $count++;
  }
  function startCompanyList($letter, &$count){
  ?>
  <li class="loop-entry">
    <div class="col"> <span><?php echo strtoupper( $letter )?></span>
      <div class="list-col">
  <?php
   }
  function endCompanyList(){
    ?>
    </div>
  </div>
  </li>         
  <?php
  }
 function addCompanyList($company){
 ?>
 <a href="#"><?php echo $company?></a>
 <?php
 }
 ?>