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


SuperbHosting.net have generously sponsored dedicated servers to ensure a reliable and scalable dedicated hosting solution for BigResource.com.





Sort A Mysql Result According To A Php Array


Hi everybody !
I have a question for the gurus ;+)
I have to arrays :

PHP Code:




$id = array(45,9,21);
$pos = array(3,1,2);






I select in my mysql table using the ids :

PHP Code:




$sql = "SELECT * FROM MyTable WHERE id IN(".implode("",$id).")";






and I now want to sort the result according to the $pos array.
Is there a way to do this directly in mysql ?
If not, what would be the smartest way to do it in PHP ?
Because I would prefer NOT to do the following :

PHP Code:




while ($t = mysql_fetch_array($ret)) {
    $a_field1[] = $t["field1"];
    $a_field2[] = $t["field2"];
    $a_field3[] = $t["field3"];
    $a_field4[] = $t["field4"];
...
}
array_multisort($pos,$a_field1,$a_field2,$a_field3,$a_field4)






I fear it could be a very time consuming process when there is a lot of results and a lot of fields. But may be the no other solution...

Thanks




View Complete Forum Thread with Replies

Related Forum Messages:
Allow The User To Sort A Mysql Result
So what i got going on is a database that has, things about a video game, including the image location. what i want to do is have a nav bar up top, so the user can select a differnt way of sorting the mysql result, from like ASC to DESC, or sort by genre.

View Replies !
Allow User To Sort MYSQL Result
I'm just looking for ideas for the best way to allow a user to sort a list of results from a MYSQL query.

I have a system working at the moment, but it's fairly messy and inefficient, so I'd like to know if anyone has any better ideas.

Right now, my page allows users to view entries from the last day, week, month, year, or simply all. I use variable $organize, which is passed through get. Code:

View Replies !
Mysql Result Into Array?
I have a shopping cart script sending info to a processor. I need to send the qty's and item name's in some sort of string via a single variable to the process form.
i.e. (3) Hipster Turnips, (6) Butter Milk Baby Brains, (2) Super Freaks Code:

View Replies !
Mysql Result Set As An Array
the question is, is there a way of assigning each value returned from the sql query and assinging them to a single array. for example i have a a query like so;

$table = "sales_table";
        
$query = "select treatments from $table where
date between  ད/08/2006' and ཕ/08/2006'";
$result = mysql_query($query);

because the values stored in the treatments field are imploded as a single string when the user submits the form the value looks like; 1, 22, 33. Code:

View Replies !
MySQL Result As Array
$db= new MySQL($host,$dbUser,$dbPass,$dbName);
$sql=("SELECT * FROM sticker_files WHERE sticker_cat_id=Ǝ'");
$catdirs = $db->query($sql);
$dirs=$catdirs->fetch()

and I need a for each loop (i think) to insert that data below:

$params = array(
'mode' => 'Jumping',
'perPage' => 3,
'delta' => 2,
'itemData' => array(****RESULTS HERE AS COMMA SEPARATED string)
);

View Replies !
Mysql Result To Array
I have this query, it gives the suspected information in myphpadmin:

select listid, count(*) from table1 group by listid

How do I put this the result into an array in php? I have problems with the
count(*) field

