How do I sort this loop? I can't sort it because I just began learning PHP, so I don't know as much as most of you users... Can anyone help me?

<?php
    $red = array();
    $x=0;
    $min=500;
    $max=2000;

    echo "<table border=\"1\">";
    for($row=1;$row<=20; $row++){

                    echo "<tr>\n";
                    //Need table ONE to have 20 by 20 rows & columns 
                    //Need the table to contain numbers between 900 - 2000 (Randomly)
                    //Table two will take table ONE and sorts it from Smallest to Biggest
                    //Table three will take table ONE and sorts it from Biggest to Smallest
                    //Table 4 will only show a (*) net to the Odd Numbers and (#) next to the Even Numbers 

                      for($col=1;$col<=20; $col++){
                          $x=rand($min,$max);
                                   $red[$row][$col] = $x;
                                   $blue[$row][$col]=$table[$row][$col];

                                     echo"<td>$x</td>\n"; 
                                                     }
                       echo "</tr>";
                                  }
    echo "</table>"; 




$blue= array();
echo "<table border=\"1\">";

for($row=1;$row<=20; $row++){
    echo "<tr>\n";

for($col=1;$col<=20;$col++){
    $blue[$row][$col] = $y;
    echo"<td>$y</td>\n";
    }
echo "</tr>";
}
echo "</table>"; 
?>

Basically what is in the comments inside the code is what I need to achieve... I unfortunately can't, so please help me!!!

Recommended Answers

All 5 Replies

Is this a homework assignment? If so, we will not just give you the answer. I will, however, help you find the answer yourself.

You will need the functions range(), shuffle(), and sort(). Look these up on php.net.

No, this isn't for a class of any sort... this is just me trying to figure out different ways of manipulating arrays...

Apparently I want to use a loop to sort these... you know how to set this up?

Thanks for the quick response!

Pretty much you need to make an array filled with the numbers from 500-2000, then shuffle the array and display it using a loop. For the other tables, you run the array through a sort function to sort ascending/descending and display the same way you did above.

Can you write me a couple of lines on how to do it? Apparently, I have messed up numerous times today in making this work... do you mind helping?

Thanks again!

Here is part of the code to get you started. In this case, I hate just giving code, but it is usually easier to show you.

<?php

$numbers = range(900,2000);
shuffle( $numbers );

//loop through $numbers and display in table

$numbers_asc = $numbers_desc = $numbers;

asort( $numbers_asc,SORT_NUMERIC );

//loop through $numbers_asc and display in table

arsort( $numbers_desc,SORT_NUMERIC );

//loop through $numbers_desc and display in table

?>

There might be some errors in there, I wrote it pretty quickly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.