Forum Moderators: coopster

Message Too Old, No Replies

mysql data displaying by alphabetic

         

Shanee

6:14 am on Nov 29, 2007 (gmt 0)

10+ Year Member



how its possible if i want to display mysql data in alphabetic group with the help of php.

i.e

A:
Animals
Air

B:
Boy
Bell
ball

C:
Cat
Canal

etc....

Habtom

10:32 am on Nov 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The question is a bit broad, if you have tried doing it we could adjust the code accordingly.

Here is an approach though:

1. While retrieving the data from the database in your sql statement sort the data in that_field.

2. While trying to display the data, you keep on looping the data for display till you see a change in the first character of the words, like a change from A to B to C . . .

Hope this gives you a hint, but yes there can be easier ways of doing it based on what you exactly want to display.

Habtom

PHP_Chimp

10:34 am on Nov 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There may well be a better way to do this through your database query. However if you want to do it through php then you could use something like -

<?php
$input = // your sql data, assuming this is individual words in array format
$alph = array('a', 'b', 'c', ... , 'w', 'x', 'y', 'z');
foreach ($input as $word) {
for ($i=0; $i<26; $i++) {
$pattern = "%^$alph[$i]%i"; // starts with letter from alph
if (preg_match($pattern, $word, $matches) {
echo $alph[$i];
echo '<pre>';
print_r($matches);
echo '</pre>';
}
}
}
?>

You would need to actually put the complete $alph in there. I haven't tested that so there may be some problems, but hopefully it will give you an idea.

[edited by: PHP_Chimp at 10:36 am (utc) on Nov. 29, 2007]