How to add a PHP Variable into HTML code - php

So I am trying to make a multiplication table and have run into this issue :
<?php
$table = '';
if ($_POST) {
$table .= '<table border="1">';
for ($i = 0; $i < $_POST['qty_line']; $i++) {
$table .= '<tr>';
for ($j = 0; $j < $_POST['qty_column']; $j++) {
$val = $i * $j;
$table .= '<td width="50"> VARIABLE GOES HERE </td>';
}
$table .= '</tr>';
}
$table .= '</table>';
}
?>
Sorry if this is a very simple question but I can't find a solution after searching. Basically i want to insert the $val variable into where it says "VARIABLE GOES HERE" on that specific table data cell. How would I go about this?

You could use
$table .= '<td width="50">' . $val . '</td>';
See the documentation for more examples.

Related

How to calculate cumulative sum in PHP?

I want to calculate the sum and output a two columns table. But, the format is wrong. The numbers are all packed in one line.
<td>
<?php
for ($j = 1; $j <= 10; $j++)
{
print "<tr>";
print $j;
print "</tr>";
}
?>
</td>
<td>
<?php
$sum = 0;
for($i = 1; $i<=10; $i++)
{
print "<tr>";
print $sum = $sum + $i;
print "</tr>";
}
?>
</td>
Here, we would be writing one or two for loops, join our desired HTML string, and finally we would print it.
Test:
<?php
$html = '<tr>';
for ($j = 1; $j <= 10; $j++) {
$html .= "<td>" . $j . "</td>";
}
$html .= '</tr>';
$html .= '<tr>';
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i;
$html .= "<td>" . $sum . "</td>";
}
$html .= '</tr>';
print($html);
?>
We would be also adding table open and close tags (<table>, </table>):
<?php
$html = '<table>';
$html .= '<tr>';
$html .= "<td>Number</td>";
for ($j = 1; $j <= 10; $j++) {
$html .= "<td>" . $j . "</td>";
}
$html .= '</tr>';
$html .= '<tr>';
$html .= "<td>Cumulative Sum</td>";
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i;
$html .= "<td>" . $sum . "</td>";
}
$html .= '</tr>';
$html .= '</table>';
print($html);
?>
For two columns, we would just use one for` loop:
<?php
$html = '<table>';
$html .= "<tr><th>Number</th><th>Cumulative Sum</th></tr>";
for ($j = 1; $j <= 10; $j++) {
$html .= "<tr>";
$html .= "<th>" . $j . "</th>";
$sum += $j;
$html .= "<th>" . $sum . "</th>";
$html .= "</tr>";
}
$html .= '</table>';
print($html);
I ended up here when looking to get a cumulative sum in PHP. For the ones like me, I wrote the following function. It's safe for empty arrays as well.
function cumsum ($arr) {
return array_reduce($arr, function ($c, $i) { $c[] = end($c) + $i; return $c; }, []);
}

Adding spaces in between printed alphabet

I found this handy code in order to print the alphabet horizontally across the page. I would like to put a few spaces in between the letters since they are bunched really close together. Thanks for any help!
$alphabet = range('A', 'Z');
$table = '<table>';
for ($i = 0; $i < count($alphabet); $i++) {
$table .= '<td>' . $alphabet[$i] . '</td>';
}
$table .= '</table>';
echo $table;
If you don't want to use CSS, try adding a cellspacing attribute to your table.
$alphabet = range('A', 'Z');
$table = '<table cellspacing='10'><tr>';
for ($i = 0; $i < count($alphabet); $i++) {
$table .= '<td>' . $alphabet[$i] . '</td>';
}
$table .= '</tr></table>';
echo $table;
This adds 10 pixels between the cells.
You could also do this with CSS like so..
table tr td { margin-left: 5px; margin-right: 5px; }
Also, your code is not currently out putting a valid HTML table. I fixed that too ;)
You can do it across CSS
td { padding-bottom : 20px; }

How to add a header to a table, and how to add a border in php

I have been writing queries to get information from a table in php. I can print out the information, but it does not look pretty at all. I was wondering how I could make the tables that are outputted better by having black lines as the borders of each cell, and also how to add a column header to my tables. Right now my output for my first query looks like this:
Massachusetts 152082
Missouri 151580
Illinois 111454
And I want my output to look like this (I also want each cell to have a black border):
district population
Massachusetts 152082
Missouri 151580
Illinois 111454
Here is the code for when I print out my table. I dont think you will need any code from the queries so I wont post that. Thanks for the help in advance.
echo "<table>\n";
while($line = pg_fetch_array($result, null, PGSQL_ASSOC)){
echo "\t<tr>\n";
foreach($line as $col_value){
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
try this:
//table header
$table = "<table border='1px'>";
$table .= "<thead>";
$table .= "<tr>";
$i = pg_num_fields($result);
for ($j = 0; $j < $i; $j++) {
$fieldname = pg_field_name($result, $j);
$table .= "<th>$fieldname</th>";
}
$table .= "</tr>";
//table body
$table .= "<tbody>";
while($row = pg_fetch_assoc($result))
{
$table .= "<tr>";
foreach ($row as $key => $value)
{
$table .= "<td>$value</td>";
}
$table .= "</tr>";
}
$table .= "</tbody>";
$table .= "</table>";
//echo table
echo $table;

Building HTML table by appending to PHP string

First let me start by saying this is a homework problem so the way I have to build this table is because of the requirements for the assignment. The requirements call for three php functions to build an html table. I have a table, td, and tr function. I think I am very close to figuring out hwo to get this to work. Just running into one small problem and was wondering if someone could help me out or explain why I am running into this problem. Here is my code:
<?php
//need a constant to keep track of the number of rows
define('NUM_ROWS', '3');
//function to start building table
function table()
{
$myTable = '<table border="1">' . tr($myTable) . '</table>';
return $myTable;
}
//function to build table rows
function tr( $myTable )
{
$myTable .= '<tr>' . td($myTable) . '</tr>';
return $myTable;
}
//function to build table data
function td($myTable)
{
$rowData = array("Planes", "Trains", "Automobiles");
for($i = 0, $length = count($rowData); $i < $length; $i++)
{
$myTable .= '<td>' . $rowData[$i] . '</td>';
}
return $myTable;
}
echo table();
?>
The problem I am having is in the table function. If I write the function the way it is shown above it will display the table but it also gives me an undefined variable error on $myTable when I call the tr function. If I change that function to look like the function below it doesn't display my table at all and just shows a blank page.
function table()
{
$myTable = '<table border="1">';
tr($myTable);
$myTable .= '</table>';
return $myTable;
}
Any ideas on what I am doing wrong or can do differently?
You are not actually using the passed variable nicely anyways so just don't try to pass the variable (which you were passing before it existed and hence was complaining about being undefined); just do this:
<?php
//need a constant to keep track of the number of rows
define('NUM_ROWS', '3');
//function to start building table
function table()
{
$myTable = '<table border="1">' . tr() . '</table>';
return $myTable;
}
//function to build table rows
function tr()
{
$tr = '<tr>' . td() . '</tr>';
return $tr;
}
//function to build table data
function td()
{
$td = '';
$rowData = array("Planes", "Trains", "Automobiles");
for($i = 0, $length = count($rowData); $i < $length; $i++)
{
$td .= '<td>' . $rowData[$i] . '</td>';
}
return $td;
}
echo table();
?>
you need to pass
you aren't passing any parameter to function table
function table($myTable)
{
$myTable = '<table border="1">' . tr($myTable) . '</table>';
return $myTable;
}
Try this.
//need a constant to keep track of the number of rows
define('NUM_ROWS', '3');
//function to start building table
function table($val)
{
$myTable="";
$myTable = '<table border="1">' . tr($val) . '</table>';
return $myTable;
}
//function to build table rows
function tr( $val )
{
$myTable="";
$myTable .= '<tr>' . td($val) . '</tr>';
return $myTable;
}
//function to build table data
function td($val)
{
$myTable="";
for($i = 0, $length = NUM_ROWS; $i < $length; $i++)
{
$myTable .= '<td>' . $val[$i] . '</td>';
}
return $myTable;
}
$rowData = array("Planes", "Trains", "Automobiles");
echo table($rowData);

How can I find last <td> in table with PHP

I wonder how can i find out last in table with PHP, aka if that is last then i want to apply different style and content to it.
This is part of my code generating table content.
$content .= '</tr>';
$totalcols = count ($columns);
if (is_array ($tabledata))
{
foreach ($tabledata as $tablevalues)
{
if ($tablevalues[0] == 'dividingline')
{
$content .= '<tr><td colspan="' . $totalcols . '" style="background-color:#efefef;"><div align="left"><b>' . $tablevalues[1] . '</b></div></td></tr>';
continue;
}
else
{
$content .= '<tr>';
foreach ($tablevalues as $tablevalue)
{
$content .= '<td>' . $tablevalue . '</td>';
}
$content .= '</tr>';
continue;
}
}
}
else
{
$content .= '<tr><td align="center" style="border-bottom: none;" colspan="' . $totalcols . '">No Records Found</td></tr>';
}
I know there is option to do something like that with jQuery later on, but I don't want to complicate it and just solve it with php at time when i generate table.
Keep a count:
$count = 1;
foreach ($tablevalues as $tablevalue)
{
$count++;
if( $count > count( $tablevalues)) {
echo "Last td tag is up next! ";
}
$content .= '<td>' . $tablevalue . '</td>';
}
you can execute any code in last loop:
for($i=0;$i<count($tablevalues);$i++)
{
$tablevalue=$tablevalues[$i];
$content .= '<td>' . $tablevalue . '</td>';
if($i==count($tablevalues)-1){
// last loop execution
}
}
here is a somehow trick
foreach ($tablevalues as $k => $tablevalue)
{
if ($k==count($tablevalues)-1)
{
// last td of current row
$content .= '<td>' . $tablevalue . '</td>';
}
else
{
$content .= '<td>' . $tablevalue . '</td>';
}
}
I just hope that your keys of your $tablevalues set match this code.
You should use a for($i = 0; i < count($tablevalues); $i++) {} loop and consider count($tablevalues)-1 to be the last key of your array.
So that $tablevalues[count($tablevalues)-1] is your last <td>.
Why dont you use jQuery?It has provisions to do something like that.

Categories