473,799 Members | 3,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Resorting an array?

LRW
I'm not even sure this is possible, and if it is, I'm having a hard
time focusing on a function that would make it work.

What I have is a dynamically generated form with dozens of rows from a
mySQL query.
Each row in this form has a checkbox to select what rows the user
wants sent to the processing page.
Each row also has a slew of various text fields and dropdown menus
with data from the query and editable by the user.
The user checks the boxes to send on.

Then, on the process page, an array is created based on which
checkboxes are selected.

Now at the moment the process page processes each selected row on a
for-each in the order they were listed on the previous page.

Keeping the mySQL sort order the same as it is on the 1st page, is
there a way to resort the array on the process page so that the rows
are processed in a different order, based on one of the fields in the
array?

For example, each row has the following data:

checkbox value(which is also the row
id),firstname,l astname,address ,size,color

And the array could have dozens of rows. Right now it's processed in
order of checkbox value because that's how it's sorted on page one.
What if I want to change the sort in the array by lastname before
processing?

Did that make any sense?

I looked up asort() and usort() and they LOOK like something that
might work...at least I understand what they're doing, but I'm not
sure if something like that would work in this complicated (maybe?) of
an array I have going.
And to be honest, I have no idea in my array what is the key (the
value of the checkbox selected?) and what happens when you have more
than 2 items in each row of the array?

If one of the ?sort() types IS what I need to look into, someone let
me know? If there's another concept that would work better, someone
clue me in?
I'd appreciate it!
Thanks,
Liam
Jul 17 '05 #1
4 1771
LRW wrote:
I'm not even sure this is possible, and if it is, I'm having a hard
time focusing on a function that would make it work.

What I have is a dynamically generated form with dozens of rows from a
mySQL query.
Each row in this form has a checkbox to select what rows the user
wants sent to the processing page.
Each row also has a slew of various text fields and dropdown menus
with data from the query and editable by the user.
The user checks the boxes to send on.

Then, on the process page, an array is created based on which
checkboxes are selected.

Now at the moment the process page processes each selected row on a
for-each in the order they were listed on the previous page.

Keeping the mySQL sort order the same as it is on the 1st page, is
there a way to resort the array on the process page so that the rows
are processed in a different order, based on one of the fields in the
array?

For example, each row has the following data:

checkbox value(which is also the row
id),firstname,l astname,address ,size,color

And the array could have dozens of rows. Right now it's processed in
order of checkbox value because that's how it's sorted on page one.
What if I want to change the sort in the array by lastname before
processing?

Did that make any sense?

I looked up asort() and usort() and they LOOK like something that
might work...at least I understand what they're doing, but I'm not
sure if something like that would work in this complicated (maybe?) of
an array I have going.
And to be honest, I have no idea in my array what is the key (the
value of the checkbox selected?) and what happens when you have more
than 2 items in each row of the array?

If one of the ?sort() types IS what I need to look into, someone let
me know? If there's another concept that would work better, someone
clue me in?
I'd appreciate it!
Thanks,
Liam


Hi Liam,

I think I understand what you want.
Only because i stubled on a similar problem recently. :-)

Your problem is that you want a bunch of associated data sorted on one of
the values of one item in those bunches of data, right?
This is how you can manage that: associative arrays (you guessed) but just a
little different than you tried. :-)

in your receiving script try this:
1) store all your rows in arrays.
eg:
loop over your Postdata{
$myRow[1]["var1"] = $_POST["var1"];
$myRow[1]["var2"] = $_POST["var2"];
$myRow[1]["var3"] = $_POST["var3"];
}

ok?
the [1] respresents a row with data. (Use 2 for second row, etc.)
the ["varX"] respresents the value for a certain variable for that row.

2) Now create a new assoc array and use for the key the value where you want
to sort on.
So suppose you need var2 for sorting:
for all your $myRow{
$allData[$var3-from-RowX] = $myRow[X]
}

3) Now you have a fresh datastructure that has keys based on var3.
You can sort it now with asort.

Hope this helps you.

Maybe there is a smarter/faster way, but I like this way because I
understand how it is working. :-)

Good luck,
Erwin Moller

Jul 17 '05 #2
Erwin Moller wrote:

topy correction:
So suppose you need var2 for sorting:
should of course be:
So suppose you need var3 for sorting:

Jul 17 '05 #3
.oO(Erwin Moller)
Your problem is that you want a bunch of associated data sorted on one of
the values of one item in those bunches of data, right?


You can use array_multisort () to create a kind of column sort function:

function csort($array, $column) {
$s = array();
foreach($array as $row) {
$s[] = $row[$column];
}
array_multisort ($s, SORT_ASC, $array);
return $array;
}
Example:

$foo = array(
array('firstnam e' => 'foo', 'lastname' => 'bar'),
array('firstnam e' => 'bar', 'lastname' => 'foo')
);

print_r(csort($ foo, 'firstname'));
Output:

Array
(
[0] => Array
(
[firstname] => bar
[lastname] => foo
)

[1] => Array
(
[firstname] => foo
[lastname] => bar
)

)

HTH
Micha
Jul 17 '05 #4
Michael Fesser wrote:
.oO(Erwin Moller)
Your problem is that you want a bunch of associated data sorted on one of
the values of one item in those bunches of data, right?


You can use array_multisort () to create a kind of column sort function:


<snip>

Hee Micha,

That is cool. :-)
Never ever saw that function.
I'll check it.

Thanks,

Regards,
Erwin Moller

Jul 17 '05 #5

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

Similar topics

2
2783
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
575
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a print_r cmd any suggestions would be great. Thanks much Todd //avShed array Array ( => Array ( => 1 => 08:00 ) => Array ( => 1 => 08:05 ) => Array ( => 1 => 08:10 ) => Array ( => 1 => 08:15 ) => Array ( => 1 => 08:20 ) => Array...
15
5195
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
3484
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array( "name", "title", "reports to user id", "start date in the format: mm/dd/yyyy" ) ); How can I display this hierarchy in simple nested <li> tags in the most
12
55573
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are also removed) So far I have (val is value, ar is array, returns new array):
8
10234
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when it is assigned a text literal?? HOW can I change the array value to upper case then? What other method exists for arrays? Ex: var GridArrayName1 = new Array(); GridArrayName1 = new Array ('test-value'); GridArrayName1 = GridArrayName1...
58
10186
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
35
6669
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
11
39472
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not rst.EOF i = i + 1 If rst!Something = Then astrMyArray(i) = rst!Something
0
9686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10475
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.