Sorting Alpha and Numeric

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Sorting Alpha and Numeric

Post by Smudly »

Hi,

I have a page that lists the names of the Artists in one column, and the title of their song in the next column. I've included a sort section that allows users to view the artist (with the corresponding title) that starts with whatever letter they press. I've now come to a point where I need to add in a check for numbers.
(View attached image to understand what we're talking about)

So if the artist name starts with any number between 0-9, it will be shown in order on the page once a user clicks on "#". I've tried doing it various ways, but can't seem to get it just right.
Here is the code (I took out all my attempts to get the numeric sort working):

Code: Select all

<?php
session_start();

include_once('inc/connect.php');


if (isset($_SESSION['username'])){
$loginstatus = "logout";
}
else{
$loginstatus = "login";
}
if(!isset($_SESSION['sort_counter']))
{$_SESSION['sort_counter'] = 1;}

if(($_SESSION['sort_counter']%2) == 0){ //test even value
  $sortcount = "DESC";
}else{ //odd value
  $sortcount = "";
}

$result = mysql_query("SELECT * FROM sheets ORDER BY artist");
$sheetscount = mysql_num_rows($result);
$sortletteris = $_GET['letter'];
$downloadclick = $_GET['downloadclick'];
$show = $_GET['show'];
$today = date("Y-m-d");

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="styles/style.css" />
</head>
<body bgcolor="#343331">

<!-- Header -->
<div id="header">
	<div id="headerleft"></div>
	<div id="headermiddle"><a href="index.php"><img src="img/logo.png"></a></div>
	<div id="headerright">
	
	</div>
	
</div>

<!-- Content Top -->
<div id="contenttop">
	<div id="links">
	
<!-- 92x30 -->
	</div>
</div>

<!-- Content Middle -->
<div id="contentmiddle">
<div id="content">
<div id="sort">
	<?php 
	echo "<center>".$sheetscount." Sheets Available<br />";
	echo "<a href='newlyadded.php'>New Sheets</a><span>&nbsp; | &nbsp;</span><a href='request.php'>Request a Sheet</a></center>";
	$letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	echo "<center><div id='letters'>";
	$i = 0;
	while ($i<26){
	$sortletter = $letters[$i];
	echo "<a href='index.php?letter=".$i."'>".$letters[$i]."&nbsp;</a>";
	$i += 1;
	}
	echo "&nbsp;<a href='index.php'>All</a></div></center>";

	if (($sortletteris)!=""){
		
		// The letter that was clicked is set to this variable
		$mysortletter = $letters[$sortletteris];
		//echo $mysortletter;
		// lname LIKE '$letter%'
		$result = mysql_query("SELECT * FROM sheets WHERE artist REGEXP '^[$mysortletter]' ORDER BY artist $sortcount"); 
		
	$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
	}
	
	elseif (($sortletteris)==""){
	
	$result = mysql_query("SELECT * FROM sheets ORDER BY artist $sortcount"); 
		
	$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
	
	}

	  $greenboxleft = "greenboxleft";
	  $greenboxright = "greenboxright";
	  $grayboxleft = "grayboxleft";
	  $grayboxright = "grayboxright";
	  $colorvalue = 0;
	  
	echo "<br /><table width='600px' align='center' style='border-collapse:separate;
border-spacing:0px;'><th style='background-color: #cccccc; border-bottom-style: solid; border-color: #6aa504;'>Artist</th><th style='background-color: #cccccc; border-bottom-style: solid; border-color: #6aa504;'>Title</th>";
	while($row = mysql_fetch_array($result))
	  {
	  if(($colorvalue%2)==0){
	  $styleleft = $greenboxleft;
	  $styleright = $greenboxright;
	  }
	  else{
	  $styleleft = $grayboxleft;
	  $styleright = $grayboxright;
	  }
	  
	  
	echo "<tr>";
	  echo "<td align='center' width='250' id='$styleleft'><div id='songsboxleft'>". ucwords($row['artist']). "</div></td>";
	  echo "<td align='center' width='250' id='$styleright'><div id='songsboxright'><a target='_blank' name='downloadclick' href='".$row['url']."'>" .ucwords($row['title']). "</a></div></td>";
	
	echo "</tr>";
	$colorvalue++;
	}
	echo "</table>";
	
	?>
	</div>


</div>

</div>

<!-- Content Bottom -->
<div id="contentbottom">

</div>

</body>
</html>
Attachments
#.png
#.png (21 KiB) Viewed 150 times
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Sorting Alpha and Numeric

Post by Weirdan »

Code: Select all

// $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$letters = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//........ skipped
        if (($sortletteris)!=""){
                
                // The letter that was clicked is set to this variable
                $mysortletter = $letters[$sortletteris];
                // handle the numbers
                if ($mysortletter == '#') $mysortletter = '0-9';  // <=================== added
                
                //echo $mysortletter;
                // lname LIKE '$letter%'
                $result = mysql_query("SELECT * FROM sheets WHERE artist REGEXP '^[$mysortletter]' ORDER BY artist $sortcount"); 
                
                $_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run
        }
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Re: Sorting Alpha and Numeric

Post by Smudly »

Thank you VERY MUCH!! It's working.

One side note however. if I try to refresh the page, the sort is switched as if i clicked a litter again to swap the order the artists are shown. How can I modify this code to only change order on click instead of on refresh of domain?

Thanks :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Sorting Alpha and Numeric

Post by Weirdan »

You can't do that (easily), because server cannot distinguish between click and reload (both are just requests to it).
Post Reply