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.
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";
}
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>";
?>
I am trying to print an array so every 6th element is on a new line. I'm trying to do this in a table. I would prefer if it could be answered in PHP, but Javascripts Ok.
$array_control = array(
"1","5","6","2","1",
"2","1","6","4","3",
"3","2","5","6","6",
"4","3","1","5","4",
"6","4","2","3","6"
);
$arrayLength = count($array_control);
$i = 0;
while ($i < $arrayLength)
{
if ($i==0) {
echo "<tr>";
}
if (is_int($i/5)) {
echo '<td style="width:16%;">'.$array_control[$i].'</td>';
echo "</tr>";
echo "<tr>";
}else{
echo '<td style="width:16%;">'.$array_control[$i-1].'</td>';
}
$i++;
}
Any help would be great!
The %-operator (modulus) solves this for you:
echo '<table><tr>';
for($i = 1; $i <= count($array_control); $i++ ){
echo '<td style="width:16%;">';
if( ($i % 6) == 0){
echo $array_control[$i-1] . '</td></tr><tr>';
} else{
echo $array_control[$i-1] . '</td>';
}
}
echo '</tr></table>';
or if you feel sassy:
echo '<table><tr>';
for($i = 1; $i <= count($array_control); $i++ ){
echo '<td style="width:16%;">' . $array_control[$i-1], ( ($i % 6) == 0 ) ? '</td></tr><tr>' : '</td>';
}
echo '</tr></table>';
A for loop seems better, and maybe the mod operator is more clear.
echo '<tr>';
for ($i < = 0; i < $arrayLength; ++ $i) {
echo '<td style="width:16%;">'.$array_control[$i].'</td>';
if ($i % 6 == 5) {
echo '</tr>';
echo '<tr>';
}
}
echo '</tr>';
This isn't quite ideal, because you'll get an empty '' at the end, but the browser should hide that. A little more logic can be added if you need to eliminate that too.
Further, with the array structure you're showing, you could eliminate the $arrayLength variable entirely, and instead use:
foreach ($array_control as $i => $val) {
echo '<td style="width:16%;">'.$val.'</td>';
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 need to generate a simple table with dynamically coloured <td>'s based on mysql retrieved values.
Here is what I need:
$target = 20;
$achieved = 13;
echo "<table border='1'><tr>";
for($i = 0; $i <= $target; $i++){
echo "<td></td>";
}
echo "</tr></table>";
The above code plots a table with no of <td>'s equal to $target. All I need is then to color the background of these <td>'s with the value inside variable $achieved. So in this case I want 13 colored <td>'s.
maybe you can try this, and add a td.coloured on your css
$target = 20;
$achieved = 13;
echo "<table border='1'><tr>";
for($i = 0; $i <= $target; $i++){
if ($i < $achieved) {
echo "<td class=\"coloured\"></td>";
}
else {
echo "<td></td>";
}
}
echo "</tr></table>";
You should be able to use a standard if statement here, something similar to:
if ($i < $achieved) // do the color
Inside the loop, you can populate a $bgcolor variable and then append that into the <td> you're outputting:
for($i = 0; $i <= $target; $i++){
$bgcolor = '';
if ($i < $achieved) {
// give it a red background color
$bgcolor = ' bgcolor="#ff0000"';
}
echo "<td" . $bgcolor . "></td>";
}
If you want more advanced styles, I'd suggest going with CSS instead of the bgcolor attribute. The same approach can be taken as above:
for($i = 0; $i <= $target; $i++){
$style = '';
if ($i < $achieved) {
// give it a red background color
$style = ' style="td-acheived"';
}
echo "<td" . $style . "></td>";
}
And then you could have the style:
<style>
.td-acheived {
background-color: #ff0000;
}
</style>
i would do this by combining it with css with the following change to your php
$target = 20;
$achieved = 13;
echo "<table border='1'><tr>";
for($i = 0; $i <= $target; $i++){
echo "<td class='bgcolor-".$achieved."'></td>";
}
echo "</tr></table>";
then in your css file
.bgcolor-13{
background-color:blue;
}
I'm not sure if i understand well but i'll try.
$target = 20;
$achieved = 13;
echo "<table border='1'><tr>";
for($i = 0; $i <= $target; $i++){
if($i<=$archieved){
echo "<td bgcolor='red'></td>";
}else{
echo "<td></td>";
}
}
echo "</tr></table>";