Changing letters to numbers?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Changing letters to numbers?

Post by jmansa »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have this script that list users in alphabethic order, but there is just 1 problem. I use the danish alphabet with the letters "Æ(ae), Ø(oe), Å(aa)". The problem is that it doesnt list those letters correct.

So my thourgt that if i changed the letters to numbers in an "array" it should be able to list them from 1 - 28, but the problem is that it doesnt seem to work. Can anybody help?

Code: Select all

$query = "SELECT uid, name, 3d, 2d, link FROM users ORDER BY REPLACE(LOWER(name),'aa','å') ASC";

$result = mysql_query($query);
$names = array();
while($r = mysql_fetch_row($result)) {
    $names[] = array('uid'=>$r[0],'name'=>$r[1],'3d'=>$r[2],'2d'=>$r[3],'link'=>$r[4]);
	sort($array); }	

$currLetter = array('A' => '1', 'B' => '2', 'C' => '3', 'D' => '4', 'E' => '5', 'F' => '6', 'G' => '7', 'H' => '8', 'I' => '9', 'J' => '10', 'K' => '11', 'L' => '12', 'M' => '13', 'N' => '14', 'O' => '15', 'P' => '16', 'Q' => '17', 'R' => '18', 'S' => '19', 'T' => '20', 'U' => '21', 'V' => '22', 'W' => '23', 'X' => '24', 'Z' => '25', 'Æ' => '26', 'Ø' => '27', 'Aa' => '28', 'Å' => '28');

$numColumns = 4;
$numNames = mysql_num_rows($result); 
$numNamesPerColumn = ceil($numNames/$numColumns);
?>

<table width="100%">
    <tr>
    <?php

    $currLetter = '';
    $c = 0;
    for($i=0;$i<$numColumns;$i++) {
        echo '<td width="25%" valign="top">';
        for($x=0;$x<$numNamesPerColumn;$x++) {
            if(!isset($names[$c])) { break; }
             
            if($names[$c]['name']{0} != $currLetter) {
			
				if($x!=0) { echo '<br>'; }
				$letterimages = array('A' => '1', 'B' => '2', 'C' => '3', 'D' => '4', 'E' => '5', 'F' => '6', 'G' => '7', 'H' => '8', 'I' => '9', 'J' => '10', 'K' => '11', 'L' => '12', 'M' => '13', 'N' => '14', 'O' => '15', 'P' => '16', 'Q' => '17', 'R' => '18', 'S' => '19', 'T' => '20', 'U' => '21', 'V' => '22', 'W' => '23', 'X' => '24', 'Z' => '25', 'Æ' => '26', 'Ø' => '27', 'Å' => '28');
				

echo '<img src="img/letters/'.$letterimages[strtoupper($names[$c]['name']{0})].'.png" alt="" /><br>';

                $currLetter = $names[$c]['name']{0};
            }
			
            echo '<a href="'.$names[$c]['link'].'">'.$names[$c]['name'].'</a>';
            if($names[$c]['3d']=='yes') { echo ' &nbsp;<img src="img/3d.gif" width="10" height="10"  
			alt="" /> '; }
            echo '<br>';
            $c++;
        }
        echo '</td>';
    }
    ?>
Thanks alot!!!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]1.[/b] Select the correct board for your query. Take some time to read the guidelines in the sticky topic.[/quote]
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

jmansa wrote:I use the danish alphabet with the letters "Æ(ae), Ø(oe), Å(aa)". The problem is that it doesnt list those letters correct.
Can you give an example please?
Which version of mysql do you use? (in case of doubt use SELECT Version()).
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

Post by jmansa »

I use "PHP version 4.4.2", but the problem does not seem to be there. I have tryid to "setLocale" and all that, so now im trying to attack the problem from another angle.

The names starting with the danish letters "Æ,Ø,Å" should be listed in that order but comes out like this "Ø,Å,Æ". Besides that in denmark we sometimes spell a name starting with Å with an dobbel AA. That also gives problem because the script sees that as an A. Thats why i would like to change the letters with numbers... Hopes that claryfies the issue!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

volka wrote:Which version of mysql do you use? (in case of doubt use SELECT Version()).
You can set a character/collation set for mysql databases and there are presets for danish.
e.g. with

Code: Select all

CREATE TABLE `danishtest` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `fornavn` varchar(48) collate latin1_danish_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_danish_ci;

INSERT INTO `danishtest` (`id`,`fornavn`) VALUES (1,'Ømbjørg');
INSERT INTO `danishtest` (`id`,`fornavn`) VALUES (2,'Ärngärdh');
INSERT INTO `danishtest` (`id`,`fornavn`) VALUES (3,'Æsa');
INSERT INTO `danishtest` (`id`,`fornavn`) VALUES (4,'Åsbjørg');
INSERT INTO `danishtest` (`id`,`fornavn`) VALUES (5,'Ørgine');
INSERT INTO `danishtest` (`id`,`fornavn`) VALUES (6,'Åsmund');
INSERT INTO `danishtest` (`id`,`fornavn`) VALUES (7,'Alessandra');
INSERT INTO `danishtest` (`id`,`fornavn`) VALUES (8,'Olaf');
INSERT INTO `danishtest` (`id`,`fornavn`) VALUES (10,'Gertrud');

Code: Select all

SELECT
      fornavn
FROM
    danishtest
ORDER BY
     fornavn
gives me
Alessandra
Gertrud
Olaf
Ärngärdh
Æsa
Ømbjørg
Ørgine
Åsbjørg
Åsmund
Post Reply