Jump to content

Beginner Question


Solence

Recommended Posts

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="stylesheet.css">
<meta charset="utf-8">
</head>
<body>


<?php
	$array = array();
		echo "<table>";
		for ($z=0; $z <=9; $z++)
		{		
			echo "<tr>";
			for ($s=0; $s <=9; $s++)
			{
			$r = mt_rand (1, 100);
			array_push ($array, $r);
				if ($r %2 != 0)
				{
					echo "<td bgcolor = red>";
					echo $r;
					echo "</td>";
				}
				else
				{
					echo "<td bgcolor= lightgreen>";
					echo $r;
					echo "</td>";
				}
			}
			echo "</tr>";
		}
		echo "</table>";						
		echo "<br>";
		echo "<table>";
		
		sort($array);
		$arraysplit = array_chunk($array, count($array)/10);
		for ($z=0; $z <=9; $z++)
		{		
			echo "<tr>";
			for ($s=0; $s <=9; $s++)
			{
				if ($r %2 != 0)
				{
					echo "<td bgcolor = red>";
					echo json_encode (arraysplit);
					echo "</td>";
				}
				else
				{
					echo "<td bgcolor= lightgreen>";
					echo json_encode ($arraysplit);
					echo "</td>";
				}
			}
			echo "</tr>";
		}
?>
</body>
</html>

Hey absolute beginner here.

Im working on a table that gives random numbers out and then colorizes them if they can be devided by 2. That works fine.

Now i want to get those randomly generated numbers into a second table but sorted. I already managed to put the numbers i generated into an array and sort it but i cant figure out how to echo them 1 by 1 into the table. Maybe you can help me out.

Link to comment
Share on other sites

Create a function which outputs data from an array into a table (You do it with the first table so you have some idea how to do it)

  • Generate random array
  • call function to output it
  • sort the array
  • call same function to output it

Your html markup is obsolete. I see you have a css file - use it.

Link to comment
Share on other sites

in the first table im generating a random number with each loop which i then put into an array for the second table. So the array is already done. I have no idea how i output an array into a table. At least not the way i want. I only manage to get ether a table with the full array in each field or the same single value of the array in each field.

Right now i tried to play around with

foreach ($array as &$value);

but this just shows me the same number in each field again. I only started a few days ago with php so i barely understand my own code.

Link to comment
Share on other sites

fyi - my solution

<?php

// generate the array
$data = [];
for ($i=0; $i<100; $i++) $data[] = mt_rand(1, 99);

// output array into table, sort, and output again
$t1 = array2table($data);
sort($data);
$t2 = array2table($data);

function array2table(array $arr) 
{
    $result = "<table border='1'>\n";
    $rows = array_chunk($arr, 10);                            // split the array into rows of 10 numbers each
    foreach ($rows as $cols) {                                // process each row
        $result .= '<tr>';
        foreach ($cols as $c) {                               // process each column in the current row
            $cls = $c % 2 ? 'odd':'even';                     // apply the appropriate class
            $result .= "<td class='$cls'>$c</td>";
        }
        $result .= "</tr>\n";
    }
    $result .= "</table>\n";                                  // return the table
    return $result;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example</title>
<style type='text/css'>
    table {
        border-collapse: collapse;
        margin: 20px;
        display: inline-block;
        font-family: calibri, sans-serif;
        font-size: 12pt;
    }
    td {
        width: 20px;
        height:20px;
        padding: 2px;
        text-align: center;
    }
    .odd {
        background-color: #FF8080;
    }
    .even {
        background-color: #C0FFC0;
    }
</style>
</head>
<body>
    <?=$t1?> 
    <?=$t2?>
</body>
</html>

 

Capture.PNG

Link to comment
Share on other sites

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.