Jump to content

How do I echo results in alphabetical order?


Fluoresce
Go to solution Solved by Fluoresce,

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.

 

Link to comment
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
Link to comment
Share on other sites

  • Solution

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
Link to comment
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
Link to comment
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
Link to comment
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.

Link to comment
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.

Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.