Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

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
0
jmingo
Asked:
jmingo
  • 9
  • 5
  • 2
  • +2
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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_hunterCommented:
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_hunterCommented:
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
 
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_hunterCommented:
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_hunterCommented:
yes :)
0
 
jmingoAuthor Commented:
thanks... i'll accept your post anyways for all the help.
0
 
blue_hunterCommented:
thanks you for the points too ;)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now