# Distributing text over columns

**URL:** <https://forum.kirupa.com/t/distributing-text-over-columns/64341>\
**Category:** Uncategorized\
**Created:** [September 15, 2004, 11:22am UTC](https://forum.kirupa.com/t/distributing-text-over-columns/64341 "2004-09-15T11:22:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![superduper](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@superduper](https://forum.kirupa.com/u/superduper)\
**Post date:** [September 15, 2004, 11:22am UTC](https://forum.kirupa.com/t/distributing-text-over-columns/64341/1 "2004-09-15T11:22:28Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 15, 2004, 11:54pm UTC](https://forum.kirupa.com/t/distributing-text-over-columns/64341/2 "2004-09-15T23:54:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 16, 2004, 12:33am UTC](https://forum.kirupa.com/t/distributing-text-over-columns/64341/3 "2004-09-16T00:33:11Z")

</div>

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:

```php

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 17, 2004, 12:48pm UTC](https://forum.kirupa.com/t/distributing-text-over-columns/64341/4 "2004-09-17T12:48:19Z")

</div>

thanks!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 19, 2004, 2:50am UTC](https://forum.kirupa.com/t/distributing-text-over-columns/64341/5 "2004-09-19T02:50:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 19, 2004, 11:15am UTC](https://forum.kirupa.com/t/distributing-text-over-columns/64341/6 "2004-09-19T11:15:02Z")

</div>

simply add an empty cell to the html output:

```php

// 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.
