-3

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

mcv
  • 1,380
  • 3
  • 16
  • 41
Umar
  • 11
  • 5
  • maybe use SQL's [`ORDER BY`](https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html)? – ficuscr Mar 21 '18 at 18:16
  • `select * from companies ORDER BY company_name` – RiggsFolly Mar 21 '18 at 18:17
  • Or maybe `select * from companies ORDER BY company_name DESC` – RiggsFolly Mar 21 '18 at 18:18
  • select * from companies ORDER BY company_name DESC arranges just alphabets...https://i.stack.imgur.com/Hp7d9.pngaccording to this image – Umar Mar 21 '18 at 18:20
  • Here is an example: https://stackoverflow.com/questions/6760222/group-php-results-on-first-letter-of-name and https://stackoverflow.com/questions/14576075/group-by-first-letter-alphabetically-best-way?rq=1 – ficuscr Mar 21 '18 at 18:24
  • The image makes no sense. You want to order things like that or this is a sample of how your ordering currently looks? – mcv Mar 21 '18 at 18:25
  • @mcv i want to order things like that – Umar Mar 21 '18 at 18:27
  • Like ***what*** ? – IncredibleHat Mar 21 '18 at 18:28
  • @IncredibleHat the values in A section are coming from database and i want to arrange them in their alphabatic section – Umar Mar 21 '18 at 18:29
  • But your image shows A with non A values beneath it. Or this image you are showing us is a really poor example. – mcv Mar 21 '18 at 18:31
  • 1
    Do you mean: "I want companies starting with **A** to be under the **A** column header, and companies starting with B to be under the B header..." etc? I agree, that image of "*according to this image*" is confusing. – IncredibleHat Mar 21 '18 at 18:31
  • Well, the code you have needs a total rewrite to make that happen. Depends as well if you want to do a separate query for each column, or pull everything out of the database and pre-process it in php variables first. (how big/how many companies you expecting to list?) – IncredibleHat Mar 21 '18 at 18:34
  • @IncredibleHat u r right:Do you mean: "I want companies starting with A to be under the A column header, and companies starting with B to be under the B header..." etc?............sory for bad image ,,,actually this is screnshot of my template – Umar Mar 21 '18 at 18:37
  • 1
    @mcv sory 4 bad img .... The values in A column are coming from db as mentioned in above code and "I want companies starting with A to be under the A column header, and companies starting with B to be under the B header..." – Umar Mar 21 '18 at 18:38
  • @IncredibleHat just 5 companies to show and and companies will must come from database, cant pull them out of db,,, and how to use a which query will preferable coz every query is giving me errors – Umar Mar 21 '18 at 18:43
  • @Umar if you have no companies with the value of B can B be skipped or should it appear and be left empty? – mcv Mar 21 '18 at 18:44
  • The query suggested above by other users is correct. You just need to check the first letter of each company before putting it into a div. Your code above behaves like it does because the code is not checking the values that are being returned from the database. – mcv Mar 21 '18 at 18:47
  • @mcv if i have no companies with the value of B then B be skipped – Umar Mar 21 '18 at 18:47
  • I would highly suggest you edit the details of this question to include this information. :) I will try to assist you but I am busy right now. – mcv Mar 21 '18 at 19:01
  • @mcv thnx for your response nd wi8ing for your assistance in itcoz: – Umar Mar 21 '18 at 19:38

1 Answers1

0

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
}
?>
mcv
  • 1,380
  • 3
  • 16
  • 41
  • now the last error is Fatal error: Call-time pass-by-reference has been removed in ,,,,,,,,,which is: createCompanyList($company, &$letter,&$count); .................... Php Sandbox is showing same error,,, i removed the call-time pas-by-ref & but not wroking – Umar Mar 22 '18 at 05:14
  • No the PHP Sandbox link is correct. If you press execute it should work with no errors. I noted the line when calling createCompanyList inside the while loop I forgot to remove the &. That is the incorrect location. I fixed it in the code above. Also when you get an error look at the line number it is referring. It will point you to the exact line which the code is failing. – mcv Mar 22 '18 at 10:31
  • @Umar if this solution answers your question, thank acknowledge it as your preferred answer – mcv Mar 22 '18 at 11:55
  • thnx @mcv for your great response,,,actually i was at weekend holidays, i hv checked the code in sandbox which is working seprately, but in temp. it shows: Fatal error: Call to undefined function createCompanyList() ,,,,,,,,which is createCompanyList($company, $letter,$count); – Umar Mar 26 '18 at 03:34
  • How have you included the functions above in your code? Is everything on the same file? Or are creating a new issue by storing them in a separate file? Is your code Object Oriented? If you search [Fatal error: Call to undefined function](https://stackoverflow.com/questions/47492353/php-fatal-error-call-to-undefined-function) you will find this a common error. I have linked to one potential solution. – mcv Mar 26 '18 at 19:44
  • thanks a lot @mcv for your great support,,,its working fine now :) – Umar Mar 28 '18 at 18:27