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.
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.
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)
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.
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:
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)));
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.
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.