PDA

View Full Version : Another question, alphabetical


Tidus
08-03-2002, 01:50 PM
Ok.
I have now made a lyrics page and it is supposed to find the first letter in every title i have in the database and list as displayed.

-A-
Abraham
Alan
Alva
Art

-B-
Bob
Bradley

-C-
Cary
Cletus

Now, with the code i have I can get it working, but it only does it once, it wont loop until there are no more records.. So have a song starting with D and one starting with H and i am only getting the H?

here is the code i am using:


$result = mysql_query("SELECT * FROM lyrics ",$db);

if ($myrow = mysql_fetch_array($result)) {

// display list if there are records to display

$firstChar = "";
while($myrow = mysql_fetch_array($result)) {
if ($firstChar != $myrow["title"][0]){
$firstChar = $myrow["title"][0]; // get the first char in the name
echo "<p />-" . $firstChar . "-<br />\n";
}

printf("<a href=http://www.handclaireonline.com/lyrics/lyrics.php3?id=%s>%s</a><br>\n",$myrow["id"],$myrow["title"]);

} while ($myrow = mysql_fetch_array($result));

} else {

Ökii
08-03-2002, 04:44 PM
You could query the database and build an array from the results and then echo that

$query = "SELECT title, id FROM `tablename`";
$c = mysql_query($query);

while ($d = mysql_fetch_array($c)) {
$titlearray[][0] = $d['title'];
$titlearray[][1] = $d['id'];
}

sort($titlearray);

for($e=0;$e<count($titlearray);$e++) {
echo '<p />-'.ucfirst(substr($titlearray[$e][0],0,1)).'-<br />
';
echo '<a href=http://www.handclaireonline.com/lyrics/lyrics.php3?id='.$titlearray[$e][1].'>'.$titlearray[$e][0].'</a><br>
';
}

Fly typed so might parse badly.

Tidus
08-03-2002, 05:06 PM
$result = mysql_query("SELECT * FROM lyrics ",$db);

if ($myrow = mysql_fetch_array($result)) {

// display list if there are records to display

$firstChar = "";
while($myrow = mysql_fetch_array($result)) {
if ($firstChar != $myrow["title"][0]){
$firstChar = $myrow["title"][0]; // get the first char in the name
echo "<p />-" . $firstChar . "-<br />\n";
$firstChar = "";
}

printf("<a href=http://www.handclaireonline.com/lyrics/lyrics.php3?id=%s>%s</a><br>\n",$myrow["id"],$myrow["title"]);

} while ($myrow = mysql_fetch_array($result));

} else

This code is making it show up twice :

-H-
Hillary


-H-
Hello

and it still doesn't show the one starting with D?

Any more advice?

Tidus
08-03-2002, 05:10 PM
Ökii!

By using the code you gave this is what came out!

--



--



-H-
Hillary


-H-
Hello


--



-Z-
Zena

Ökii
08-04-2002, 01:46 PM
$tempfirst = ucfirst(substr($titlearray[$e][0],0,1));
if(!$firstchar || $firstchar!==$tempfirst) {
echo '<p />-'.$tempfirst.'-<br />
';
$firstchar = $tempfirst;
}

Like your original coding - an if(a=b) clause to test whether the first character has been previously used before echoing the -X- bit should make the output look better.

As to why the one with a D doesn't show - well you can see the actual script works so that seems to indict the D error lies elsewhere - have you checked the actual table records for D....?

Tidus
08-05-2002, 02:02 AM
Hey!

The script is working fine aside from these extra lines?


--

--

-H-
Half A Heart
-H-
Hello
--

-Z-
Zena

anybody got any idea's!


<?php



##connect

mysql_select_db("news",$db);

$query = "SELECT title, id FROM `lyrics`";
$c = mysql_query($query);

while ($d = mysql_fetch_array($c)) {
$titlearray[][0] = $d['title'];
$titlearray[][1] = $d['id'];
}

sort($titlearray);

for($e=0;$e<count($titlearray);$e++) {
echo '-'.ucfirst(substr($titlearray[$e][0],0,1)).'-<br />
';
echo '<a href=http://www.handclaireonline.com/lyrics/lyrics.php3?id='.$titlearray[$e][1].'>'.$titlearray[$e][0].'</a><br>
';
}
?>


Thanks