Nottice that your "A" is always 1? No matter wad?
The error occurs here
$letter = substr($row["movie_title"],0,1);
//see if they are the same letter
if($letter != $letter2)
{
echo "<br>" . $letter;
echo " - " . $counter;
$counter = 1;
}
Notice that before that you declared your $letter2 as "". Therefore on the very first while loop. $letter = "A" (presumming the first string is Asadasdas) and $letter2 is "".
This cause the if else statement to be logically correct and therefore print it to be 1 and go on to other statements.
Also, in the if statement, you set $counter =1, which is incorrect, because at the start of the while loop, you already do a $counter++. Therefore causing the extra count in each group.
Try this
$letter = "";
$letter2 = "";
$counter = 0;
$loop = 1; //Use as test variable.
while ($row = @ mysql_fetch_array($rs))
{
$counter++;
//get first letter of title
$letter = substr($row["movie_title"],0,1);
// This make sure you don't skip the first loop.
if ($loop == 1) {
$letter2 = $letter;
$loop = 0;
}
//see if they are the same letter
if($letter != $letter2) {
echo "<br>" . $letter;
echo " - " . $counter;
$counter = 0;
}
// make $letter2 equal $letter
$letter2 = $letter;
}