I made a script where it creates a pyramid using a php for loop and html table elements, but the pyramid isn't how I want it yet.
The code:
<?php
echo "<table width=400px";
echo "<tr>";
//Inner Loop
for ($x = 0; $x <= 8; $x++) {
//Outer loop
for ($z = 1; $z <= $x; $z++) {
echo "<td height=50px width=50px bgcolor=black></td>";
}
echo "\n";
echo "</tr>";
}
echo "</table>";
?>
Right now it looks like this:
But I want it to look like this:
Can someone help me with that?
Simply add cellspacing=0 to table
<?php
echo "<table cellspacing=0 width=400px";
echo "<tr>";
//Inner Loop
for ($x = 0; $x <= 8; $x++) {
//Outer loop
for ($z = 1; $z <= $x; $z++) {
echo "<td height=50px width=50px bgcolor=black></td>";
}
echo "\n";
echo "</tr>";
}
echo "</table>";
?>
Related
This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed last year.
I had a little problem, i cannot break the table because I'm looking for a 16 columns in these table to create a unicode table...
The aim is to find where i must break the line of the column as like as there:
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
enter image description here
You used the modulus in the first loop, so use it again in the second
print "</tr>";
print "<tr>";
for($i = 1; $i <= 50000; $i++){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>$i</td><td>$unicodeChar</td>\n";
if ( $i % $columnas/2 ) == 0 ) {
print "</tr><tr>\n";
}
}
print "</tr>\n";
Thank you very much for this help, i have been fixed!!
here is the code that i fixed, with your idea :)
´´´´
<?php
$columnas = 16;
$filas = 16;
$word= 0;
$contador = 0;
print "<table border=\"1\">\n";
print "<caption>ASCII</caption>\n";
print "<tbody>\n";
print "<tr>\n";
for ($j = 1; $j <= $columnas; $j++){
if ($j%2 == 1){
print "<th>Codigo</th>\n";
}elseif ($j%2 == 0){
print "<th>Valor</th>\n";
}
}
print "</tr>";
for ($i = 1; $i <= $filas; $i++){
print "<tr>";
for($i = 1; $i <= 50000; $i++,$contador){
$unicodeChar = "&#{$i}";
$contador--;
print "<td>" .$i. "</td>\n";
print "<td>".$unicodeChar."</td> \n";
if (($i % 8 ) == 0 ){
print "</tr><tr>";
}
}
print "</tr>\n";
}
print "</tbody>\n";
print "</table>\n";
?>
´´´´
Like Riggsfolly said but maybe divide the columns number by 2 before the modulo, because there are 2 TDs / iteration:
if ( $i % ($columnas/2) ) == 0 ) {
print "</tr><tr>\n";
}
Below is my code and it produces a table with 3 Columns and 6 Rows. I want the produced HTML syntax to stored the variable in PHP. Is there a way this is possible?
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($i=0; $i <= $col_count; $i++) {
echo "<tr>\n";
for ($j=0; $j <= $row_count; $j++) {
$p = $data2[$i][$j];
echo "<td>$p</td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
Try below both way codes it's work proper..!
<?php
//one way
echo '<table border ="1" style="border-collapse: collapse">';
for ($i=0; $i <= $col_count; $i++)
{
echo '<tr>\n';
for ($j=0; $j <= $row_count; $j++)
{
$p = $data2[$i][$j];
echo '<td>'.$p.'</td> \n';
}
echo '/tr>';
}
echo '</table>';
//second way(all html tag and string store in one php variable and after echo in last)
$tbl = '<table border ="1" style="border-collapse: collapse">';
for ($i=0; $i <= $col_count; $i++)
{
$tbl .= '<tr>\n';
for ($j=0; $j <= $row_count; $j++)
{
$p = $data2[$i][$j];
$tbl .= '<td>'.$p.'</td> \n';
}
$tbl .= '/tr>';
}
$tbl .= '</table>';
echo $tbl;
?>
A quick and easy solution would be to use output buffering functions:
ob_start();
// echo your stuff here exactly as you are right now
$html = ob_get_clean();
Then you can echo $html (or <?= $html ?> if you're not in a PHP tag already) wherever / as many times as needed.
This should also be faster than using concatenation over and over again.
count value is changes on every refresh.
How to i store count value in variable and use in "for loop" over here $x <= 10;
<?php
echo "<div class='p_list_heading'> Product Name </div>";
echo "<div class='p_list_heading'> Product Price </div>";
echo "<div class='p_list_heading'> Product Quantity </div>";
echo count($_SESSION['product_name']);
for ($x = 0; $x <= 10; $x++) {
if($_SESSION['product_name'][$x]!==""){
echo "<div class=\"c_hold\">";
echo "<div>".#$_SESSION['product_name'][$x]."</div>";
echo "<div>".#$_SESSION['product_price'][$x]."Rs". "</div>";
echo "</div>";
}
}
?>
Store in some variable $y
$y = count($_SESSION['product_name']);
for ($x = 0; $x <= $y; $x++) {
if($_SESSION['product_name'][$x]!==""){
echo "<div class=\"c_hold\">";
echo "<div>".#$_SESSION['product_name'][$x]."</div>";
echo "<div>".#$_SESSION['product_price'][$x]."Rs". "</div>";
echo "</div>";
}
}
Instead of storing in variable, you can also do:
for ($x = 0; $x <= count($_SESSION['product_name']); $x++) {
if($_SESSION['product_name'][$x]!==""){
echo "<div class=\"c_hold\">";
echo "<div>".#$_SESSION['product_name'][$x]."</div>";
echo "<div>".#$_SESSION['product_price'][$x]."Rs". "</div>";
echo "</div>";
}
}
Please try following:
$count = count($_SESSION['product_name']);
echo $count;
for ($x = 0; $x <= $count; $x++)
{
}
I'm a beginner at PHP and I have the following issue: I need to create a 5x5 table in PHP like this:
This is what I have so far:
echo '<table border="1" style="width:100px">';
for ($i=0; $i < 5; $i++) {
echo "<tr>";
for ($j=0; $j < 5; $j++) {
echo "<td>";
echo $j;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
Change echo $j; to:
echo ($i*5) + $j + 1;
I new to PHP. I'm trying to make an Incidence of Coincidence (IC) calculator from bits and pieces of code that i've patched together from other peoples work. It's all working fine except for the part where it is posted to table. I want it to stop making new cells after 50.
I've been looking at it for hours and have tried several other methods but i've confused myself. Any help please?
Here's the code that's writes the table:
<?php
if ( $showTable ) {
// display tabulated values
echo "<table class=\"trans\" width=\"1000\" border=\"1\" cellpadding=\"0\">";
for ( $i = 1; $i < 50; $i++ ) {
echo "<tr>";
for ($c=0; $c <$cols; $c++)
{
echo "<td>";
echo($cells.': '.round($history[$i+$c], 3));
$cells++;
echo "</td>";
}
}
echo "</tr>";
$i += $c;
}
echo "</table>";
?>
If I understood you correctly,
<?php
if ( $showTable ) {
// display tabulated values
echo "<table class=\"trans\" width=\"1000\" border=\"1\" cellpadding=\"0\">";
for ( $i = 1; $i < 50; $i++ ) { // 50 rows will be created
echo "<tr>";
for ($c=0; $c <50; $c++) // each row will contain 50 <td>
{
echo "<td>";
echo($cells.': '.round($history[$i+$c], 3));
$cells++;
echo "</td>";
$i += $c;
}
echo "</tr>";
}
}
echo "</table>";
?>