Jump to content
Fluoresce

How do I echo results in alphabetical order?

Recommended Posts

How do I present these documentaries like this:

 

0-9

10 Things You Need to Know About Sleep

20 Animals That Will Kill You

 

A

Ant Kingdom

Atkins Diet

 

B

Battle of the Brains

Body Talk

 

I don't even know where to start!

 

I'm not asking anyone to do this for me; I just need a push in the right direction.

 

Any help will be appreciated.

 

Share this post


Link to post
Share on other sites

It kind of depends on how you are accessing them to begin with.  If in a database, you can just specify the ORDER. If coming from somewhere else, you can put the values into an array and then sort() them.

  • Like 1

Share this post


Link to post
Share on other sites

Yes, I can select them from a database and order them by name. But how do I echo them as explained above?

Share this post


Link to post
Share on other sites

By printing a heading with the first letter whenever the first letter changes.

  • Like 1

Share this post


Link to post
Share on other sites

You could do something like this?

$documentaries = array("A Great Film","A Not So Great Film","Brilliant Film","Chirpy Film");
$currentHeader = '';

foreach($documentaries as $documentary){

  $headerCheck = substr($documentary,0,1);

  if($currentHeader!==$headerCheck){
    echo '<h2>'.$headerCheck.'</h2>';
  }

  $currentHeader = $headerCheck;

  echo $documentary;
  echo "</br>";

}
Edited by paddy_fields
  • Like 1

Share this post


Link to post
Share on other sites

Okay, I seem to have done it.

 

What do you guys think?

 

Yes, I know I'm an amateur! I only know if/else statements!

$con = mysql_connect("", "", "") or handle_mysql_error("Con failed! ");
mysql_select_db("") or handle_mysql_error("Select db failed! ");
$sql = "SELECT title, link FROM `doc_table` ORDER BY title ASC";                    
$query = mysql_query($sql, $con) or handle_mysql_error("Query failed! ");
echo"<h1>Full List</h1>";
echo "<div id=\"floated-left\">";
$prevLetter = '';
$count = 0;
while($row = mysql_fetch_assoc($query)) {                            
     $currLetter = substr($row['title'], 0, 1);                                
     if(strpbrk($currLetter, '0123456789') == true) {
          $currLetter = '0-9';
     }                                
     if($currLetter !== $prevLetter) {
          if($prevLetter) {
               if($counter == 14) {
                    echo "</ol>";
                    echo "</div><!--Close floated-left-->";
                    echo "<div id=\"floated-right\">";
               }
               else {
                    echo "</ol>";
               }
          }
          echo "<h3>$currLetter</h3>";
          echo "<ol>";
          $prevLetter = $currLetter;
          $count++;
     }                                                                
     echo "<li><a href=\"{$row['link']}\">{$row['title']}</a></li>";
}
echo "</ol>";
echo "</div><!--Close floated-right-->";
echo "<div class=\"clear\"></div><!--Clear floated-left and floated-right-->";
Edited by Fluoresce

Share this post


Link to post
Share on other sites

Where does $counter come from? It looks like that should be $count?

 

Not a big deal, but you could use is_numeric() here (I think it reads a little better):

if(strpbrk($currLetter, '0123456789') == true) {
if (is_numeric($currLetter)) {
  • Like 1

Share this post


Link to post
Share on other sites

Also, it appears that if you have a title that starts with a lower case "a" and another with a capital "A", it will output in 2 separate headers instead of putting them both under "A".

 

I'd change these lines:

$currLetter = strtolower(substr($row, 0, 1)); //force to lowercase for the comparison
echo "<h3>" . strtoupper($currLetter) . "</h3>"; //output uppercase
  • Like 1

Share this post


Link to post
Share on other sites

 

Where does $counter come from? It looks like that should be $count?

 

Not a big deal, but you could use is_numeric() here (I think it reads a little better):

if(strpbrk($currLetter, '0123456789') == true) {
if (is_numeric($currLetter)) {

Good spot regarding $counter!

 

Thanks for is_numeric(). Used it.

 

Thanks also for the strtoupper() advice.

Share this post


Link to post
Share on other sites

 

You could do something like this?

$documentaries = array("A Great Film","A Not So Great Film","Brilliant Film","Chirpy Film");
$currentHeader = '';

foreach($documentaries as $documentary){

  $headerCheck = substr($documentary,0,1);

  if($currentHeader!==$headerCheck){
    echo '<h2>'.$headerCheck.'</h2>';
  }

  $currentHeader = $headerCheck;

  echo $documentary;
  echo "</br>";

}

Thanks for trying. Your code was helpful.

Share this post


Link to post
Share on other sites

What about if the title is not based on the English alphabet?

<?php

//$row['title'] = 'jazz';

$row['title'] = 'джаз';

$currLetter = substr($row['title'], 0, 1);                                
   
 echo $currLetter

You might be considered using multibyte string functions in php.

 

You do nothing to prevent your code of sql injections. Read this article up - https://www.owasp.org/index.php/SQL_Injection

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.