Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Sorting Alpha and Numeric
Old 08-12-2010, 03:10 PM Sorting Alpha and Numeric
Skilled Talker

Posts: 83
Trades: 0
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):

PHP Code:
<?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>
Attached Images
File Type: png #.png (21.0 KB, 3 views)
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-13-2010, 06:27 AM Re: Sorting Alpha and Numeric
Super Talker

Posts: 139
Name: John Davis
Trades: 0
Why you can't to use same method for numbers?
You using REGEXP in your query, let use [0-9] in case of numbers.
__________________
»
Please login or register to view this content. Registration is FREE
- Interactive maps for websites
»
Please login or register to view this content. Registration is FREE
for web developers
MapMaster is offline
Reply With Quote
View Public Profile Visit MapMaster's homepage!
 
Reply     « Reply to Sorting Alpha and Numeric
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.42041 seconds with 13 queries