duangou1551 2018-03-21 18:13
浏览 107
已采纳

如何在php中按字母顺序排列值

I am working on job portal project, and want to arrange values alphabetically according to their starting alphabet section, I want companies starting with A to be under the A column header, and companies starting with B to be under the B header..." or if i have no companies with the value of B then B be skipped.

<span>a</span>
<?php         
    $sql = "select * from companies";
    $result = mysqli_query($con, $sql);
    while($data = mysqli_fetch_array($result)) {
        $company = $data['company_name'];
?>
 <div class="list-col"> <a href="#"><?php echo $company; ?>(0)</a>  </div>
<?php
    }
?>
 </div>
 </li>
<!-- Sample of how column in HTML -->
<li class="loop-entry">
  <div class="col"> 
    <span>C</span>
    <div class="list-col"> 
       <a href="#">Company Name (0)</a> 
       <a href="#">Company Name (1)</a> 
       <a href="#">Company Name (2)</a> 
       <a href="#">Company Name (3)</a> 
       <a href="#">Company Name (4)</a> 
   </div>
 </div>
</li>              

i wants to arrange according to this image

enter image description here

  • 写回答

1条回答 默认 最新

  • doukezi4576 2018-03-22 00:26
    关注

    Okay here it is. I used functions to capture what you wanted. I will detail more later to explain things that you need to understand.

    I completed an example on PHP Sandbox with an array of values to display the code as outputted in text in HTML

    <?php         
        $sql = "select * from companies ORDER BY company_name";
        $result = mysqli_query($con, $sql);
        $letter = '';
        $count = 0;
        while($data = mysqli_fetch_array($result)) {
            $company = $data['company_name'];
            createCompanyList($company, $letter,$count);
        }
    /**
    creates Company Listing
    */
    
    function creatCompanyList($company, &$letter, &$count){
    
        if($letter !== $company[0]) {
              if($count >0) { 
                  endCompanyList();
                  $count = 0;
              }
              $letter = $company[0];
              startCompanyList($letter, $count);
         }
    
        //only adds the company if the letter and company matches 
        //and permits only the first 5 companies 
        if($letter == $company[0] && $count <= 4) addCompanyList($company);
    
        if($count==4) endCompanyList();
        $count++;
    }
    
    //creates opening divs for a Company List based upon letter
    function startCompanyList($letter, &$count){
      ?>
      <li class="loop-entry">
      <div class="col"> <span><?=$letter?></span>
      <div class="list-col">
      <?php
    }
    
    //closes divs for the Company List
    function endCompanyList(){   
      ?>
      </div>
      </div>
      </li>         
      <?php
    }
    
    //inserts company into to a column
    function addCompanyList($company){
      ?>
      <a href="#"><?=$company?></a>
      <?php
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题