Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




How Can I Tell Which Way My Database Is Sorting?


Ive got some mysql results,.. and I have wrote a simple script to sort them by column headers. Little link and url var to tell the sql which way to sort. My problem is how to I go about assigning ASC or DESC? Now that seemed real simple to me,. but then the question came up. How is it sorted in the first place?

If I could find out how it is sorted then it would be fairly simple to do an if statement to change the var in the ORDER BY clause. Anyone know how to tell which way the results are sorted to begin with?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Sorting Variables From Database
I have a function listed below, right now it sorts by $member_id I believe. I desire the same output ie.. member names, but I would like to sort it alphabetically by name. Is that possible? PHP Code:

Sorting Database Query Results
I want to know if it is possible to sort query results so that the records with a value in a certain field is displayed first and the records with the field empty thereafter.

Sorting...
I was trying to sort a multi-dimensional array with one dimension being the sorting criteria. I wanted to make it sort by an alternative one if the main one is equal on two entries.

However that doesn't work: it sorts properly according to the main criteria, but it won't sort according to the alternative one. I hope you understood what I'm trying to say, since I'm not a native English speaker. Here is my code:

Sorting By Word?
Okay, I am making a member database, and when I print it out I want it to display the list by rank. Unfortantly, I stored Rank as a word. I could make a sort function that would be quite extensive and probably take a long time to execute every time...


..Or, is there anyway I can specify in SQL: Put this on the top of the list if "rank='General'" and then next if "rank='Captain'", and so on? I think if this was possible it would take a lot less time to execute...

Or else I will probably have to redesign much of the site with numerical ranks instead. Thanks for any answers/advice.

Sorting Options
I have an array I would like to sort, but the sorting options that I have found are inadiquate. I need to do a case insensitive sort, that does not maintain the index correlation. My problem is I have some items in my array beginning with "a" and some with "A", and I want the sorting based strictly on the letter not the case, and then I have to be able to reference them in an order that maintains the sort.

Sorting Data
Im displaying data from a mysql query that displays a Player, email, id etc.. and I have links that sort the data by player, email, id etc... But Ive seen websites where you click sort and it will sort the data by that and if you click it again it will sort that data this time in descending order.

How am I able to have link that will sort the data normally and descending order?

Sorting Strings!
Hi

I need to read a .txt file and sort the value, highest firs, lowest last.

The .txt file contains the following info on a different row:
altavista:joe:5
yahoo:eric:2
disney:dis:4

the firs thing i did was reading the first line and split the code by ":" so that
echo $referrer[0] //would give altavista
echo $referrer[1] //would give joe
echo $referrer[2] //would give 5

I want to sort these lines on the numbers(highest first, lowest last) and print this in a table. How can i do this?
so what i would like in a table is this

<table>
<tr><td>
altavista</td><td>5</td></tr>
<tr><td>
disney</td><td>4</td></tr>
<tr><td>
yahoo</td><td>3</td></tr>
</table>


Is this possible anyway?

Sorting An Array
I'm trying to sort a listing of albums alphabetically for LyricsList.com, the problem is that I want the initial 'The' and 'A' in some albums (A Hard Days Night, The Wall) to be ignored. So that the list will look like:

Abbey Road
Dark Side Of the Moon
The Final Cut
A Hard Days Night
Kill 'em All
The Wall

ABC order, 'A' and 'The' negated. The info comes from a database, in three calls:

// call albums that begin with letter $letter
$query = "SELECT * FROM albums WHERE album LIKE '$letter%' ORDER BY album";
$result = mysql_query($query) or die(mysql_error());

// call albums that begin with letter A
$query_a = "SELECT * FROM albums WHERE album LIKE 'a' ORDER BY album";
$result_a = mysql_query($query_a) or die(mysql_error());

// call albums that begin with letter T
$query_t = "SELECT * FROM albums WHERE album LIKE 't' ORDER BY album";
$result_t = mysql_query($query_t) or die(mysql_error());

So I need to get just the albums from $result_t that begin with 'The ' (easy regex) and from $result_a that begin with 'A ' (easy regex), but puting the three of them in order is proving beyond my skill.

Thanks in advance for any advise.

Sorting Algorithm In Php
the sort() function in php, what kind of sorting algorithm is it? selection sort? bubble sort? insertion sort? if i were to create my own sort, which would be fastest for sorting many string values?

