Ordering SQL Results

Discussion in 'PHP' started by cro91, Oct 14, 2011.

  1. #1
    Hey guys,

    I have the following script that is searching my database for genres that are selected via check boxes. (Small portion of the script)



    
            $where = '';
        if (isset($_GET['submit'])) 
            {
                $sql = 'SELECT Anime_Name FROM Anime';
                if (isset($_GET['Action'])) 
                {
                    $where = ' WHERE Action=\'1\'';
                }
                if (isset($_GET['Adventure'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Adventure=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Adventure=\'1\'';
                    }
                }
                            $addquery = " ORDER BY Anime_Name";
                $query = mysql_query($sql.$where.$addquery);
                  $num_rows = mysql_num_rows($query);
                if($num_rows > 0) 
                {
                    while($row = mysql_fetch_assoc($query)) 
                    {
                        echo $row['Anime_Name']."</br>";
                    }
                }
                else if($num_rows < 1)
                {
                    echo "Seems like your search returned no results. Try eliminating a genre.";
                }
              }
    
    PHP:
    Currently that just displays one list of everything under Anime_Name (Like it should)

    What I am trying to accomplish is to get the already ordered results into something like this...

    
    ----- A -----AAB
    ABA
    ABB
    ABC
    
    
    ----- B -----
    BAA
    BBA
    BCB
    BCC
    Code (markup):
    Where all the results that start with the letter "A" are in a A column and all the results that start with "B" are in a B column and so on.

    What I can't figure out is how to do it I am fairly new to all this and would love to learn, not asking for you to write the code but giving some pointers to what could possibly give me the outcome I'm looking for would be awesome!

    Thanks


    Edit 1: Ok, I managed to get it to do what I am trying but now its adding a extra line between the letters

    Here's how I managed to get it to sort it by letter in case anyone in the future has this problem.

    
            $where = '';
        if (isset($_GET['submit'])) 
            {
                $sql = 'SELECT Anime_Name FROM Anime';
                if (isset($_GET['Action'])) 
                {
                    $where = ' WHERE Action=\'1\'';
                }
                if (isset($_GET['Adventure'])) 
                {
                    if(strlen($where) > 0) 
                    {
                       $where .= ' AND Adventure=\'1\'';
                    } 
                    else 
                    {
                       $where = ' WHERE Adventure=\'1\'';
                    }
                }
                            $letter_first = '';
                $letter_all = '';
                $addquery = " ORDER BY Anime_Name";
                $query = mysql_query($sql.$where.$addquery);
                  $num_rows = mysql_num_rows($query);
                if($num_rows > 0) 
                {
                    while($row = mysql_fetch_assoc($query)) 
                    {
                        // only if first letter is different from stored letter 
                        if (strtoupper(substr($row['Anime_Name'],0,1)) != $letter_first) { 
                            // store new first letter 
                            $letter_first = strtoupper(substr($row['Anime_Name'],0,1)); 
                            $letter_all .= '<a href="#'.$letter_first.'">'.$letter_first.'</a> | '; 
                            // and print it 
                            echo '<b>'.$letter_all.'</b><br />'; 
                        } 
                        // then print the name 
                        echo $letter_all;
                        echo $row['Anime_Name']."</br>";
                    }
                    unset($letter);
                }
                else if($num_rows < 1)
                {
                    echo "Seems like your search returned no results. Try eliminating a genre.";
                }
              }
    
    PHP:
    Output

    
    [B]A | [/B]
    A | Apple 
    A | Animal
    [B]A | B | [/B]
    A | B | Boy
    
    Code (markup):
    Anyone know how to remove the spaces between where it changes letter groups and where its displaying A and B on one option?

    Edit 2: Fixed and finished!

    The final code looks like this

    (Changed from the above section of code)
    $letter = '';			$addquery = " ORDER BY Anime_Name";
    			$query = mysql_query($sql.$where.$addquery);
      			$num_rows = mysql_num_rows($query);
    			if($num_rows > 0) 
    			{
    				while ($row = mysql_fetch_assoc($query)){ 
    					// only if first letter is different from stored letter 
    					if (strtoupper(substr($row['Anime_Name'],0,1)) != $letter) 
    					{ 
    						// store new first letter 
    						$letter = strtoupper(substr($row['Anime_Name'],0,1)); 
    						// and print it 
    						echo '<b>'.$letter.'</b><br />'; 
    					} 
    					// then print the name 
    					echo $row['Anime_Name'].'<br />'; 
    				} 
    Code (markup):
    Output:

    
    [B]A[/B]
    aBleach
    AellzBud
    [B]B[/B]
    Bleach
    
    Code (markup):
     
    Last edited: Oct 14, 2011
    cro91, Oct 14, 2011 IP
  2. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #2
    If I understand you correctly, remove this line to clear the 'A |', 'B |' entries:

    echo '<b>'.$letter_all.'</b><br />';

    As for the 'A | B |' string. It's like that as you're using .= to compile it which adds appends to whatever's in the variable already. Try removing the dot (.). i.e.

    $letter_all = '<a href="#'.$letter_first.'">'.$letter_first.'</a> | ';
     
    mfscripts, Oct 14, 2011 IP
  3. cro91

    cro91 Peon

    Messages:
    62
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ah that would make sense. The 'A |', 'B |' entries I am going to use as anchors so at some point I think I will use them, right now its just getting used as headers.
     
    cro91, Oct 14, 2011 IP