Is there a way to distribute text over several columns?
I have a table with three td’s. The text is loaded by php. I want to start printing the text in td1 and use td2 as spacing. When td1 is full I want the text to continue in td3 and so on.
How can I to do that?
Is there a way to make the rest of the text “jump” to another td when the first is full?
Ok, very hasty… and not good for enormous blocks of text, but it works.
It will accept text only as it counts the number of words by spaces. There is probably a more efficient way to do it, but if you need a quick fix:
// Fill a variable with some text
$someText = "some test text for the columns";
// Set the number of Cells
$numCells = 3;
// Count the spaces to determine how many words there are.
$wordArray = explode(" ", $someText);
$countWords = sizeof($wordArray) + 1;
// Find out how many words to put into each cell
$wordsPerCell = floor($countWords/$numCells);
// Rejoin words
$count = 1;
$currentCell = 1;
$newStringArray = array();
foreach ($wordArray as $key=>$word)
{
// Initialise string for population
if (!isset($newStringArray[$currentCell]))
{
$newStringArray[$currentCell] = "";
}
// Perform string concat into array for each cell
$newStringArray[$currentCell] = $newStringArray[$currentCell].$word." ";
// Check count against max words in each cell
if ($count < $wordsPerCell)
{
// increment count
$count ++;
} else {
// Go to next cell, reset word count
$count = 1;
$currentCell ++;
}
}
// trim last space off the final cell data
$newStringArray[$numCells] = substr($newStringArray[$numCells], 0, -1);
// Output table
echo "<table width=\"100%\"><tr>";
for ($i = 1; $i <= $numCells; $i++)
{
echo "<td>".$newStringArray[$i]."</td>";
}
echo "</table></tr>";