Number Sorting
I have a database from which I extract the top ten rows, dependent on a certain value. However, when two entrys have the same value they get the same placement in the "top ten" thingie. I know I can sort them first by position, then by for example name, but that doesn't do it for me.

(1) Printing the values gets me;

1. Malmö
2. Limhamn
3. Jönköping
4. Stockholm
4. Västerås
5. Sjöbo
5. Södertälje
6. Väsby
6. Ystad
6. Örebro

(2) The problem is that it SHOULD say;

1. Malmö
2. Limhamn
3. Jönköping
4. Stockholm
4. Västerås
6. Sjöbo
6. Södertälje
8. Väsby
8. Ystad
8. Örebro

You see the difference? In (1) "5. Sjöbo" should actually be "6. Sjöbo", since it's the sixth value, cause there are two cities sharing the number 4 spot.

The code I'm using;
Code:
while($row = mysql_fetch_object($result)) {
if ($prev_value == $row->name) $i=$i-1;
echo $i.". ".$row->city_name."
";
$prev_value = $row->name;
$i++;
}

Sorting By Number
What I am looking for is a way to sort either a few variables or an array.

i.e I have an array
$myarray[1] = 12
$myarray[2] = 76
$myarray[3] = 3
$myarray[4] = 27
$myarray[5] = 52

Now I want to be able to sort the array by the value of it, so it would come out as.

52
76
27
12
3
Anyideas how to do this?

Data Sorting
I have a database table that has these fields: id (auto increment), date, message and what I wanted to do is sort the messages in order starting from the latest messages. I then wanted to sort the messages and only show 5 messages per page. I managed to do all this but now I also want all the messages with the SAME DATE (but different times) to be shown in the same table. for example: Code:

Order Sorting
I have been trying to search a script on how to start making a Order Sorting on field or items on db. Do not know on how to start it.. It's already making me nuts.. I have this excel format that i want to converted on php to be accessible on a webpage. that act like an excel.

something like with a item look up on a database with a specific criteria.

Item Search [text box]
Old [button]
New [button]
Hot realease [button]

........

Array Sorting
i have to retrieve some info from tables and then add all the fields together, then list out. 10 records per time, then got 2nd page, 3rd page..so on...

my problem is ..i can't get my records to sort(highest point to lowest point), then i decided to use array. PHP Code:

Sorting Csv File
I'm trying to sort a csv file on a specific field, but am having
trouble figuring out where to start. Any ideas?

Sorting Problem
I want to make categories on a website, like folders that have a tree to
them...

I was wondering if the best way is to have it in an array and have them all
point to each other and sort them with some sort of algorithm.

Is this the best way to do it?

Sorting Numbers
I'm trying to sorting a file in a directory.
It works well but it sorts me the number in this way:

1
10
11
2
20
21
....

is there a way to sort in this way?
1
2
3
...
10
11
12

this is my code:

<?php
if ($handle = opendir('/home/davide/')) {
$files = array();
while (false !== ($file = readdir($handle))) {
if (($file != "." && $file != "..") AND (eregi($select,$file)))
$files[] = $file;
}
closedir($handle);
sort($files);
foreach($files as $file) {
$ext = substr(strrchr($file, "."), 1);
if ($ext == 'txt') {
$idx = str_replace('.txt', '', $file);
echo "$idx";
}
}
}
}
?>

Sorting Information
I have a script that completes a mysql query and displays all the information & all works fine

The problem im having is that i only want it to display the information i tell it too ie...

there are 3 people with an age of 40 and the rest with various other ages, can someone point me in the right direction to get the script to display only those who are 40 & ignore the rest...

Sorting Data Help
I know this should be fairly easy, but I am still very new at this. Ok, I am trying to pull data from my database and sort it. I want it to sort it by using field one sorting alphabetically, but only pulling up results from field two that has a specific name. I am not explaining this well sorry. I'll give you the code that I am trying and it's not working because I know I am not doing it right.

$result = mysql_query("select * from items where itemName Like'A%' and WHERE field2='value' order by itemName asc");

Sorting An Array
I have a performed a fairly simple calculation on a mysql table which has returned a new set of values the only problem I have is that I want to sort them using this new value.
PHP Code:

Sorting Array?
I have an array that has 3 keys and I want to sort the array by the values in the key called "key3" so which sort function do I use? array("key1"=>"hey", "key2"=>"hello", "key3"=>"hi")

