creating a table from 1 to 100 in php nested in html - php

This is a practice question I am having a problem with. So far, what I got to create is a table using the following code. I need help to fix it, with explanation on what my mistakes are.
What I am trying to to do is pretty simple: generating numbers from 1 to 100 in a table using php nested in an html code
Each 10 numbers should be in an aligned row, then break after 10
| 1 | 2 | . . . | 10 |
|11 | 12| .... | 20 |
|21 | 22| .... | 30 |
.
.
.
|91| 92 | ... |100|
<table border="1">
<?php
for ($x=1; $x <= 100; $x++){
if($x%11 ==0) {
echo "<tr><td>" . "<br>" . "</tr></td>";
}
echo "<tc><td>" . $x . "</td></tc>" ;
}
?>
</table>
Also using a for statement for the loop with if statement nested in it for the break the row
Could someone point out my mistakes in this code please.
all right the correct code should be like this:
<table border="1">
<?php
for ($x=1; $x <= 100; $x++){
if($x%10 ==1) {
echo "<tr>";
}
echo "<td>" . $x . "</td>" ;
}
?>
</table>
Thank you all for the help

We need to first understand the requirement.
Requirement:
Need incremental counter horizontally.
Every row should contain a whole 10 numbers starting from say 1 to 10, 11 to 20 etc.
So, if we consider the output as HTML table, <tr> increments by 11 and <td> increments by 1.
So, we need two loops: once incrementing by 10: the outer loop and other incrementing by 1: the inner loop.
So, write outer loop and inside it, add inner loop, both have step: 1
So, outer loop will increment the counter and inside it, inner loop will also run.
For every outer loop, the inner loop will run 10 times.
Thus, we get exact rows: 1-10, 11-20
Try this:
<table>
<?php
$k=0;
for ($i=0; $i<10; $i++) {
?>
<tr>
<?php
for ($j=0; $j<10; $j++) {
++$k;
?>
<td><?php print $k;?></td>
<?php
}
?>
</tr>
<?php
}
?>
</table>

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

<table>
<?php
for ($x=1; $x <= 100; $x++)
{
if($x%10 ==1)
**{
echo "<tr><td>" . "<br>" . "</tr></td>";**
}
echo "<tc><td>" . $x . "</td></tc>" ;
}
?>
</table>

Related

For loop array in php

Hello Guys how can i output this array using for loop?
This code is working but it is not for loop.
$a=array('Pol','Peter');
$b=array('3.2','2.79');
$c=array('1','1.4');
$values = array($a,$b,$c);
echo '<table border="1"><tr>';
echo '<td>'.$values[0][0].'</td>';
echo '<td>'.$values[1][0].'</td>';
echo '<td>'.$values[2][0].'</td>>';
echo '<tr><tr>';
echo '<td>'.$values[0][1].'</td>';
echo '<td>'.$values[1][1].'</td>';
echo '<td>'.$values[2][1].'</td>';
echo '</tr></table>';
Output:
--------------------------------
| Pol | 3.2 | 2.79 |
| Peter | 1 | 1.4 |
--------------------------------
Help me how loop this output.
Thanks in advance
You're looking for a for loop inside of a for loop.
The <table> tags should come outside of the outer loop, and the <tr> tags should come inside of the outer loop, but outside of the inner loop. Naturally, the <td> tags should come inside both.
This would look something like:
echo '<table border="1">';
for ($j = 0; $j < $c; $j++) {
echo '<tr>';
for ($i = 0; $i < count($values); $i++) {
echo '<td>'.$values[$i][$j].'</td>';
}
echo '<tr>';
}
echo '</table>';
Hope this helps! :)

Print one table, two-column PHP associative array as three side-by-side, two-column tables

