Jump to content

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


Recommended Posts

How do I make it so it lists 1 before 10? If the names are

 

Djasa 1: jaskja

Djasa 10: 8jd2s

 

I have many results which relate to each other in series so I would like it to me in numerical order starting with 1, 2, 3,...,10,11,12.

 

How can I do this?

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>
";
}
?>

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

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>
";
}
?>

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?

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`;");
?>

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?

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.

I quickly found out you cant have a decimal in a key. I made another field named 'sort' and I will set these up (hopefully i can use decimals in these) and sort it by this. It fixed my problem for the moment at least.

 

Thanks for everyones help!

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.