PDA

View Full Version : Text-columns. What is wrong with my code?



superduper
November 9th, 2004, 11:07 AM
Check this out!!



<?
$col_size = 1600;
$line_length = 35;
$text=" text goes here ", ($text);
$num_text = strlen($text);
$num_cols = $num_text / $col_size;
if($_GET['s'] == "")
{
$start = 0;
}
else
{
$start = $_GET['s'];
}
$bstart = $start - $col_size - $col_size - 1;
for ($i = 0; $i < 2; $i++)
{
$q = 1;
$text_array[$i] = substr($text, $start, $col_size);
while(substr($text_array[$i], -1) != " ")
{
$text_array[$i] = substr($text, $start, $col_size + $q);
$q++;
}
$start = $start + strlen($text_array[$i]);
}
$max = count($text_array);
print "<table cellspacing='0' cellpadding='0' border='0' width='468'><tr>";
print "<td width='224' valign='top'>" . $text_array[0] . "</td>";
print "<td width='20'></td>";
print "<td width='224' valign='top'>" . $text_array[1] . "</td>";
print "</tr></table>";

if($bstart < 0)
{}else{
?>
<a href="?page=art&amp;s=<? print $bstart; ?>">Previous</a>
<?
}
if($start >= $num_text)
{}else{
?>
<a href="?page=art&amp;s=<? print $start; ?>">Next</a>
<?
}
?>



Me and me buddy have been making this awsome code to distibute text to columns, but there is something wrong here... I have no clue what that is:cyclops: cause I'm a real rookie. So... I was wondering: Can u guys help me? I think the error is somewhere around the $text-stuff in the top.

Pardon my bad english:disco: It would be great if u could help me!

ironikart
November 9th, 2004, 08:32 PM
It's a little messy... :(

This:
$num_cols = $num_text / $col_size;

will return a decimal number, you might want to try:
$num_cols = ciel($num_text / $col_size);

that will round the column up to the nearest whole number.

The rest is a little confusing, and at a guess I think your taking the wrong approach. If you divide the text up using the string length function then you'll get things like half words. I've done some code similar to what you trying to do here before and I've found the easiest (probably not the best) way to divide columns is to break the text up into an array of words, then restrict columns to a certain word count.

eg:


// Get an array of words
$aWords = explode(" ", $text);

// Count the words
$iWordCount = sizeof($aWords);


then maybe use foreach() to iterate through the array and generate a new column once a certain word count has been reached.

this is probably not ideal for large word counts and you'd be better off using the code to generate a static file rather than having it dynamically display for each visitor.