Jump to content

How do I echo results in alphabetical order?


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.

 

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>";

}

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-->";

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)) {

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

 

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.

 

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.