Jump to content

[SOLVED] How to arrange an array?


PugJr

Recommended Posts

Now below would withdraw all data from table and repeat echoing it until it ran out of rows. Now how would I do it so it arranges numerical (assuming $var is always a number). If I do a bad job at explaining like this is what would normally happen:

 

Assuming this is my table content:

 

ID var

1  3

2 7

3 2

4 5

 

I would get with what I currently have:

 

3

7

2

5

 

But which I want is:

 

2

3

5

7

 

I would prefer if it would be possible to get a hint on how to solve this or a guide. I'd prefer not having the direct answer as then I learn little. Thank you.

 

 

$query = "SELECT * FROM table" or die();

 

$result = mysql_query($query) or die();

 

 

while($row = mysql_fetch_array($result)){

 

 

 

$var = $row['var'];

 

echo "$var <br>";

}

 

Link to comment
https://forums.phpfreaks.com/topic/153271-solved-how-to-arrange-an-array/
Share on other sites

not direct answer eh? well what i would do is set up a for loop to run through all the values.

 

Then set var i = to your first value and test to see if its less then the other and if it is then leave it else the other var takes its place ill try to type up a small example before i pass out

<?php
for ($i, $i < 4, $i++){

for ($r,$r<4,$r++){
if($row[$i] < $row[$r]){
//then nothing happens because the lower number is higher
}else{
//set the var in row'i' to a temp cause its about to get over written
$temp = $row[$i];
//overwrite the var at i
$row[$i] = $row[$r];
//replace the var at r with the temp variable
$row[$r]=$temp;
        }
   }
}
?>

 

so obviously this wouldnt work if u just took this and extracted it into your code im not that nice :) but i hope you can get the point from it??

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.