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
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?