Creating a simple table with unique ID


#1

I need a code to generate simple table with unique table ID from the mysql table, and this table should look like below. and it should be generated on the page as many as the number of IDs in the table.

							49
6	12	18	24	30	36	42	48
5	11	17	23	29	35	41	47
4	10	16	22	28	34	40	46
3	9	15	21	27	33	39	45
2	8	14	20	26	32	38	44
1	7	13	19	25	31	37	43

I have a sample code bu it is not exactly what I need.

<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
	for ($row=1; $row <= 6; $row++) { 
		echo "<tr> \n";
		for ($col=1; $col <= 8; $col++) { 
		   $p = $col * $row;
		   echo "<td>$p</td> \n";
		   	}
	  	    echo "</tr>";
		}
		echo "</table>";
?>

`
I hope this is simple, and someone can help me. thanks!


#2

so you need a range that decreases from 7 to 1. Within every loop you need a range from $i to 42 with an offset of $i and a step of 6

<?php

foreach(range(7, 1) as $i){
    echo implode(' ', range($i, 42 + $i, 6)), "\n";
}

you can put this within an 2 dimensional array and build a table upon this, with some modifications.


#3

thanks, chorn. I will try that. but I also need to give a unique ID to each table. something like this:<table Id="1" > , <table Id="2">, etc.. the ids should be taken from the db table.


#4

Anothing thing which I find challenging, how can i put "49" on the right top of table.


#5

The 49 already is on the "right top" of the table, you just have to skip every other number within the row - or you just start with a row that contains the 49 only and append every other number with the looping.

And as you have received the ID from the database (or all with iterating over them in a loop) you can just assign them like every other string. (echo or concatenation)


#6

I did this, but I just got the first column right, the rest didn't come out as expected. Where am I doing wrong here?

<?php

echo "<table border =\"1\" style='border-collapse: collapse'>";

foreach(range(7, 1) as $i){
	
    echo implode(' ', range($i, 42 + $i, 6)), "\n";
	echo "<tr> \n";
    echo "<td>$i</td> \n";
		   	
	  	    echo "</tr>";
	}

		echo "</table>";

?>

#7

You are only building up one cell from the row. You have to also iterate over the range (remove the implode) to get columns.

Or, as i said, you split up the tasks in prepareing a two dimensional array with the numbers at the right position - and from there you take a generalized function that converts a two dimensional array into an html table.


#8

The trick with this kind of question is that the flow of table you're looking for is not logical to the brain so it has a hard time wrapping around it. What makes this kind of problem a lot easier is to first convert it something your brain does understand, and then modify it to the wanted result.

So instead of building the table in one go, start with an array that looks like this:

 1  2  3  4  5  6 
 7  8  9 10 11 12 
13 14 15 16 17 18 
19 20 21 22 23 24 
25 26 27 28 29 30 
31 32 33 34 35 36 
37 38 39 40 41 42 
43 44 45 46 47 48

which is a lot easier to do, and then transpose it (switch columns and rows), and reverse the order of the rows to obtain

 6 12 18 24 30 36 42 48 
 5 11 17 23 29 35 41 47 
 4 10 16 22 28 34 40 46 
 3  9 15 21 27 33 39 45 
 2  8 14 20 26 32 38 44 
 1  7 13 19 25 31 37 43

Which in my opinion is a lot easier to follow.

demo here

Only the number 49 is missing. I'll leave that as an exercise to the reader :slight_smile:


#9

Thanks a lot, ScallioXTX. You brought a different dimension to the problem which is beyond my understanding, and left a tough exercise to achieve without which I wont' be able to use it :).

I paste your code here for future reference just in case the link may get outdated or deleted somehow.

function printTable(array $table) {
  foreach ($table as $row) {
    foreach ($row as $column) {
      echo str_pad($column, 2, ' ', STR_PAD_LEFT), ' ';
    }
    echo "\n";
  }
}

// which numbers go in the table, range(1, 49) creates an array [1, 2, ..., 49]
$ids = range(1, 49);
// number of colummns that should be in the table
$columns = 8;

$itemsPerColumn = floor(count($ids) / $columns);

$array = [];
for ($i = 0; $i < $columns; $i++) {
  $array[$i] = array_slice($ids, $i * $itemsPerColumn, $itemsPerColumn);
}

echo '== Initial table ==', "\n\n";
printTable($array);
echo "\n\n";

echo '== Table after transposing ==', "\n\n";
printTable(array_reverse(array_map(null, ...$array)));

which gives this result:

PHP 7.0.8 (cli) (built: Jun 23 2016 23:39:14) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
   
== Initial table ==

 1  2  3  4  5  6 
 7  8  9 10 11 12 
13 14 15 16 17 18 
19 20 21 22 23 24 
25 26 27 28 29 30 
31 32 33 34 35 36 
37 38 39 40 41 42 
43 44 45 46 47 48 


== Table after transposing ==

 6 12 18 24 30 36 42 48 
 5 11 17 23 29 35 41 47 
 4 10 16 22 28 34 40 46 
 3  9 15 21 27 33 39 45 
 2  8 14 20 26 32 38 44 
 1  7 13 19 25 31 37 43

#10

This smells like an XY Problem. OP, what is the REAL problem you are trying to solve? I dont mean your attempt at solving it.


#11

Thanks for your interest, benanamen. I just know a little of PHP and mysql. This is a lotto slip. and I want to create a table of 49 numbers as seen above, and this table should be repeated on the page as many as necessary depending on the number of the draws in the database. And one more thing, each table should have a unique which will come from the mysql table. This is basically what I need on the php side.


#12

Why use 8 columns then? If you use 7 you get a nice 7x7 square grid.


#13

Interesting point. I didn't think about it actually, since this is the way of balls lined up, so I wanted to have it like that. but if 7x7 is easier, that'd be ok too. it's not a big deal.


#14

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.