View Replies !
Array With MySQL Result
//MAIN FLOW
$counter = 0;
$sql[$counter] = "SELECT * FROM category WHERE parent = 0 ORDER BY parent";
$result[$counter] = mysql_query($sql) or die(display_and_log_error("SQL ERROR: ".__LINE__));
while($myrow[$counter] = mysql_fetch_assoc($result[$counter])){
echo $myrow[$counter][category_name];
...

View Replies !
Replacing Array With Mysql Result
I wanted to repalce the following line of code $data = array(40,21,17,14,23); with

for($i=0;$i<$numrows;$i++)
{
//print(mysql_result($result,$i,2));print("<br>");
array_push($data,mysql_result($result,$i,2));}

where mysql_result($result,$i,2) is the value and when i print it displays the values. But $data array is the Y axis value for drawing a chart. Here I wanted to replace the hard coded value with values from mysql but the second code does not function. Does anybody have idea how can I replace the $data array or what may be the problem with my coding The first one works but the second one does not work but in both cases it does not display any error.

View Replies !
If Result Of Mysql Query Is Only One Row/column, Why Use An Array?
If I'm doing a very specific select statement, which I know will only ever return one value, can I get that resulting bit of data without storing it in an array or object?
If not, is mysql_fetch_array() the fastest method for getting this one value?

View Replies !
MySQL Result To Real Array In Function
I'd like to create a function which input is the result of a mySQL
query.
The output should be exactly the same, only not a mySQL result array,
but a 'real' array.
So it should also get the fieldnames returned by mySQL and use those as
keys.

I can't get things to work properly: it should return a
multidimensional array,
like

$result_array[1] = array(
[field1] => field1 value,
[field2] => field2 value,
etc.
)

somehow my result is (with code below)

$result_array[1] = array(
[0] => field1 value,
[field1] => field1 value,
[1] => field2 value,
[field2] => field2 value,
etc.
)

+++++ code ++++++

$get_res= mysql_query(QUERY);

if( $res = mysql_fetch_array( $get_res ) )
{

do{

$result[] = $res;

}while( $res = mysql_fetch_array( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value);

};

View Replies !
Changing Array Format From Result Of MySQL
here is what MySQL returns in a visual way..

game_id | team_id | stat_id
1 | 1 | 1
1 | 2 | 3
1 | 3 | 7
1 | 3 | 6
2 | 1 | 1
2 | 4 | 3

in records with the same game_id, there will be no repeating team_id
in records with the same team_id, there will be no repeating stat_id

so, after I got the query, I made them into this format

PHP Code:

View Replies !
Can I Put A Mysql Query Result Directly Into A Multidimensional Array?
I have two joined tables:

Departments - which contains the department description and a key
Positions - which contains position data

What I'm attempting to achieve is to display the department title in one table cell and then list all the positions associated with that department in the cell under neither the title. I need to generate a table two column table with as many rows as required, depending on the number of departments, which is dynamic.

Do this I'm trying to turn the result of my SQL query into multidimensional array e.g. dept = VioP positions = "designer", "engineer" etc, which I can do then use to generate the table.

I've done a search on the forum and looked around the manual and I'm stumped on how to achieve this, a point in the right direction would be greatly appreciated. The query I'm using is below. PHP Code:

View Replies !
Making Mysql Result Into Multi-dimensional Array
My php problem is I want to make the result of 3 tables linked through foreign keys into a 3-dimensional array that reflects their relationship without repetition and without having to call the database more three times.

I know I'm doing a couple of things wrong, one thing for instance is initializing a variable at the end of the loop to remember the previous value s that it does not repeat itself, surely there must be a more elegant way to go about this, but I'm lost and I've been stuck on this all day. Code:

View Replies !
Sorting Sql Results In An Array Using Things Outside The Array To Sort Them?
I'm using PHP to connect to a database full of place names, and their associated post codes, latitude and longitude on the earth. A user types his or her post code into a form, plus a distance (in miles), and the script looks for other area codes (first half of the post code) within that distance.

I can do that fine, but the results are ordered alphabetically by postcode, and I want to order it by the distance. Problem here, is that the distance isn't part of the array taken from the database, it takes the latitude and longitude of the area it's on in the while loop, and calculates the distance between the user's post code and that one, but it's not part of the array, so I can't sort the array using it.

How can I sort it? Can I add the calculated distances to the array, and sort it using sort()? If yes, how do I add them to the array?

Here's the page, just to explain better what the script does: http://navimaker.org/ed/owain/results.php?postcode=gl15&distance=34
Mess around with the postcode and distance vars.

I'd put the source on here too but I'm doing all this over SSH and can't copy & paste very easily .

View Replies !
Sort Array - [2] => Array( [id] => 232, [zone] => C, [color] => Yellow )
Array:

=> Array( [id] => 64, [zone] => a, [color] => blue )[1] => Array( [id] => 17, [zone] => b, [color] => grey )
    [2] => Array( [id] => 232, [zone] => c, [color] => yellow )
    [3] => Array( [id] => 36, [zone] => a, [color] => red )
    [4] => Array( [id] => 178, [zone] => a, [color] => white )
    [5] => Array( [id] => 10, [zone] => c, [color] => blue )
    [6] => Array( [id] => 115, [zone] => a, [color] => orange )
    [7] => Array( [id] => 345, [zone] => a, [color] => black )
    [8] => Array( [id] => 19, [zone] => b, [color] => green )
    [9] => Array( [id] => 206, [zone] => a, [color] => red )

View Replies !
Array Explode Problem Result In A 6 Item Array
I have the following:

$BB_Basket = "basket=¦APL6007,2.00¦AVX8900,1.00¦ARCAV500,1.00";
$basket_containers = explode('¦', $BB_Basket);
unset($basket_containers[0]);
print_r($basket_containers);

This results in

Array ( [1] => APL6007,2.00 [2] => AVX8900,1.00 [3] => ARCAV500,1.00 )

Now when I try to explode further with

$basket_items = explode(',', $basket_containers);
print_r($basket_items);

This results in

Array ( [0] => Array )

This should result in a 6 item array. Any ideas what I'm doing wrong?

View Replies !
Sort The Array By The Values In Array[$i][0]
How would I go about sorting the following array, I've been looking at array_multisort but to be honest I'm either not fully understanding it or it's not capable of what I'm trying to do.

What I'd like to do is to sort the array by the values in array[$i][0] but I can't figure it out, the values being zzzz,a0,a1,00000000000000. PHP Code:

View Replies !
Array Sort By Value
I have an array that has the following structure:

Array
(
[0] => xml_make Object
(
[name] => Alfa Romeo
[totalItems] => 270
)

[1] => xml_make Object
(
[name] => Aston Martin
[totalItems] => 7
)

[2] => xml_make Object
(
[name] => Audi
[totalItems] => 948
)

[3] => xml_make Object
(
[name] => Austin
[totalItems] => 51
)

[4] => xml_make Object
(
[name] => BMW
[totalItems] => 2235
)
}

How can I sort the array by the value in total Items?

View Replies !
I Need To Sort An Array
Current Case:
I have a table in a database

PRODUCTS
--------------
product_id
product_name
product_category
product_order

I am trying to create a change order script. With Up/Down links.

So I run my querry and store the data in the database. So I end up with an
array ....

View Replies !
RSS Array Sort
I am pulling in a few RSS feeds into my site and I need to be able to sort the array I build by the pubdate of the feeds. The resulting array looks like this:

Array
(
    [0] => Array
        (
            [title] => Title of Feed
            [link] => link of feed
            [pubdate] =>  2007-05-04 00:00:00
            [description] =>  Description
        )

    [1] => Array
        (
            [title] => Title of Feed
            [link] => link of feed
            [pubdate] =>  2007-06-04 00:00:00
            [description] =>  Description
        )

I need to be able to sort by $theitems[$i]['pubdate'], how can I do that?

View Replies !
Sort An Array
How do we sort 3 dimensional array by sorting on 3 , 2 and 1st dimensions?

View Replies !
Sort An Array By Sub Key
If i have this array : PHP Code:

(
    [0] => Array
        (
            [id] => 9
            [date] => 2005
        )

    [1] => Array
        (
            [id] => 10
             [date] => 2002
        )

    [2] => Array
        (
            [id] => 11
            [date] => 2004
        )

)

Is there any function to sort it by date key ?

View Replies !
Array Sort
I've got an array with the following structure:

$process[0] => array("recordID" => 1, "name" => "matt");
$process[1] => array("recordID" => 2, "name" => "john");
etc

I need the process array sorting by record ID...... I've tried doing

asort($process)

but that doesnt work - I dont know how to access the elements inside the array to sort by..... any ideas?

View Replies !
Trying To Sort An Array
I want to sort an array using an associate key. How do I sort an array as the following: Code:

View Replies !
Sort PHP Array
Code:

$clientArray[$i]["CLIENT_CODE"]
$clientArray[$i]["CLIENT_NAME"]
$clientArray[$i]["SERVICE"]
$clientArray[$i]["HARDWARE"]
$clientArray[$i]["MUSIC"]
$clientArray[$i]["PURCHASE"]
$clientArray[$i]["INVOICE_TOT"]
$clientArray[$i]["POST"]
$clientArray[$i]["RECALL"]
$clientArray[$i]["FIRST_INV"]
$clientArray[$i]["GROUP_TYPE"]
$clientArray[$i]["END_DATE_S"]
$clientArray[$i]["MEMO"]

View Replies !
Sort Array
I've read the manual and looked at a few posts on this site as well as through goole, and I cannot get my head around sorting an array with the following data: Code:

View Replies !
Using A Pseudocode To Sort An Array
I want to sort an array, but the problem is that I can't use sort, rsort, asort, etc. So I want a Pseudocode to order the array.

View Replies !
Sort() Messed Up My Array!
I have an assosiative array with "key"s and "value"s which I get by using array_count_values() on $array.

$array is an array of numbers, for example 1, 0, 0, -5.

Here's some code:

View Replies !
Sort Array Without Index
I'd like to sort a multidimensional array that hasn't numerical
index, in fact it is like:

Array
(
[mobile] => Array
(
[name_str] => mobile
[relationCount_int] => 1
)

[video] => Array
(
[name_str] => video
[relationCount_int] => 2
)

[flash] => Array
(
[name_str] => flash
[relationCount_int] => 1
)

[tool] => Array
(
[name_str] => tool
[relationCount_int] => 1
)

)

And I'd like to sort, for instance for the value of
"relationCount_int", so the new order should be something like:

Array
(
[video] => Array
(
[name_str] => video
[relationCount_int] => 2
)
[mobile] => Array
(
[name_str] => mobile
[relationCount_int] => 1
)


[flash] => Array
(
[name_str] => flash
[relationCount_int] => 1
)

[tool] => Array
(
[name_str] => tool
[relationCount_int] => 1
)

)

View Replies !
Multidim Array Sort
I am wondering if someone can point me to the needed sort function. There are so many of
them that I simply got lost by reading their descriptions. I try to explain what I need.

I have a multidimensional array and I have a function that outputs it as a table.

$arr[0] contains an array that contains usernames
$arr[1] contains an array that contains real names
$arr[2] contains an array that contains e-mails
etc...
$arr[last] contains integers for users "levels"

My function outputs users with their datas in the columns. Each column means one user.

Now I want to sort this whole array by levels. So, all other data should be sorted
according to the pattern in levels row.

How is this thing done?

View Replies !
Sort Values In Array?
Does anybody know how to sort through an array so that I would only receive distinct values. For example, if I have an array like this:

Examplearray[0] = apple
Examplearray[1] = orange
Examplearray[2] = orange
Examplearray[3] = pear
Examplearray[4] = banana
Examplearray[5] = banana

I want the output to be this:

apple, orange, pear, banana

View Replies !
How Do I Sort My Two Dimensional Array?
How do I sort my two dimensional array? I want to sort "numb" numerically, then "part" alphabetically. Code:

View Replies !
Sort Array By Date
<?
$fp = fopen("data/_index_all.txt","r");
while ($line = fgets($fp,1000)){
if(ereg($strAuswahl,$line)){
$t = explode ("|", $line);
list ($name, $prefix, $datetime, $svcid, $category, $msgnum, $prio, $keyword, $catchline, $title, $imagesrc, $teaser ) = $t; }} ?>

how can sort the above array by $datetime? so that the latest messages will appear on top.

View Replies !
Multidimension Array Sort
I need to sort a multidimension array according to a specified column..

View Replies !
Sort An Array Of Objects
I would like to sort an array of objects by a variable of an object variable. I have an array of objects (obj_array). One of the variables of (obj_a) is an object (obj_b). I want to sort by a variable of (obj_b). PHP Code:

View Replies !
Sort Color Array?
i found this message:
and i want to know more about it too.
==========================================
example:
$s_Colors="0066FF;0066CC;3366CC;0033FF;003399;003366;99CCFF;3 399FF;0099FF;66
99CC;336699;006699;66CCFF;33CCFF;00CCFF;3399CC;009 9CC;003333;99CCCC;66CCCC;3
39999;669999;006666;336666;CCFFFF;99FFFF;66FFFF;33 FFFF;00FFFF;00CCCC;99FFCC;
66FFCC;33FFCC;00FFCC;33CCCC;009999;66CC99;33CC99;0 0CC99;339966;009966;006633
;66FF99;33FF99;00FF99;33CC66;00CC66;009933;99FF99; 66FF66;33FF66;00FF66;33993
3;006600;CCFFCC;99CC99;66CC66;669966;336633;003300 ;33FF33;00FF33;00FF00;00CC
00;33CC33;00CC33;66FF00;66FF33;33FF00;33CC00;33990 0;009900;CCFF99;99FF66;66C
C00;66CC33;669933;336600;99FF00;99FF33;99CC66;99CC 00;99CC33;669900;CCFF66;CC
FF00;CCFF33;CCCC99;666633;333300;CCCC66;CCCC33;999 966;999933;999900;666600;F
FFFFF;CCCCCC;999999;666666;333333;000000;43dddd;65 432b;777665;4443cc;CCCCC3;
A906B0;9CC33F;C8C8C8;688888;777752;66E554;7655FF;5 55577;D4DCE8;99AECE;3A5A8A
;9A0030;EF3F6B;FFF543;C906CF;AA2222 ";
And true a function i add random colors in the string,
$s_Colors.= ";".strtoupper($_POST['colorvalue']);

then i make one array:
$a_Colors=array_change_key_case(split(";",$s_Colors), CASE_UPPER);
when i do
sort($a_Colors);
print_r($a_Colors);

or
asort($a_Colors);
print_r($a_Colors);

View Replies !
Sort Array From .dat File
I'm using the following to open and print values from a .dat file but
I can't figure out how to sort the results. I think I need to use
either the array_multisort() or krsort() functions. If so, how would I
do that?

<html>
<body>
<pre>
<?php

$fp = fopen("datfile.dat", r);
$count = 0;
while(!feof($fp))
{
$register = fgets($fp, 4);
$sales = fgets($fp, 5);

$register_array[$count] = $register;
$sales_array[$count] = $sales;

print "Register: $register_array[$count]<br>";
print "Sales: $sales_array[$count]<br>";
$count = $count + 1;
}

?>
</pre>
</body>
</html>


View Replies !
How Do You Sort An Array Of Objects?
The PHP Manual offered me NO help on this one, so now I appeal to the
populace:

Code: ( php )

View Replies !
Sort Array By Third Value - Date
Let's say I have 3 elements in my array:

= apple[1] = orange
[2] = 06/12/2007

How do I go about sorting by date using the 3rd element?

View Replies !
Sort Array Alphabeticaly
I am trying to sort array alphabetically ... But I have a problem..

$array = array(
'1' => 'a',
'2' => 'c',
'3' => 'b'
);

When I sort this array I get this:

[code]
$array = array(
'1' => 'a',
'2' => 'b',
'3' => 'c'
);

But I want to get this:

[code]
$array = array(
'1' => 'a',
'3' => 'b',
'2' => 'c'
);

I want key words to be sorted as well .

View Replies !
Array Sort() Function
Does it also sort numbers? IE, sort it so the array is in order 1,2,3,4,5

But also I have some text after the number.

It doesn't seem to be able to sort numbers. If not, what can do this?

View Replies !
Sort 2D Array (by Value Of Second Element)
I was looking forward to sorting an array by the value of the second element. e.g.: Sort by number of hits:

<?php
$webs = array (
   array("url" => "example.com", "name" => "example 1", "hits" => "5"),
   array("url" => "example.org", "name" => "example 2", "hits" => "20"),
   array("url" => "example.net", "name" => "example 3", "hits" => "10")
);
?>

View Replies !
Sort Array In Order
I have an array, of several elements (which I populate from reading from a db) that may or may not have duplicate values in it.

I want to sort the array so that the value that occurs the most amount of times is top, etc, so when I output the array, it is in order of the most frequent occuring value to the least.

If there's no duplicate values then I'll want to reverse the array - and i know array_reverse() will do that. I've seen usort() but I'm not sure if that's what I need.

View Replies !
How Can I Sort This Array By Timestamp?
I have an array of posts that I need to sort by date. I use an rss reader to get the posts, but it fills the array by post, and not by data. I then need to parse the array to display the posts by date.

How can I do this? is there a way to insert the data into the array based on the timestamp? Or should I insert the data and then sort by the timestamps? ....

View Replies !
Sort Array $_post
I am trying to do is to sort an array from the $_post. The user enters 5 numbers from a form and I want to sort those 5 numbers. My current script looks like this Code:

View Replies !
How Do I Sort An Array By It's Second Element.
I've searched the forums and can't seem to find a solution to this. How do I sort an array by it's second element. For example, how do I sort this array with this general setup:

$player[0]['pts'] = 2;

so that the player with the most points (pts) is at the start of the array?

View Replies !
Array Sort & Output
$match['date'],
$date,
$link,
$match['money'],
$result

I have a while statement... in it it is calling for $sorttext[$run]= array( )....
It runs from 0-99. How physically can I write the array statement so it takes in the above variables...

then I want to run another while statement or for statement where it only displays from 0-49. It needs to sort by $match['date'] and then only output $date,$link,$match['money'],$result and each part goes into its own section of a table row.

Have any idea of the actual array statement writing, sorting, and output?

View Replies !
How Do You Sort A 2-dimensional Array?
$fbArray[$j] = array($feedbackCategoryArray[$i]['attributes']['NAME'] =>
$feedbackCategoryArray[$i]['attributes']['DISPLAYNAME']);

I can't begin to fathom in my befuddled mind how to sort this array $fbArray
into alphabetical order according to
$feedbackCategoryArray[$i]['attributes']['NAME'] - I am utterly lost, can
someone explain in detail how I would do it and why.

View Replies !
Sort Array By String Length
I've been muddling through functions to make a small search engine. I'm at the point where I have each word and its position in an array, the word position being the key and the word as the value.

Each word gets hashed to an integer value, and it's useful for me to have the values in the array ordered by their string length (in ascending order) to do this.

After a gander through the array functions in the PHP manual the closest match I see to this is uasort(array,function cmp_function)

Would this be the right function to use? And if it is, any idea what the function would look like to get them in ascending order of length...

View Replies !
How Can I Sort A Multi-dimensional Array?
How can I sort a multi-dimensional array?

For instance:
a[Name], a[adress], a[number of messages].

How to sort on the item of number of messages?

View Replies !
Sort File Array By Time
I have an array called $data, which contains every image in a specific folder and i want to sort them, preferrably by upload time, but if this isn't possible the time of the last change would also do the work. Anybody of you guys know how?

View Replies !
Sort An Associative Array Alphabetically
I am working on a product catalog where it displays a product type on a page via the type variable. The products should be displayed alphabetically by $make and $model and i couldn't find any snippets or info other than asort etc. Just curious if anyone else needed such a sort function and how they did it.

ie: product makes Maico, Madsen, Bio-logic etc and their models MA-300, Itera, AC-200. So I would have listings like Maico MA-300, Maico MA-800, Madsen Itera etc., and I need these alphabetical using make and model values. PHP Code:

View Replies !

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