Array question? Ordering variables

I grab 5 variables from my database... that's fine

for example, here are the variables

$file1 = "test"
$file2 = ""
$file3 = ""
$file4 = "testing2"
$file5 = "testing3

what i want to do is is make

$file1 = "test"
$file2 = "testing2"
$file3 = "testing3"
$file4 = ""
$file5 = ""

this could be in any order of order. sometimes file2 and file4 could be filled..

thanks
jmingoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TeRReFCommented:
The quickest way would be to an ORDER BY in the query already.
0
jmingoAuthor Commented:
in the sql select statement?? what would it be??
0
DolGuldurCommented:
SELECT fieldname FROM tablename ORDER BY fieldname

By the default the order is ascending, if you want it descending, add desc at the end of your statement.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

jmingoAuthor Commented:
i need to select everything from my table. i don't think a select statement would arrange my variables would it??

0
DolGuldurCommented:
SELECT * FROM tablename ORDER BY fieldname
0
jmingoAuthor Commented:
i don't think i'm explaining myself correctly. i select info from the database

$file1 = $rows['file1'];
$file2 = $rows['file2'];
$file3 = $rows['file3'];
$file4 = $rows['file4'];
$file5 = $rows['file5'];

some databse fields are blank and others aren't....

i want to reassign my variables in order with only fields that have info. for example if file3 is the first field that has data then i want it to be $file1. then if the next field that has data is file5 then i want that to be $file2.

i only want this to happen for the file1, file2, file3, file4, file5 fields.
0
RoonaanCommented:
You can use:

sort($rows);
$file1 = $rows[0];
$file2 = $rows[1];
$file3 = $rows[2];
$file4 = $rows[3];
$file5 = $rows[4];

-r-
0
jmingoAuthor Commented:
but there are lots of other fields i'm gathering also.... i just want to sort the $file variables.
0
blue_hunterTechnical ConsultantCommented:
try asort
it sort the values in the array

you can get the examples for the sorting from links belows
http://my.php.net/manual/en/function.asort.php

0
jmingoAuthor Commented:
blue_hunter: thanks, i'll look into that.
0
jmingoAuthor Commented:
blue_hunter: i can't really figure out how asort would work in my situation. can you shed some light on it, how i could possible do it?
0
blue_hunterTechnical ConsultantCommented:
sorry pardon my mistake at previous post,
should use arsort() in this case
would need to reverse sort the value since we need to push the empty value to be at the bottom of the array
http://my.php.net/manual/en/function.arsort.php


firstly, assign those variable in array

$file = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$file[] = $row["files1"];
$file[] = $row["files2"];
$file[] = $row["files3"];
$file[] = $row["files4"];
$file[] = $row["files5"];
}
arsort($file);


then you would have somethings like below

$file[0] = "test"
$file[1] = "testing2"
$file[2] = "testing3"
$file[3] = ""
$file[4] = ""


* pardon me, if i had misunderstand about your question :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmingoAuthor Commented:
here's the solution i found

error_reporting(E_ALL ^ E_NOTICE);

$file1 = "/something/this file.txt";
$file2 = "";
$file3 = "/this/that.txt";
$file4 = "";
$file5 = "/test/file.txt";

$filearr=array($file1,$file2,$file3,$file4,$file5);

if ($filearr[0] == "") {
unset($filearr[0]);
}

if ($filearr[1] == "") {
unset($filearr[1]);
}

if ($filearr[2] == "") {
unset($filearr[2]);
}

if ($filearr[3] == "") {
unset($filearr[3]);
}

if ($filearr[4] == "") {
unset($filearr[4]);
}

$filearr = array_values($filearr);

$newfile1 = $filearr[0];
$newfile2 = $filearr[1];
$newfile3 = $filearr[2];
$newfile4 = $filearr[3];
$newfile5 = $filearr[4];

?>

is there a better way to do it??
0
blue_hunterTechnical ConsultantCommented:
maybe add
array_pad() at the bottom of the scripts  to make it 5 elements

array_pad($filearr, 5, "");


0
jmingoAuthor Commented:
so put it here after this

$filearr = array_values($filearr);

array_pad($filearr, 5, "");

??
0
blue_hunterTechnical ConsultantCommented:
yes :)
0
jmingoAuthor Commented:
thanks... i'll accept your post anyways for all the help.
0
blue_hunterTechnical ConsultantCommented:
thanks you for the points too ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.