While() screwing over a page

He has: 296 posts

Joined: May 2002

Problem: I have a page (a-f.php) that _supposedly_ displays Album's begining with the letters a-f. It didn't work at first, then it started working, but I got this result. Not what I want.

PHP Snippet:

<?php
  $query
= mysql_query(\"SELECT albumtitle,id FROM `ata_albums`\");

  echo (\"    <CENTER><FONT CLASS=\\"
xxxlarge\\"><B>A</B></FONT></CENTER>\n\");
  echo (\"  </TD>\n\");
  echo (\"</TR>\n\");

 
$row = mysql_fetch_assoc($query);
   
$title = $row[\"albumtitle\"];
   
$id = $row[\"id\"];
   
   
$first_letter = substr($title, 0, 1);
   
    while (
$first_letter == 'A') {
      echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"    <FONT CLASS=\\"content\\">\n\");
      echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?id=$id\\">$title</A>\n\");
      echo (\"    </FONT>\n\");
      echo (\"  </TD>\n\");
      echo (\"</TR>\n\");
    }

  echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
 
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
 
echo (\"    <CENTER><FONT CLASS=\\"xxxlarge\\"><B>B</B></FONT></CENTER>\n\");
  echo (\"  </TD>\n\");
  echo (\"</TR>\n\");
 
    while (
$first_letter == 'B') {
      echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"    <FONT CLASS=\\"content\\">\n\");
      echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?id=$id\\">$title</A>\n\");
      echo (\"    </FONT>\n\");
      echo (\"  </TD>\n\");
      echo (\"</TR>\n\");
    }

  echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
 
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
 
echo (\"    <CENTER><FONT CLASS=\\"xxxlarge\\"><B>C</B></FONT></CENTER>\n\");
  echo (\"  </TD>\n\");
  echo (\"</TR>\n\");

    while (
$first_letter == 'C') {
      echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"    <FONT CLASS=\\"content\\">\n\");
      echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?aid=$id\\">$title</A>\n\");
      echo (\"    </FONT>\n\");
      echo (\"  </TD>\n\");
      echo (\"</TR>\n\");
    }

  echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
 
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
 
echo (\"    <CENTER><FONT CLASS=\\"xxxlarge\\"><B>D</B></FONT></CENTER>\n\");
  echo (\"  </TD>\n\");
  echo (\"</TR>\n\");

    while (
$first_letter == 'D') {
      echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"    <FONT CLASS=\\"content\\">\n\");
      echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?id=$id\\">$title</A>\n\");
      echo (\"    </FONT>\n\");
      echo (\"  </TD>\n\");
      echo (\"</TR>\n\");
    }

  echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
 
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
 
echo (\"    <CENTER><FONT CLASS=\\"xxxlarge\\"><B>E</B></FONT></CENTER>\n\");
  echo (\"  </TD>\n\");
  echo (\"</TR>\n\");

    while (
$first_letter == 'E') {
      echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"    <FONT CLASS=\\"content\\">\n\");
      echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?id=$id\\">$title</A>\n\");
      echo (\"    </FONT>\n\");
      echo (\"  </TD>\n\");
      echo (\"</TR>\n\");
    }

  echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
 
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
 
echo (\"    <CENTER><FONT CLASS=\\"xxxlarge\\"><B>F</B></FONT></CENTER>\n\");
  echo (\"  </TD>\n\");
  echo (\"</TR>\n\");

    while (
$first_letter == 'F') {
      echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
     
echo (\"    <FONT CLASS=\\"content\\">\n\");
      echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?id=$id\\">$title</A>\n\");
      echo (\"    </FONT>\n\");
      echo (\"  </TD>\n\");
      echo (\"</TR>\n\");
    }
?>

It's probably somethin' simple, but I'd like some help Smiling

[James Logsdon]

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

try this:

<?php
function Print_Letter($result, $letter) {
   
   
$letter = strtolower($letter);
   
    echo (\
"<TR BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
   
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
   
echo (\"    <CENTER><FONT CLASS=\\"xxxlarge\\"><B>\" . ucfirst($letter) . \"</B></FONT></CENTER>\n\");
    echo (\"  </TD>\n\");
    echo (\"</TR>\n\");
   
   
$row = mysql_fetch_assoc($result);
   
$title = $row[\"albumtitle\"];
   
$id = $row[\"id\"];
   
$first_letter = substr($title, 0, 1);
   
    while (strtolower(
$first_letter) == $letter) {
        echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
       
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
       
echo (\"    <FONT CLASS=\\"content\\">\n\");
        echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?id=$id\\">$title</A>\n\");
        echo (\"    </FONT>\n\");
        echo (\"  </TD>\n\");
        echo (\"</TR>\n\");
       
       
$row = mysql_fetch_assoc($result);
       
$title = $row[\"albumtitle\"];
       
$id = $row[\"id\"];
       
$first_letter = substr($title, 0, 1);
    }
}


$result = mysql_query(\"SELECT albumtitle,id FROM `ata_albums` ORDER BY albumtitle ASC\");

echo (\"<TABLE BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"2\\" CELLSPACING=\\"1\\" VALIGN=\\"TOP\\">\n\");

Print_Letter($result, 'A');
Print_Letter($result, 'B');
Print_Letter($result, 'C');
Print_Letter($result, 'D');
Print_Letter($result, 'E');
Print_Letter($result, 'F');

echo (\
"</TABLE>\n\");
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 296 posts

Joined: May 2002

Now I get no results:
http://audio.anitrade.net/mp3/a-f.php
at least before I was getting results, even if it wouldn't stop ><

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

<?php
$letters
= \"a,b,c,d,e,f\";
$split = explode(\",\", $letters);
foreach (
$split as $k => $val) {
$result = mysql_query(\"SELECT albumtitle,id FROM `ata_albums` WHERE (albumtitle) LIKE ('$val') ORDER BY albumtitle ASC \");
$row = mysql_fetch_assoc($result);
$title = $row[albumtitle];
$id = $row[id];

       echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
       
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
       
echo (\"    <FONT CLASS=\\"content\\">\n\");
        echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?id=$id\\">$title</A>\n\");
        echo (\"    </FONT>\n\");
        echo (\"  </TD>\n\");
        echo (\"</TR>\n\");

}
?>

Maybe try that?

Laughing out loud

He has: 296 posts

Joined: May 2002

Same, No results >< This is pissing me of soooo bad! I wanted to have this script done 2 nights ago!

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Oops!
My mistake on the code
Try this:

<?php
$letters
= \"a,b,c,d,e,f\";
$split = explode(\",\", $letters);
foreach (
$split as $k => $val) {
$result = mysql_query(\"SELECT albumtitle,id FROM `ata_albums` WHERE (albumtitle) LIKE ('$val%') ORDER BY albumtitle ASC \");
$row = mysql_fetch_assoc($result);
$title = $row[albumtitle];
$id = $row[id];

       echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
       
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
       
echo (\"    <FONT CLASS=\\"content\\">\n\");
        echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?id=$id\\">$title</A>\n\");
        echo (\"    </FONT>\n\");
        echo (\"  </TD>\n\");
        echo (\"</TR>\n\");

}
?>

Laughing out loud

He has: 296 posts

Joined: May 2002

Awesome! It works. Thanks nike_guy_man!

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

That method is less desired as it executes one query per letter and contains a WHERE clause.

I'm not sure why my script didn't work. But at any rate, I didn't do a desk check. Looking at it know I see that it would have omitted records in some circumstances.

I've not tested this... comments inset so you can follow my thought process.

<?php
echo (\"<TABLE BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"2\\" CELLSPACING=\\"1\\" VALIGN=\\"TOP\\">\n\");

// initiallize our array of letters
// (the NULL is needed as the first element is skipped in the loop below)
$letters = array(NULL, 'a', 'b', 'c', 'd', 'e', 'f');

// set reference $letter to the first letter in our $letters array
$letter =& $letters[0];

// initialize our protection counter
$i=0;

// get a recordset with our pre-sorted album titles
$result = mysql_query(\"SELECT albumtitle,id FROM `ata_albums` ORDER BY albumtitle ASC\");

// loop through the album list
while (
$row = mysql_fetch_assoc($result)) {
   
$title = $row[\"albumtitle\"];
   
$id = $row[\"id\"];
   
    // get the first letter of our album title
   
$first_letter = substr($title, 0, 1);
   
    // if the first letter of the album title doesn't match the letter we're on
    while (strtolower(
$first_letter)!=strtolower($letter)) {
        // if we have more letters to go through
        if (
$i<count($letters)) {
            // go to the next letter
           
$letter++;
           
$i++;
        }
        else {
            // we've already been through all of our letters
            // break out of two while loops
            break 2;
        }
       
        // print the heading for the next letter
        echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
       
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
       
echo (\"    <CENTER><FONT CLASS=\\"xxxlarge\\"><B>\" . ucfirst($letter) . \"</B></FONT></CENTER>\n\");
        echo (\"  </TD>\n\");
        echo (\"</TR>\n\");
    }
   
    // print the album title
    echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
   
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
   
echo (\"    <FONT CLASS=\\"content\\">\n\");
    echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?id=$id\\">$title</A>\n\");
    echo (\"    </FONT>\n\");
    echo (\"  </TD>\n\");
    echo (\"</TR>\n\");
   
}

echo (\"</TABLE>\n\");
?>

Mark Hensler
If there is no answer on Google, then there is no question.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I removed the reference...

<?php
echo (\"<TABLE BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"2\\" CELLSPACING=\\"1\\" VALIGN=\\"TOP\\">\n\");

// initiallize our array of letters
// (the NULL is needed as the first element is skipped in the loop below)
$letters = array(NULL, 'a', 'b', 'c', 'd', 'e', 'f');

// initialize our index counter
$i=0;

// get a recordset with our pre-sorted album titles
$result = mysql_query(\"SELECT albumtitle,id FROM `ata_albums` ORDER BY albumtitle ASC\");

// loop through the album list
while (
$row = mysql_fetch_assoc($result)) {
   
$title = $row[\"albumtitle\"];
   
$id = $row[\"id\"];
   
    // get the first letter of our album title
   
$first_letter = substr($title, 0, 1);
   
    // if the first letter of the album title doesn't match the letter we're on
    while (strtolower(
$first_letter)!=strtolower($letters[$i])) {
        // if we have more letters to go through
        if (
$i<count($letters)) {
            // go to the next letter
           
$i++;
        }
        else {
            // we've already been through all of our letters
            // break out of two while loops
            break 2;
        }
       
        // print the heading for the next letter
        echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
       
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
       
echo (\"    <CENTER><FONT CLASS=\\"xxxlarge\\"><B>\" . ucfirst($letters[$i]) . \"</B></FONT></CENTER>\n\");
        echo (\"  </TD>\n\");
        echo (\"</TR>\n\");
    }
   
    // print the album title
    echo (\"<TR BORDER=0 WIDTH=\\"
100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
   
echo (\"  <TD BORDER=0 WIDTH=\\"100%\\" HEIGHT=\\"100%\\" BGCOLOR=\\"#FFFFFF\\" CELLPADDING=\\"0\\" CELLSPACING=\\"0\\" VALIGN=\\"TOP\\">\n\");
   
echo (\"    <FONT CLASS=\\"content\\">\n\");
    echo (\"      <A HREF=\\"
$site[url]/mp3/details.php?id=$id\\">$title</A>\n\");
    echo (\"    </FONT>\n\");
    echo (\"  </TD>\n\");
    echo (\"</TR>\n\");
   
}

echo (\"</TABLE>\n\");
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 296 posts

Joined: May 2002

Awesome! That cut the size of the file down EMINSLY(sp?)! Thanks alot Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.