Jump to content

[SOLVED] ORDER BY name ASC - but 10 is before 1 (please help)


helpmeplease2

Recommended Posts

This is confusing for me. Why doesn't this work?

 

<?php
$game=$_GET['game'];
$result = mysql_query("SELECT substring_index(VTitle,'',-1) AS ID FROM videos ORDER BY ID DESC")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {

echo "
  <tr>
      <td class=\"4\"><a href=\"http://www.gameanyone.com?p=video&game=$game&video=".$row['VTitle']."\">".$row['VTitle']."</a></td>
  </tr>
";
}
?>

Link to comment
Share on other sites

It doesn't work because you are sorting by a string and not a number. You can do:

SELECT CAST(substring_index(VTitle,'',-1) AS UNSIGNED) AS ID FROM videos ORDER BY ID DESC

or even:
SELECT ROUND(substring_index(VTitle,'',-1)) AS ID FROM videos ORDER BY ID DESC

Link to comment
Share on other sites

The only thing I can get to work is this, but if I were to add a new addition it would go to the end. I want it to be alphabetical and numerically sorted.

<?php
$game=$_GET['game'];
$result = mysql_query("SELECT * FROM videos WHERE GTitle = '$game' ORDER BY ID ASC")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {

echo "
  <tr>
      <td class=\"4\"><a href=\"http://www.gameanyone.com?p=video&game=$game&video=".$row['VTitle']."\">".$row['VTitle']."</a></td>
  </tr>
";
}
?>

Link to comment
Share on other sites

I want it to be alphabetical and numerically sorted.

 

I think we need to see a rational example of half a dozen or so of these 'names' and how they would appear if they were alphabetical and numerically sorted.

Link to comment
Share on other sites

Here is an example

 

Walkthrough Chapter 1: Point Insertion - Part 1
Walkthrough Chapter 1: Point Insertion - Part 2
Walkthrough Chapter 1: Point Insertion - Part 3
Walkthrough Chapter 1: Point Insertion - Part 4
Walkthrough Chapter 2: A Red Letter Day - Part 1
Walkthrough Chapter 2: A Red Letter Day - Part 2
Walkthrough Chapter 2: A Red Letter Day - Part 3
Walkthrough Chapter 3: Route Kanal - Part 1
Walkthrough Chapter 3: Route Kanal - Part 2

 

Ordering them by ID works fine, however If I were to go back and add a part 5 for chapter 1 then it would get messed up. How can I make it so it always sorts them this way no matter when they were added?

Link to comment
Share on other sites

Erm.. ok try

 

<?php
$sql = mysql_query("SELECT *, 
                        CASE WHEN ASCII(RIGHT(`VTitle`, 1)) > 47 AND ASCII(RIGHT(`VTitle`, 1)) < 58
                        THEN LPAD(CONCAT(`VTitle`,  '-'), 52, '0') 
                        ELSE LPAD(`VTitle`, 52, '0') END AS `vsort` 
                        FROM videos ORDER BY `vsort`;");
?>

Link to comment
Share on other sites

Extract the chapter and part number to create decimal values

 

Walkthrough Chapter 2: A Red Letter Day - Part 3

Walkthrough Chapter 3: Route Kanal - Part 1

Walkthrough Chapter 3: Route Kanal - Part 2

Walkthrough Chapter 30: Route Kanal - Part 2

 

would be

2.3

3.1

3.2

30.2

 

cast over to a float and sort as normal.... that would work, right?

Link to comment
Share on other sites

I'm now thinking it would be easier to use decimals in the ID field (if you can do that). Then I could sort the data by the ID field and have it then when I add a new entry I put in a decimal to get it where I want it. This just seems easier than all other suggestions, if it works.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.