I need some help figuring this out. I am trying to create a function that outputs values in an array alphabetically, but also echoes a new letter title after it reaches the end of the current letter. Ex:
A
Amazon
America
B
Bahamas
Bermuda
C
Canada
Cambodia
Etc.
I can do it by repeating the code from “$result” on so far, but I understand it’s just not good practice, so I’m looking for something that includes a variable instead of the “a%”, and autoincrements the letter (stopping at Z), after each successful run of the script.
<?php
$username = "myusername";
$password = "mypsswrd";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("mydb",$dbhandle)
or die("Could not select database");
$result = mysql_query("SELECT user, name FROM users WHERE name like 'a%' ORDER BY name");
echo "A<br />";
while ($row = mysql_fetch_array($result)) {
echo "<a href=\"/artists/artist.php?user=".$row{'user'}."\">".$row{'name'}."</a><br />";
}
echo "<br />";
1 Answer