Sorting XML Results
The following code parses an XML file and displays the results. The function resultSort is meant to sort the results by price from highest to lowest. Unfortunately, it's not sorting correctly in all cases. I think this may be due to the prices being in euros. Right now, this is how it outputs the results:


1 208,00
2 148,00
412,00
638,00
942,00

This is how it should look:

2 148,00
1 208,00
942,00
638,00
412,00

Here's the code: PHP Code:

Not Alpha Sorting!
A couple of quick questions! Does this count as one query, as I have managed to reduce my queries from 13 to 1? PHP Code:

Sorting Data
I want my web site to be as dynamic as possible (hence the reason for trying to master php) but the knowledge I have at the moment will not quite stretch to doing the following:

I want to have the height attribute of an image to be controlled by data in a database table. The table will hold the numeric test result that pupils score when they take a test on my web based tutorial. The image is a column, the larger the score, the larger the column. I would like to know: Code:

Sorting Variables
I have built a database of basketball statistics. I am only storing the most NECESSARY information. Example, I am not storing "points scored" because points scored = (field goals made * 2) + three-pointers made + free throws made.

After doing a select query to get all of the data from the table, I create a variable called $ppg for points per game [((field goals made * 2) + three-pointers made + free throws made) / games]. I use "( $row = mysql_fetch_array($result))" and "extract($row);" to put the data into variables. The problem is I can't figure out how to sort by a variable.

In order to make my top 10 scorers list, I have to "cheat" by ordering by "((field goals made * 2) + three-pointers made + free throws made) / games" in my sql query. I want to be able to order the data by the variable $ppg. Code:

Sorting Results
I hava a table with a primary key cod_topico and I'd sort my results.
I do this, ok?
$query='SELECT * from topico order by cod_topico asc' ;
But I would not sorting asc, How I sort a non-asc form? What is the oposite of asc?

Listing/sorting
I want to print out only if the site is in the database. this works for the links,
however, if the there is no site under that letter I still have the output of the letter.
if I do it under the second example I have a letter for each link. How can I do this? Code:

Need Help Sorting A XML File
I have a similar xml file and I redisplay it with the following array in
PHP. What I would like to do is sort the <idfield before it is
redisplayed. Looking for a sample code.

<rss>
<item>
<id>3</id>
<name>Brian</name>
</item>
<item>
<id>1</id>
<name>James</name>
</item>
<item>
<id>5</id>
<name>Mark</name>
</item>
<item>
<id>2</id>
<name>Keith</name>
</item>
<item>
<id>4</id>
<name>Derek</name>
</item>
</rss>
.............

Query Sorting
How would I have a query return only the top 3 or so entries based on a certain fields highest value? example,

entry 1 = 32
entry 2 = 21
entry 3 = 18
entry 4 = 41
entry 5 = 39
entry 6 = 22
entry 7 = 36

I would only want entries 4,5 and 7 to be returned. The top three out of the seven entries.

Sorting Files
i have a small little problem - i am looking for some sort of script, that will allow me to sort out files into order, determined by their filename, eg. 30-04-2005.php - would be a file that has todays date - i use dates for filenames, as events happen on these days, and its easier to find them.

Files are currently automatically sorted after being created, into a folder, and subfolder, depending on what the filename date is, for example, 30-04-2005.php would go in the folder ./2005/04/ - whereas 05-06-2004.php would go in ./2004/06/.

The sorter I am looking for, will sort all the files that lie in the folder, into order, after they have been created, onto a list where each link goes to the particular file, preferably, with the name of the month at the top of the monthly list, the files that belong into the month, in date order (ie. 1st april, would be above 10th april) - and the list contains all the months for the year, in month order (ie. jan-dec), and the file with the list in, lies in the year folder, so it can be accessed easily.

One more small thing - for the current year, the files are all in reverse order, so that the latest file is at the top - but again, sorted into months, but going down from dec-jan....

Dat And Time Sorting
I'm trying to program a small news system... I have DATE and TIME separated and they are in DATE and TIME format, not varchar or anything. How can I sort this news the way that the very top of the news is the last news posted. Cos if I put a SORT BY newsdate, newstime DESC. or by

SORT BY newsdate DESC I only get an order by day but the last news is at the bottom. Also... how can I change with DATE_FORMAT the format of my DATE cos I have it in YYYY-MM-DD format and I've tried many ways to change it to DD-MM-YYYY but I can't find the way out.

Sorting Doesn't Sort?
I've got an array of arrays that I need to sort on preferably one of the
indices of the nested array, keeping the order of the outside array. The
structure is like this:

array([i] => array(key["name"] => data)
and what I need to do is to sort the nested array alphabetically according
to key["name"].
I can either use the name property of the nested array as index for the
outside array (then I need to keep the relationship between the outside
array and the nested array, or use a numeric index thus making the
relationship between the outside and nested array irrellevant.) What I need
to do in any case is to sort the nested array. I've tried using
arsort($array, SORT_STRING) but for some reason this doesn't sort the array
the way that I expected.

A var_dump of one element of the outside array looks like this:
array(2)
{ [1]=> array(2)
{ ["id"]=> string(1) "1" ["name"]=> string(6) "JustforShow" }
{ [2]=> array(2)
{ ["id"]=> string(1) "1" ["name"]=> string(6) "Testing" }

etc.

Sorting Folders
I have a site with photo-albums on it.
Each directory represents an album.
What i want is on the "home page" of the site a feature
which says "newest album: *******"

So i have to search the album directory, and then sort them
by date. And then it automatically has to grab the newest album.

Maybe a slight problem is that the albums are divided over 4
subcategories. (Match, players etc.)

Match: - Match 1
- Match 2
- Match 3

Players: - player 1
etc...

Albums would be 'Match 1', 'Match 2', etc.

hope ypu understand.

Sorting Arrays
having some trouble understanding what is happening with some array
sorting functions. In all cases, my compare function is:
function compare($x, $y)
{
if ( $x[1] == $y[1] )
return 0;
else if ( $x[1] < $y[1] )
return -1;
else
return 1;
}
I started with this:
$contents = array( array( 'TIR', 'Tires', 100 ),
array( 'OIL', 'Oil', 10 ),
array( 'SPK', 'Spark Plugs', 4 ) );
echo '<br>XXX<br>'
foreach($contents as $r) {
var_export($r);
echo '<br>'
}
echo '<br>YYY<br>'
uasort($contents, 'compare');
foreach($contents as $r) {
var_export($r);
echo '<br>'
}
And the output, whether I use usort or uasort is always:
XXX
array ( 0 => 'TIR', 1 => 'Tires', 2 => 100, )
array ( 0 => 'OIL', 1 => 'Oil', 2 => 10, )
array ( 0 => 'SPK', 1 => 'Spark Plugs', 2 => 4, )

YYY
array ( 0 => 'OIL', 1 => 'Oil', 2 => 10, )
array ( 0 => 'SPK', 1 => 'Spark Plugs', 2 => 4, )
array ( 0 => 'TIR', 1 => 'Tires', 2 => 100, )
But if I use uksort I get:
XXX
array ( 0 => 'TIR', 1 => 'Tires', 2 => 100, )
array ( 0 => 'OIL', 1 => 'Oil', 2 => 10, )
array ( 0 => 'SPK', 1 => 'Spark Plugs', 2 => 4, )

YYY
array ( 0 => 'SPK', 1 => 'Spark Plugs', 2 => 4, )
array ( 0 => 'OIL', 1 => 'Oil', 2 => 10, )
array ( 0 => 'TIR', 1 => 'Tires', 2 => 100, )

But I cannot see why I get this reversal of order???

I then tried:
$contents = array( array( c=>'TIR', d=>'Tires', p=>100 ),
array( c=>'OIL', d=>'Oil', p=>10 ),
array( c=>'SPK', d=>'Spark Plugs', p=>4 ) );

MySQL Sorting
I'd like to do is divide results from a MySQL query over multiple pages, each holding a certain number of results. Now I have a PHP script that uses the MySQL command LIMIT in the query to get # results at a time (which also means not all the results are returned every time, just a few, obviously this is a good thing for speed/load so I wanna keep it that way).

The MySQL manual states that if you use LIMIT # with ORDER BY, MySQL will end the sorting as soon as it has found the first # lines Ok I could've posted this on the MySQL forum first but actually I was wondering whether someone has a solution in PHP for this?

Sorting Results
What I need to do is from the query I send sort the results by ID numbers from highest to lowest, because the highest number is the most recent news post I would like this to be printed first and then the lower the ID number the further down the post goes. Am I making sense? PHP Code:

Sorting A 3d Array
I am trying to sort the display url subarray and it works, however, the other arrays stay in there place which throws the whole thing off. How do I sort by displayurl and make the other sub arrays move with it? PHP Code:

Sorting Dates
i have a db full of events and i need to display the next event on a page. is there any way i can sort the dates after i have got them from the db or do i need to sort them in the sql query and how would i do it?

Sorting By Date
im trying to sort a fixtures list by date! But obviously it just sorts it by first numbers for example it orders the list by day value and not day and month, i found this code in a previous post :

SELECT DATE_FORMAT(date_field, '%W %M %Y')FROM table_name

Trouble is i dont really understand. How do i implement that if i want to select all from the table and order it by the date???

Sorting An Array
im going to loop through 17 thousand mysql entries (each entry has aunique, and incremental, id) then calculate a rating of each entry based on some other information. (between 1 and 5). I then want to sort the data with the highest rated entries at the top of the list?

my first plan was to add each calculation into an array, as the array...

Column Sorting
I have found the below code to sort columns, but would like to hide it if another column is sorted as I have the following columns: username , age , online etc... so if online is sorted there is no text saying Sort ascending or Sory descending in username or age. Code:

Sorting After Query
I have a db with intergers to hold "kills" & "deaths". This is for a game,.. now I have pulled my query ,. done my math,. and came to the number I want. Question is how would I sort it by the lowest number at this point? I need it sorted by "$tt" Code:

Sorting An Array..
i've have the following array:

files['name'][1]
['size'][1]
['time'][1]
files['name'][2]
['size'][2]
['mtime'][2]
...etc.

how do I sort this array according to "time" index? so that, for
example, files['name'][0] would give "my_file.txt" that was last
created?

Sorting By Ip Addresses
I am trying to sort some information from the database by ip address but for obvious reasons it does not sort the ips in a propper order for example the ips get listed like this: Code:

Sorting Tables
I have the following script which first gets a list of users out of one table, it then uses this list to get the stats for each user out of another table I would like to know how I can sort the table by $cmb DESC (or even let the user decide how to sort it via a dropdown menu but this would affect $rank?).

I dont think i can use sql as there is 26 entries per user in the stats table. also is there an easier way of doing this? I always seem to do everything the long way. Code:

Handle Sorting
Right now i'm trying to figure out a good way to have a single php file handle many different types of sorting.

For example, sometimes from _GET the page will display by category and by user. Other times, it may just be by user, or other times when there is no _GET, it displays everything.

The thing I can't think of is how the query can handle these multiple WHERE clauses, even when the clauses may not exist at certain times the page is pulled up.

I tried something like the following: WHERE $category, $user. However, when $user is null, it breaks because of the extra comma. If the explanation is long, can someone point me to a good resource on doing this?

Sorting An Array
I've got a small problem. I have an array that looks something like this:$smthin = array (

"character1" => 0,
"character2" => 0,
"character3" => 0,
"character4" => 0,
"character5" => 0);

I add 1 to the value every little while. Then it may look like this:$smthin = array (

"character1" => 1,
"character2" => 6,
"character3" => 0,
"character4" => 1,
"character5" => 2);

And at this point I'd like to sort the array to look like following:$smthin = array (

"character2" => 6,
"character5" => 2,
"character1" => 1,
"character4" => 1,
"character3" => 0);

I don't think sort() function will help me with this. That would ruin the keys.

Sorting Arrays
How would I go about sorting the following array?

$t= array();
$t[] = array( 'text 2', 1, 2 );
$t[] = array( 'text 4', 2, 1 );
$t[] = array( 'text 1', 1, 1 );
$t[] = array( 'text 5', 2, 2 );
$t[] = array( 'text 3', 1, 3 );

// sort and get:

$t[] = array( 'text 1', 1, 1 );
$t[] = array( 'text 2', 1, 2 );
$t[] = array( 'text 3', 1, 3 );
$t[] = array( 'text 4', 2, 1 );
$t[] = array( 'text 5', 2, 2 );

I guess this is like an SQL ORDER BY col1, col2 Anyone know if there is an easy way to do this in PHP?

Sorting In A Mysql_query
How do i say to the script that it only should loop 2 times when there is 20 rows?

If you look at the code then im asking it to loop everytime it finds a player with a specific id. It works fine, it loops 20 times but i only want it to loop when season_ID equal to a specific year.

I could write season_ID = '2006', but that wouldnt work because then i dont get it to loop when the year is 2005 or 2004. Code:


Copyright © 2005-08 www.BigResource.com, All rights reserved