Link to home
Start Free TrialLog in
Avatar of jmingo
jmingo

asked on

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
Avatar of TeRReF
TeRReF
Flag of Netherlands image

The quickest way would be to an ORDER BY in the query already.
Avatar of jmingo
jmingo

ASKER

in the sql select statement?? what would it be??
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.
Avatar of jmingo

ASKER

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

SELECT * FROM tablename ORDER BY fieldname
Avatar of jmingo

ASKER

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.
Avatar of Roonaan
You can use:

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

-r-
Avatar of jmingo

ASKER

but there are lots of other fields i'm gathering also.... i just want to sort the $file variables.
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

Avatar of jmingo

ASKER

blue_hunter: thanks, i'll look into that.
Avatar of jmingo

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of blue_hunter
blue_hunter
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmingo

ASKER

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??
maybe add
array_pad() at the bottom of the scripts  to make it 5 elements

array_pad($filearr, 5, "");


Avatar of jmingo

ASKER

so put it here after this

$filearr = array_values($filearr);

array_pad($filearr, 5, "");

??
yes :)
Avatar of jmingo

ASKER

thanks... i'll accept your post anyways for all the help.
thanks you for the points too ;)