Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Another question, alphabetical

    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:

    PHP Code:
    $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 { 

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could query the database and build an array from the results and then echo that

    PHP Code:
    $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.
    Last edited by Ökii; 08-03-2002 at 03:47 PM.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #3
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts
    PHP Code:
    $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?

  • #4
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ökii!

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

    --



    --



    -H-
    Hillary


    -H-
    Hello


    --



    -Z-
    Zena

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $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....?
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #6
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •