473,385 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Array sorting question

Hello,

I have mysql table:

+----+---------+
| 1 | value1 |
+----+---------+
| 2 | value2 |
+----+---------+
| 3 | value3 |
+----+---------+
| 4 | value4 |
+----+---------+

What is the best way to sort results from:

$query = mysql_query("SELECT * FROM table");

into array like this:

array(
1 => 'value1',
2 => 'value2',
3 => 'value3',
4 => 'value4'));

TNX
Jul 17 '05 #1
13 1814
> What is the best way to sort results from:
$query = mysql_query("SELECT * FROM table");
into array like this:


Add an ORDER BY clause to your SELECT statement. You didn't say what
the names of your columns are, but it'll look something like this:

SELECT * FROM table ORDER BY column2
Jul 17 '05 #2
dr. zoidberg wrote:
Hello,

I have mysql table:

+----+---------+
| 1 | value1 |
+----+---------+
| 2 | value2 |
+----+---------+
| 3 | value3 |
+----+---------+
| 4 | value4 |
+----+---------+

What is the best way to sort results from:

$query = mysql_query("SELECT * FROM table");

into array like this:

array(
1 => 'value1',
2 => 'value2',
3 => 'value3',
4 => 'value4'));
maybe I didn't ask correctly. Sorry for my bad english. If I do a
regular query array is:
array(
0 => 'value1',
1 => 'value2',
2 => 'value3',
3 => 'value4'));


but I need to have field ID as array key and the other field as array value.
Jul 17 '05 #3

"dr. zoidberg" <zo*@example.org> wrote in message
news:c5*************@ID-93631.news.uni-berlin.de...
dr. zoidberg wrote:
Hello,

I have mysql table:

+----+---------+
| 1 | value1 |
+----+---------+
| 2 | value2 |
+----+---------+
| 3 | value3 |
+----+---------+
| 4 | value4 |
+----+---------+

What is the best way to sort results from:

$query = mysql_query("SELECT * FROM table");

into array like this:

array(
1 => 'value1',
2 => 'value2',
3 => 'value3',
4 => 'value4'));


maybe I didn't ask correctly. Sorry for my bad english. If I do a
regular query array is:
> array(
> 0 => 'value1',
> 1 => 'value2',
> 2 => 'value3',
> 3 => 'value4'));


but I need to have field ID as array key and the other field as array

value.

You want to use the id field (I assume that's the first column in your
table) as the array key for a hash table? I don't think there's much you can
do beyond creating the array manually in your array_fetch loop:

$sql="select id,value from mytable";
$result=mysql_query($sql,$dbhandle) or die("Bad SQL! $sql");
$r=array();
$hash=array();
for($i=0;$i < mysql_num_rows($result);++$i) {
$r=mysql_fetch_array($result);
$hash[$r['id']] => $r['value'];
}

HTH
Garp
Jul 17 '05 #4
In article <yD*******************@news-text.cableinet.net>,
"Garp" <ga***@no7.blueyonder.co.uk> wrote:
You want to use the id field (I assume that's the first column in your
table) as the array key for a hash table? I don't think there's much you can
do beyond creating the array manually in your array_fetch loop:

$sql="select id,value from mytable";
$result=mysql_query($sql,$dbhandle) or die("Bad SQL! $sql");
$r=array();
$hash=array();
for($i=0;$i < mysql_num_rows($result);++$i) {
$r=mysql_fetch_array($result);
$hash[$r['id']] => $r['value'];
}


Even simpler:

$sql="select id,value from mytable";
$result=mysql_query($sql,$dbhandle) or die("Bad SQL! $sql");
$hash = array();
while($r = mysql_fetch_assoc($result)) {
$hash[$r['id']] = $r['value'];
}

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #5
In article <c5*************@ID-93631.news.uni-berlin.de>,
"dr. zoidberg" <zo*@example.org> wrote:
Hello,

I have mysql table:

+----+---------+
| 1 | value1 |
+----+---------+
| 2 | value2 |
+----+---------+
| 3 | value3 |
+----+---------+
| 4 | value4 |
+----+---------+

What is the best way to sort results from:

$query = mysql_query("SELECT * FROM table");

into array like this:

array(
1 => 'value1',
2 => 'value2',
3 => 'value3',
4 => 'value4'));


$q=mysql_query("select value from table") or print mysql_error();
while ($r=mysql_fetch_array($q)){
$nr++;
$array[$nr] = $r["value"];
}

--
Sandman[.net]
Jul 17 '05 #6
In article <c5*************@ID-93631.news.uni-berlin.de>,
"dr. zoidberg" <zo*@example.org> wrote:
dr. zoidberg wrote:
Hello,

I have mysql table:

+----+---------+
| 1 | value1 |
+----+---------+
| 2 | value2 |
+----+---------+
| 3 | value3 |
+----+---------+
| 4 | value4 |
+----+---------+

What is the best way to sort results from:

$query = mysql_query("SELECT * FROM table");

into array like this:

array(
1 => 'value1',
2 => 'value2',
3 => 'value3',
4 => 'value4'));


maybe I didn't ask correctly. Sorry for my bad english. If I do a
regular query array is:
> array(
> 0 => 'value1',
> 1 => 'value2',
> 2 => 'value3',
> 3 => 'value4'));


but I need to have field ID as array key and the other field as array value.


$q=mysql_query("select id, value from table") or print mysql_error();
while ($r=mysql_fetch_array($q)){
$array[$r["id"]] = $r["value"];
}

--
Sandman[.net]
Jul 17 '05 #7

"Jan Pieter Kunst" <de*****@cauce.org> wrote in message
news:de***************************@news1.news.xs4a ll.nl...
In article <yD*******************@news-text.cableinet.net>,
"Garp" <ga***@no7.blueyonder.co.uk> wrote:
You want to use the id field (I assume that's the first column in your
table) as the array key for a hash table? I don't think there's much you can do beyond creating the array manually in your array_fetch loop:

$sql="select id,value from mytable";
$result=mysql_query($sql,$dbhandle) or die("Bad SQL! $sql");
$r=array();
$hash=array();
for($i=0;$i < mysql_num_rows($result);++$i) {
$r=mysql_fetch_array($result);
$hash[$r['id']] => $r['value'];
}


Even simpler:

$sql="select id,value from mytable";
$result=mysql_query($sql,$dbhandle) or die("Bad SQL! $sql");
$hash = array();
while($r = mysql_fetch_assoc($result)) {
$hash[$r['id']] = $r['value'];
}

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.


Stop simplifying my code!!

Garp
Jul 17 '05 #8
In article <b1*******************@news-text.cableinet.net>,
"Garp" <ga***@no7.blueyonder.co.uk> wrote:
Stop simplifying my code!!


OK then, I'll start simplifying my own code. With PEAR::DB, you can do
this to get a database result in an associative array in one go:

$hash = $database_object->getAssoc($query);

getAssoc() gives you an associative array with the first column as the
key. Very handy.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #9

"Jan Pieter Kunst" <de*****@cauce.org> wrote in message
news:de***************************@news1.news.xs4a ll.nl...
In article <b1*******************@news-text.cableinet.net>,
"Garp" <ga***@no7.blueyonder.co.uk> wrote:
Stop simplifying my code!!


OK then, I'll start simplifying my own code. With PEAR::DB, you can do
this to get a database result in an associative array in one go:

$hash = $database_object->getAssoc($query);

getAssoc() gives you an associative array with the first column as the
key. Very handy.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.


Gotta love PEAR.

Garp
Jul 17 '05 #10
Jan Pieter Kunst wrote:
In article <b1*******************@news-text.cableinet.net>,
"Garp" <ga***@no7.blueyonder.co.uk> wrote:

Stop simplifying my code!!

OK then, I'll start simplifying my own code. With PEAR::DB, you can do
this to get a database result in an associative array in one go:

$hash = $database_object->getAssoc($query);

getAssoc() gives you an associative array with the first column as the
key. Very handy.


wow, I didn't know this. Thanks.
Jul 17 '05 #11
In article <Js*******************@news-text.cableinet.net>,
"Garp" <ga***@no7.blueyonder.co.uk> wrote:
Gotta love PEAR.


Indeed! I especially love centralized error handling. You just put this
in a config file:

PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'pear_error_handler');

You define your own pear_error_handler function:

function pear_error_handler($error_object) {
do_something_with($error_object);
}

.... and you can remove all "if error, do something" or "or die" stuff
from your database access code. Much cleaner and more readable code all
around.

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #12
Sandman schrieb:
What is the best way to sort results from:

$query = mysql_query("SELECT * FROM table");

into array like this:

array(
1 => 'value1',
2 => 'value2',
3 => 'value3',
4 => 'value4'));

$q=mysql_query("select value from table") or print mysql_error();
while ($r=mysql_fetch_array($q)){
$nr++;
$array[$nr] = $r["value"];
}


Doing this will bring you into trouble when you delete some entries from
the table during life time.

To avoid this you would use

$q=mysql_query("select id, value from table order by id") or print
mysql_error();
while ($r=mysql_fetch_array($q)){
$array[$r["id"]] = $r["value"];
}

Frank

--
http://www.pncommerce.de
Jul 17 '05 #13
In article <40*********************@news.freenet.de>,
Frank Schummertz <fr**************@landseer-stuttgart.de> wrote:
Sandman schrieb:
What is the best way to sort results from:

$query = mysql_query("SELECT * FROM table");

into array like this:

array(
1 => 'value1',
2 => 'value2',
3 => 'value3',
4 => 'value4'));

$q=mysql_query("select value from table") or print mysql_error();
while ($r=mysql_fetch_array($q)){
$nr++;
$array[$nr] = $r["value"];
}


Doing this will bring you into trouble when you delete some entries from
the table during life time.


Yes, my second post to him, after he clarified what he wanted to do, did what
you do below.
To avoid this you would use

$q=mysql_query("select id, value from table order by id") or print
mysql_error();
while ($r=mysql_fetch_array($q)){
$array[$r["id"]] = $r["value"];
}

Frank


--
Sandman[.net]
Jul 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Radhika Sambamurti | last post by:
Hi, I'm a relative newbie.... I've written a program to do a bubble sort. take in numbers and sort them. My question is: at the very end of the sort() function, when I have to output the result...
5
by: trs | last post by:
thanks for reading this. problem: logical error is preventing array from sorting right code: #include <iostream> #include <cmath> using namespace std; struct coords { int x;
4
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I...
3
by: george r smith | last post by:
I am converting a delphi chess program to C#, this is how I study a new language and I have the following problem. How do you use an enumeration value as an indexer for an array ? This is what I...
4
by: annmartinson | last post by:
yes, I've read and tried umpteen of the sort functions and still can't get it right. which one do I need??? ///////////////////////////////////////////// here is my array: $final = "fire";...
1
by: richard | last post by:
OK, I have got bi-directional datagrid sorting down, have a created a nice sorting class. My question, how can I add sorting to more than one datagrid on the same page? Everything I have tried has...
11
by: Trent | last post by:
Running this I see that on first run, both bubble and selection sort have 9 sort counts while insertion sort has ZERO. With a sorted list, should this be ZERO for all? Also bsort and Ssort have...
4
by: Ravi4raj | last post by:
Hi all, Here i have to know is thr any way to sort the array according to thr Bit Reverse.. for Example: If we take the Array size of 256,then the Sorting Should be like below. a =...
11
by: michaelperl | last post by:
hi all ! I need some help please I'm using a database tool for developing and driving dynamic content. With the tool I use I make queries based on my tables and I set the sorting order of my...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.