array sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MrKnight
Forum Newbie
Posts: 22
Joined: Fri Jan 28, 2005 8:54 am
Location: Another Planet

array sorting

Post by MrKnight »

i list of a column's values by while loop, $row[column],
i wanna to list from $n, for example, in normal i get val1, val2, val3, val4.... but what should i do if i want in the way, val3, val4,val1,val2 or val2, val3, val4, val1...
thanks in advance.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Your wording was very hard to understand but if I do understand what you're asking you can do this by creating another temporary array which you will use for re-ordering and then you can move the contents of that array back into the original.
Last edited by nigma on Sat Feb 12, 2005 7:52 pm, edited 1 time in total.
MrKnight
Forum Newbie
Posts: 22
Joined: Fri Jan 28, 2005 8:54 am
Location: Another Planet

Post by MrKnight »

true, it is because of my poor english..
if i edit the question again;

i list of a column's values that i get database table by while loop,
in the way

Code: Select all

while ($row=mysql_fetch_array($query)){ print $rowїcolumn_name]; }
this work simply/orderly and outputs the values, and now what i wanna do is to line up values in different ways like val3, val4,val1,val2 or val2, val3, val4, val1... instead of val1, val2, val3, val4...

and, nigma, your suggestion makes no sense to me for now, thanks anyway i appreciate your concern as well
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

if you want them random, throw an ORDER BY RAND() on the end of your query. Otherwise, be more specific about the order you want them in.
MrKnight
Forum Newbie
Posts: 22
Joined: Fri Jan 28, 2005 8:54 am
Location: Another Planet

Post by MrKnight »

no, not random
(if i cant get a solution i will try so)
normal listing in this way; value1, value2, value3, value4
i want to list firstly in the way; value1, value2, value3, value4
and then value2, value3, value4, value1
and then value3, value4, value1, value2
and then value4, value1, value2, value3
... (sowwy)
how, sounds nonsense?

:roll:
Last edited by MrKnight on Tue Feb 01, 2005 12:10 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your sequence makes sense, up until the last example.

it's typically called rotation, or rotating shift. Basically, you shift the first element off, then push it back onto the end.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

while (list($varone, $vartwo, $varthree)=mysql_fetch_array($result)) {
   echo ("2- $vartwo 1- $varone 3-$varthree");
   // keep putting echo () with the variables in a different order
}
I take it thats what you want to do. Can't be sure though, your question was worded strangely
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Sorry for further confusing you. My whole statement was null and void because I didn't understand the situation.
MrKnight
Forum Newbie
Posts: 22
Joined: Fri Jan 28, 2005 8:54 am
Location: Another Planet

Post by MrKnight »

sorry guys, :D, for making trouble...
at last, at least, i got the event (thanks to feyd for indicating the way)
namely;

Code: Select all

$array = array("value 1", "value 2", "value 3", "value 4");

for ($i=1;$i<=count($array);$i++)&#123;
	foreach ($array as $value) print $value.", ";
	$deleted = array_shift($array);
	$added = array_push($array,$deleted);
	print "<br>";
&#125;

// outputs
value 1, value 2, value 3, value 4, 
value 2, value 3, value 4, value 1, 
value 3, value 4, value 1, value 2, 
value 4, value 1, value 2, value 3,
but values was in the database table and this piece of code needed some extras, and here it is; how i retrieved the values from database.

Code: Select all

// connect to the db and send query
$array = array();
while ($row=mysql_fetch_assoc($query)) array_push($array,$row&#1111;column_name]);
for ($i=1;$i<=count($array);$i++)&#123;
	foreach ($array as $value) print $value.", ";
	$deleted = array_shift($array);
	$added = array_push($array,$deleted);
	print "<br>";
&#125;

// outputs
value 1, value 2, value 3, value 4, 
value 2, value 3, value 4, value 1, 
value 3, value 4, value 1, value 2, 
value 4, value 1, value 2, value 3,
works...
meanwhile, there is else way to gather all rows in an array?[/quote]
Post Reply