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
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

Link to comment
Share on other sites

<?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??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.