Distributing text over columns

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?

:disco:

I think I might be able to write up a little bit of hasty code for this one…

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

thanks!

hmm… your code distrubutes text over several columns indeed, but it doesn’t fill out td1 first and the continue to the next td.

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.

I would love it if someone could help me out with this code.

simply add an empty cell to the html output:


// Output table 
echo "<table width=\"100%\"><tr>"; 
for ($i = 1; $i <= $numCells; $i++) 
{ 
echo "<td>".$newStringArray[$i]."</td>";
// Fill spacer
echo "<td width="10">&nbsp;</td>";
}
echo "</table></tr>";

you can use an expression in there somewhere to determine whether it happens after the last cell… think creatively.