PugJr Posted April 9, 2009 Share Posted April 9, 2009 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 More sharing options...
mike12255 Posted April 9, 2009 Share Posted April 9, 2009 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 https://forums.phpfreaks.com/topic/153271-solved-how-to-arrange-an-array/#findComment-805203 Share on other sites More sharing options...
PugJr Posted April 9, 2009 Author Share Posted April 9, 2009 Alright, I get what you mean, pretty well. I'll try that out! Thanks. Link to comment https://forums.phpfreaks.com/topic/153271-solved-how-to-arrange-an-array/#findComment-805207 Share on other sites More sharing options...
mike12255 Posted April 9, 2009 Share Posted April 9, 2009 <?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 https://forums.phpfreaks.com/topic/153271-solved-how-to-arrange-an-array/#findComment-805208 Share on other sites More sharing options...
PugJr Posted April 9, 2009 Author Share Posted April 9, 2009 Yeah, I think I understand. I'll work off that. Thanks! Link to comment https://forums.phpfreaks.com/topic/153271-solved-how-to-arrange-an-array/#findComment-805210 Share on other sites More sharing options...
mike12255 Posted April 9, 2009 Share Posted April 9, 2009 well anyway its 12:40 am i got to get some sleep, if you have any questions just ask and ill try to answer them when i get back if somone else hasnt already. Link to comment https://forums.phpfreaks.com/topic/153271-solved-how-to-arrange-an-array/#findComment-805211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.