I use the following to print a 15-row, two-column table from an associative arrray in PHP:
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
<?
$rowCount = 0;
foreach($word_frequency as $word => $frequency) if ($rowCount++ < 15) {?>
<tr>
<td><? echo $word; ?></td>
<td<? echo $frequency; ?></td>
</tr>
<?}?>
</tbody>
</table>
It works fine but takes up too much vertical space on the page. How can I format this into three side-by-side tables of five rows and two columns each with the first group having array items 1-5, the second group 6-10, and the last group 11-15? (refer to following illustration):
key1 value1 | key6 value6 | key11 value11
key2 value2 | key7 value7 | key12 value12
...
...
key5 value5 | key10 value10 | key15 value15
I've tried various table, div, container, and multiple loop experiments with very mixed (and unsuitable) results. Thank you in advance.
Since you can also use multiple columns to achieve this visual effect, you're going to probably want to format the data ahead of time to make the code of generating the table a little cleaner.
<?php
// Format the array data so each row contains all of the columns needed
$rows = array();
$max_per_column = 5;
$max_words = 15;
$rows = array_pad($rows, $max_per_column, array());
$count = 0;
foreach ($word_frequency as $word => $frequency) {
if ($count >= $max_words) {
break;
}
array_push($rows[$count % $max_per_column], $word, $frequency);
$count++;
}
?>
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
<?php
foreach ($rows as $cols) {
echo '<tr><td>' . implode('</td><td>', $cols) . '</td></tr>';
}
?>
</tbody>
</table>
Try some thing like that.
<table width="100%">
<tr>
<?php
$rowCount = 0;
foreach($word_frequency as $word => $frequency) if ($rowCount < 15) { ?>
<?php if($rowCount % 5 == 0) { ?>
<td><table border=1>
<?php } ?>
<tr>
<td><?php echo $word; ?></td>
<td><?php echo $frequency; ?></td>
</tr>
<?php if($rowCount % 5 == 4) { ?>
</table></td>
<?php } ?>
<?php $rowCount++; } ?>
</tr>
</table>
The only way to do this is to use a numeric key. For example
$word_frequency[0] = array();
$word_frequency[0]['word'] = "some word";
$word_frequency[0]['frequency'] = 10;
...
$word_frequency[15] = array();
$word_frequency[15]['word'] = "some other word";
$word_frequency[15]['frequency'] = 14;
Once you have your array, you could repeat the loop as follows
<table id="freq-table" class="table-list" cellspacing="0" cellpadding="10">
<tbody>
<?
$rowCount = 0;
for($i = 0; $i <= 5; $i++) {
$word[0] = $word_frequency[$i]["word"];
$frequency[0] = $word_frequency[$i]["frequency"];
$word[1] = $word_frequency[$i + 5]["word"];
$frequency[1] = $word_frequency[$i + 5]["frequency"];
$word[2] = $word_frequency[$i + 10]["word"];
$frequency[2] = $word_frequency[$i + 10]["frequency"];
?>
<tr>
<?
for($x = 0; $x <= 2; $x++ ){
?>
<td><? echo $word[$x]; ?></td>
<td<? echo $frequency[$x]; ?></td>
<?
}
?>
</tr>
<?
}
?>
</tbody>
</table>
Every $i loop creates the row, while the $x loop creates the columns. This assumes, of course, that you have at least 15 items, that you're going to create only 5 rows and three columns of key/value pairs.
Well, if I were doing it, I'd probably use a ul with a css columns :D
http://davidwalsh.name/css-columns
However, that's not a very "programmerly" thing to do.
It might help that you don't have to go through the array in order.
If you look at what you have, there is a pattern to the indices:
i + 0x, i + 1x, i + 2x, ...
Where x is your number of items divided by the number of columns.
and you stop iterating when i = total / x
Sorry to be too tired to code this for you but the gist is this:
$myarray = [1 ... 15];
$columns = 3;
$arrayKeys = array_keys($myarray);
$total = count($array);
$x = floor $total / $column;
for ($i = 0; $i > $x; $i += $x + 1 ){
echo $arrayKeys[$i] . " - " . $myarray($arrayKeys[$i]);
echo $arrayKeys[$i + $x] . " - " . $myarray($arrayKeys[$i + $x]);
echo $arrayKeys[$i + ($x * 2) ] . " - " . $myarray($arrayKeys[$i + ($x * 2)]);
}
Or something like that... that's off the top of my head and I think it has some issues, but you should be able to beat that into something that works.

PHP foreach with table rows

