Jump to content

[SOLVED] placing in order question


jamsearch

Recommended Posts

Hope someone can help me.

 

I am writing a program using php and mysql.

 

I have ten variables with number values set in each one. None of these numbers will ever equal the same value to one another.

(all will always have different values to one another)

 

for example:

 

$D01 = 444

$D02 = 474

$D03 = 644

$D04 = 447

$D05 = 488

$D06 = 554

$D07 = 1

$D08 = 44

$D09 = 1444

$D10 = 24

 

Here's the problem. I want to be able to output the variables in order from high to low. I have no problem figuring out the highest number or the lowest number but I'm baffled how to get the rest in line. I've been trying for days and nothing I come up with works. Is there a way of doing this? If I could use GOTO there would be no problem but without it ,it seems there would have to be thousands of lines of code to get the result I'm after. Any help would be greatly appreciated. Thanks. ???

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/80750-solved-placing-in-order-question/
Share on other sites

Are you familiar with arrays and sorting?  The simplest solution here is to put the values into an array and sort the array.

 

For example

 

$D[1] = 444;
# ...
$D[10] = 24;
rsort($D);
foreach ($D as $num) {
  print "$num\n";
}

 

Manual for rsort()

Yeah, arrays are the way to go for sorting. You could easily transform you variables to an array like:

 

<?php
error_reporting(E_ALL);
$D01 = 444; 
$D02 = 474;
$D03 = 644; 
$D04 = 447; 
$D05 = 488; 
$D06 = 554; 
$D07 = 1; 
$D08 = 44; 
$D09 = 1444; 
$D10 = 24; 
$D = array();
for($i=1;$i<=10;$i++){
$D[$i] = ${'D'.str_pad($i,2,0,STR_PAD_LEFT)};
}
rsort($D);
echo '<pre>'.print_r($D,1).'</pre>';
?>

 

Though it would be better to put them in an array to start with.

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.