0

Dear DANIWEB community members,

I have a question about sorting a label, and I've tried so hard in figureing it out but I realized that I am doing something wrong but not sure what... What I am doing is sorting the numbers, which is created by the loop, that populate the array...

The first table shows all the numbers in a random order

The second table displays the numbers in order and the numbers are the ones that come from table 1.

Anyone have a clue on how to do it?

My code may have errors but can someone please help me with this

<?php
$red = array();
    $x=0;
    $min=900;
    $max=2000;
    echo "<table border=\"1\">";
    for($row=1;$row<=20; $row++){
                    echo "<tr>\n";


                      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>";
                                  }

sort($red);
            foreach ($red as $key=> $value) {
                echo $value. "<br>\n";           
    echo "</table>"; 
echo "<table border=\"1\">";
    $red[$row][$col] = $x;
    echo"<td>$y</td>\n";
    }
echo "</tr>";
echo "</table>"; 
?>



<?php
sort($red);
foreach ($red as $key => $value) {
echo $value. "<br>\n";
}
?>

Please take your time and help me! If this is unclear please tell me so I can be clearer!
Thanks!,
Sedique

2
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by broj1
1

In the above code the variables $table and $y are undefined. What is the purpose of those?

Also you should not put the </table> tag within the foreach loop (see line 23). I am still looking at your code so more follows.

Edited by broj1

1

OK, I've done a bit of juggling and came up with the code below. Please note since you create a two-dimensional array you have to copy it to a one-dimensional temporary array so you can use the sort php function which works on one-dimensional arrays. See the comments in the code. I have renamed the variables you used but you can change them. Hope this is what you intended to get help with. If not then give more explanation.

<?php

$x=0;
$min=900;
$max=2000;

// initialize array of random values
$random_array = array();

// initialize temporary array for sorting
$temp_array = array();

// start html table for random values
echo "<table border=\"1\">";

// create $random_array with random numbers, display html table rows and
// store each value in onedimensional $temp_array for sorting
for($row=1;$row<=20; $row++){

    echo "<tr>\n";

    for($col=1;$col<=20; $col++) {

        $x=rand($min,$max);
        $random_array[$row][$col] = $x;

        echo"<td>$x</td>\n"; 

        // add value to the $temp_array
        $temp_array[] = $x;
    }

    echo "</tr>";
}

// end the html table
echo "</table>";

// sort the $temp_array (sort works on onedimensional arrays)
sort($temp_array);

// start a table for sorted values
echo "<table border=\"1\">";

// initialize counter that will be the $temp_array's key
$key = 0;

// display html table rows of sorted values
for($row=1;$row<=20; $row++){

    echo "<tr>\n";

    for($col=1;$col<=20; $col++) {

        echo"<td>{$temp_array[$key]}</td>\n";

        // increase the key
        $key++; 
    }

    echo "</tr>";

}

// end the html table of sorted values
echo "</table>";

?>
0

You don't realize how much you've helped me! You solved my biggest problem and I'd like to thank you for that! Those errors I made are stupidity of mine I guess and I think I was experimenting...

But I came up with a question, is there other ways in sorting a loop or maybe a better way and a way I shouldn't do it?

Other than that question, you really helped me through this one!
I truly thank you and wish you the best of luck in computer programming! Hopefully, I'll be as good as you in maybe about a 2-3 years!

Thanks!
Sedique

0

Oh a got another question... how do I make every even/odd number have for example: all evens are blue... and odds are red?

Do you know if that is possible?

And how come the table's won't separate when I insert break tags?

I know you helped me truly a lot but do you mind answering this last question????

Thank you for all your help!
Sedique

0

I'll be as good as you in maybe about a 2-3 years!

I'm not such an expert, it is just a practice and experience. If you do programming on regular basis you will get that in less than one year :-)

But I came up with a question, is there other ways in sorting a loop or maybe a better way and a way I shouldn't do it?

When it comes to sorting, there are basically two approaches:

  1. Save the data you want to sort in an array (like I did above) and sort the array with one of many array sort functions. There are plenty functions to use that sort by keys, do natural sort, case insensitive sort, multidimensional sort, reverse sort etc. See http://php.net/manual/en/array.sorting.php

  2. If you draw your data from a database then you can sort in your query using ORDER BY statement such as $query = "SELECT name, joindate FROM members ORDER BY name".

0

how do I make every even/odd number have for example: all evens are blue... and odds are red?

Do you mean every even/odd number or even/odd row in a table?

In both cases you use css style and apply it to table cells or rows. Here is the example for even/odd numbers:

// define the css styles
$style_even = {color: blue;};
$style_odd = {color: red;};

for($row=1;$row<=20; $row++){

    echo "<tr>\n";

    for($col=1;$col<=20; $col++) {

        $x=rand($min,$max);
        $random_array[$row][$col] = $x;

        // define style depending on whether the number is even or odd
        if($x % 2 == 0) {

            $style = $style_even;

        } else {

            $style = $style_odd;
        }

        // add style to the cell
        echo "<td style='$style'>$x</td>\n";

        // add value to the $temp_array
        $temp_array[] = $x;
    }

    echo "</tr>";
}

Edited by broj1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.