I have this PHP script here
$z = range(2, 123);
echo '<table width="100%">';
foreach($z as $x){
echo '<td><img src="http://www.mysite.net/' . $x . '/5.jpg" width=200></td>';
}
echo '</table>';
What I am trying to do is get 6 items in the table row and then a new row...how would I do this?
Thanks,
J
Here the modulo operator comes in. You can divide and calculate the remainder. Everytime the remainder is 0 you add a new row:
$z = range(2, 123);
echo '<table width="100%">';
echo '<tr>';
$cnt = 2;
foreach($z as $x){
if ( ($cnt - 2) % 6 == 0 ) {
echo '</tr><tr>';
}
$cnt++;
echo '<td><img src="http://www.mysite.net/' . $x . '/5.jpg" width=200></td>';
}
echo '</tr>';
echo '</table>';
If you want your codes nice and clean, always separate data manipulation from presentation code. Thus, move as much logic away from output, as possible.
So, first prepare your data
<?
$data = range(2, 123);
$data = array_chunk($data, 6);
?>
and then output it
<table width="100%">
<? foreach($data as $chunk): ?>
<tr>
<? foreach($chunk as $val): ?>
<td>
<img src="/<?=$val?>/5.jpg" width=200>
</td>
<? endforeach ?>
</tr>
<? endforeach ?>
</table>
Try something like this:
$z = range(2, 123);
echo '<table width="100%"><tr>';
foreach($z as $x){
if( ($x - 2) % 6 == 0 ) // Minus 2 because you don't start at 0 but at 2.
{
echo '</tr><tr>';
}
echo '<td><img src="http://www.mysite.net/' . $x . '/5.jpg" width=200></td>';
}
echo '</tr></table>';
The % is a Math function called modulo. More info can be found here.
Use a counter variable.
$z = range(2, 123);
$current_result=0;
echo '<table width="100%">';
echo '<tr>';
foreach($z as $x){
if ($current_result++ % 6 == 0) echo '</tr><tr>';
echo '<td><img src="http://www.mysite.net/' . $x . '/5.jpg" width=200></td>';
}
echo '</tr>';
echo '</table>';`
Well, for a start range(2, 123) is going to produce 122 numbers, so what you want is range(1, 6).
Then you need to fix your script to enclose each table cell in a row element, so your script becomes:
$z = range(1, 6);
echo '<table width="100%">';
foreach($z as $x){
echo '<tr><td><img src="http://www.mysite.net/' . $x . '/5.jpg" width=200></td></tr>';
}
echo '</table>';
Range is a bit of an oddball way of looping 6 times though - you might want to just use a string to build up the table and loop using while, then output the whole table in 1 go.
Edit
Seems I may have missed a point - if you are trying to loop 6 times within the range, then the modulo approach is what you want, although my point about the table row element is still needed otherwise you're not producing valid HTML.
just use any variable for count inside cycle. Increment it and check for you value.
For example
$cnt++;
if ($cnt >= 6) {
$cnt = 0;
echo "</tr><tr>";
}
And don't use foreach. Use for from 2 to 123

Change for in PHP with table

<table><tr>
<?php
for($i=0;$i<15;$i++) {
if($i%5 == 0) {echo '</tr> <tr>';}
?><td><?php echo $i ?></td>
<?php
}?>
</tr>
</table>
this generate:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
how can I make:
0 3 6 9 12
1 4 7 10 13
2 5 8 11 14
?
You need a nested loop
<table>
<?php
$rows = 3;
for($i=0 ; $i < $rows ; $i++ ) {
echo "<tr>";
for( $j = 0 ; $j < 5 ; $j++ ) {
echo "<td>" . ($j * $rows + $i) . "</td>";
}
echo "</tr>";
}
?>
</table>
I tried to make the variable names descriptive:
<table>
<?php
// Set the number of rows, cols, and starting number here:
$number_rows = 3;
$number_cols = 5;
$starting_num = 0;
// You can use foreach w arrays... much easier
$rows = range(0,$number_rows - 1);
$cols = range(0,$number_cols - 1);
foreach($rows as $one_row) {
?>
<tr>
<?php
foreach($cols as $one_col) {
// Do the calculation
echo "<td>" .
($starting_num + $one_col + ( max($rows) * $one_col ) + $one_row) .
"</td>";
}
?>
</tr>
<?php
}
?>
</table>
Working example like in the OP.
Now let's say you want to go from (1300-1431), then you start at 1300 and want a 4 x 8.
$number_rows = 8;
$number_cols = 4;
$starting_num = 1300;
Like this.
There are two "tricks."
The first is using range() to quickly define an array integers. I find arrays more pleasant to work with than raw numbers, since arrays can be worked on with the intuitive construct of foreach().
The second is figuring out how to know what number to print if you have the column, row, and starting, number. It's simplest to figure out the numbers for the first row and go from there. Let's number the rows and cols from 0. So col:0 row:0 will be the starting number. Col:1 Row:0, the number to the right, will just be the starting number + one (the column number) + the number of rows less one (easiest to see by looking at the matrix of number), and the number of rows less one is the maximum number in the rows array. So for the first row we have:
$starting_num + $one_col + ( max($rows) * $one_col )
then all we just add the row number to take that into account, and we've got it all:
$starting_num + $one_col + ( max($rows) * $one_col ) + $one_row
Tested example with given start and end:
<table>
<?php
$start = 1300;
$end = 1432;
$n = $end - $start + 1;
$cols = 5;
$rows = ceil($n / $cols);
for($i=0 ; $i < $rows ; $i++ ) {
echo "<tr>";
for( $j = 0 ; $j < $cols ; $j++ ) {
$val = $j * $rows + $i;
echo "<td>";
echo ($val < $n) ? $val + $start : ' ';
echo "</td>";
}
echo "</tr>";
}
?>
</table>

the problem with the listing. nested loops?

I am here to open a new line after every 6 from the data that I want to draw the line data.
When the 6-like following code prints the data 36 times.
that this function is checking how many games. If I mention below, but as there are now 36 with 6 printing units. Each one of the prints 6 times.
for($i = 0; $i < $db->oyunSayisi(); $i++)
{
if ($i % 6 == 0)
{
echo "<tr>";
}
?>
<br/>
<?php
foreach($db->oyunCek() as $oyun)
{
?>
<td width="224" height="115"><img height="115;110" src="<?=$db->siteAdres()?>/resimler/<?=$oyun['o_resim']?>" title="<?=$oyun['o_baslik']?> oyna" alt="<?=$oyun['o_baslik']?> oyna" /></td>
<?php
}
if ($i % 6 == 0)
{
echo "</tr>";
}
}
I think I see what may be causing the problem.
For every sixth item returned from oyunSayisi(), you want to create a table row that shows the data from oyunCek(). The problem is that the first modulus just outputs <tr>, then every line runs the foreach loop. Finally, the second modulus outputs </tr>. I think you want to combine everything into just one modulus, like this:
for($i = 0; $i < $db->oyunSayisi(); $i++)
{
if ($i % 6 == 0)
{
echo "<tr><br/>\n";
foreach($db->oyunCek() as $oyun)
{
?>
<td width="224" height="115"><img height="115;110" src="<?=$db->siteAdres()?>/resimler/<?=$oyun['o_resim']?>" title="<?=$oyun['o_baslik']?> oyna" alt="<?=$oyun['o_baslik']?> oyna" /></td>
<?php
}
echo "</tr>\n";
}
}
EDIT:
Upon further pondering, it did not make sense that you would want to only echo every sixth line of data... so it occurred to me that you are probably trying to create a new table row every sixth line, rather than skipping any of the inner foreach loops. Here is the modified code to do that:
echo "<tr>\n";
for($i = 0; $i < $db->oyunSayisi(); $i++)
{
foreach($db->oyunCek() as $oyun)
{
?>
<td width="224" height="115"><img height="115;110" src="<?=$db->siteAdres()?>/resimler/<?=$oyun['o_resim']?>" title="<?=$oyun['o_baslik']?> oyna" alt="<?=$oyun['o_baslik']?> oyna" /></td>
<?php
}
if (($i + 1) % 6 == 0)
{
echo "</tr>\n<tr>\n";
}
}

Categories