Page 1 of 1
array sorting
Posted: Tue Feb 01, 2005 9:58 am
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.
Posted: Tue Feb 01, 2005 10:35 am
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.
Posted: Tue Feb 01, 2005 11:00 am
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
Posted: Tue Feb 01, 2005 11:30 am
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.
Posted: Tue Feb 01, 2005 12:07 pm
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?

Posted: Tue Feb 01, 2005 12:09 pm
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.
Posted: Tue Feb 01, 2005 1:26 pm
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
Posted: Tue Feb 01, 2005 8:01 pm
by nigma
Sorry for further confusing you. My whole statement was null and void because I didn't understand the situation.
Posted: Wed Feb 02, 2005 5:10 am
by MrKnight
sorry guys,

, 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++){
foreach ($array as $value) print $value.", ";
$deleted = array_shift($array);
$added = array_push($array,$deleted);
print "<br>";
}
// 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їcolumn_name]);
for ($i=1;$i<=count($array);$i++){
foreach ($array as $value) print $value.", ";
$deleted = array_shift($array);
$added = array_push($array,$deleted);
print "<br>";
}
// 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]