Ryan Schafer
Ryan Schafer

Reputation: 241

How do I prevent the last newline?

<textarea style="resize: none;">
<?php
while ($row = mysql_fetch_array($result)) {
    echo $row[1] . "\n";
}
    ?>
</textarea>

I want to prevent the last value from having a newline - how would I go about doing that? I don't want the white space at the bottom.

Upvotes: 2

Views: 103

Answers (6)

Maxime Pacary
Maxime Pacary

Reputation: 23021

<?php
$results = array();

while ($row = mysql_fetch_array($result))
  $results[] = $row[1];

echo implode("\n", $results);
?>

or

<?php
$first = true;
while ($row = mysql_fetch_array($result)) {
    if (!$first)
      echo "\n";
    echo $row[1];
    $first = false;
}
?>

Upvotes: 0

Marcus
Marcus

Reputation: 12586

One way would be to do it like this:

<textarea style="resize: none;">
<?php
   $prefix= '';
   while ($row = mysql_fetch_array($result)) {
      echo $prefix . $row[1];
      if($prefix == ''){
         $prefix = "\n";
      }
?>
</textarea>

Upvotes: 0

Jason Brumwell
Jason Brumwell

Reputation: 3550

This should work:

<?php
$string = '';
while ($row = mysql_fetch_array($result)) {
    $string .= $row[1] . "\n";
}
?>
<textarea style="resize: none;">
    <?php echo trim($string,"\n"); ?>
</textarea>

Upvotes: 0

phixr
phixr

Reputation: 486

Save the rows in a variable and then remove the last character from the string.

<?php
$content = '';
while ($row = mysql_fetch_array($result)) {
    $content .= $row[1] . "\n";
}
echo substr($content, 0, strlen($content) - 1);
?>

Upvotes: 0

Ramon
Ramon

Reputation: 8424

<textarea style="resize: none;">
<?php
    $rows = array();
    while ($row = mysql_fetch_array($result)) {
        $rows[] = $row[1];
    }
    echo implode($rows, "\n");
?>
</textarea>

Upvotes: 5

Johnny Craig
Johnny Craig

Reputation: 5002

i usually do something like this:

<textarea style="resize: none;">
    <?php
        while ($row = mysql_fetch_array($result)) {
            if($stuff==''){
                $stuff=$row[1];
            }else{
                $stuff.= "\n" . $row[1];
            }
        }
        print $stuff;
    ?>
</textarea>

Upvotes: 0

Related Questions