Adding spaces in between printed alphabet - php

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; }

Related

How to set a page break in MPDF

I am trying to create a page break in mPDF, but not succeeding, regardless of following every possible manual instruction, with the current attempt using this info:
https://mpdf.github.io/reference/html-control-tags/pagebreak.html
I am using mPDF version 8.0.2.
My current attempt looks like this:
function pdf_generate_table_html($data)
{
$page_length = 40;
$pages = ceil(count($data) / $page_length);
$keys = array_keys($data[0]);
$html = '';
$counter = 0;
for ($i = 1; $i <= $pages; $i++) {
$slice = array_slice($data, $counter, $page_length, true);
$html .= '<table style="width: 100%; border: 1px solid #dddddd;">';
$html .= '<thead>';
$html .= '<tr>';
foreach($keys as $key) {
$html .= '<th class="content" style="padding: 8px; background-color: #333333; color: #ffffff;">' . $key . '</th>';
}
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';
foreach($slice as $key => $entry) {
$html .= '<tr>';
foreach($entry as $value) {
$html .= '<td class="content" style="padding: 8px;">' . $value . '</td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
$html .= '<pagebreak>';
$counter += $page_length;
}
return $html;
}
The following happens:
Without the page break and the code that slices the data into separate tables, the first page contains the header, second page contains ENTIRE table (and I want it split...).
With the page break, first page is blank, second page contains the header, third page contains 3 tables, correctly split, but resized and fit onto one page, fourth page is blank.
Important: Using <div> tags does not appear to be feasible for me, because I am using tabular data that may contain any number of columns, and I cannot find a way to force all columns to be the width of the widest item in a column.
Any idea what I am doing wrong, please?

How to add a PHP Variable into HTML code

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.

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.

apply different styles to different lines of text in PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get random value out of an array
I have code applying a random font to a div of text which is taken from the last 20 lines of a .txt file. I would like to apply a different random font to each line... any pointers?
<?php
$fonts = array("Helvetica", "Arial", "Courier", "Georgia", "Serif", "Comic Sans", "Tahoma");
shuffle($fonts);
$randomFont = array_shift($fonts);
$output = "";
$lines = array_slice(file("users.txt"), -20, 20);
foreach ( $lines as $line )
{
$output .= '<div style="font-family:' . $randomFont . '; margin-left: ' . rand(0, 60) . '%; opacity: 0.8;">' . $line . '</div>';
}
echo $output;
?>
Live demo here.
The code:
$fonts = array("Helvetica", "Arial", "Courier", "Georgia", "Serif", "Comic Sans", "Tahoma");
shuffle($fonts);
$output = "";
$lines = array();
for($i = 0; $i < 40; $i++) $lines[] = "line $i";
$i = 0;
foreach ( $lines as $line ) {
if($i == count($fonts)) {
shuffle($fonts);
$i = 0;
}
$output .= '<div style="font-family:' . $fonts[$i] . '; margin-left: ' . rand(0, 60) . '%; opacity: 0.8;">' . $line . "</div>\n";
$i++;
}
echo $output;
Randomize your fonts:
$Random = $fonts[rand(0, count($fonts) - 1)];
Been thinking about it and have a simpler solution.
<?php
$fonts = array ('font 1', 'font 2', 'font 3'); // 20 entries for the full set
shuffle ($fonts);
while ($font = array_pop ($fonts))
{
$output .= '<div style="font-family:' . $font . ';"></div>';
}
?>
This is obviously not an exact solution to the problem you posted above, and it's not intended to be. It's intended to provide an example of an approach for getting random values that are guaranteed to be unique. You should be able to incorporate the idea expressed here into your own code without too much difficulty.

Categories