How to print each .. on a new line in PHP?

Go To StackoverFlow.com

0

Currently, each td../td prints in just 1 line which makes the source code very hard to read, any way I can print each td../td element on new line to make it much more readable.

thanks for replies. i tried the break but it prints on screen, need to put it in here but where?

$entries[$i] = '<td>'.$id[$i] .'</td>';
    $entries[$i] .= '<td>'.$username[$i].'</td>';
    $entries[$i] .= '<td>'.$first_name[$i].'</td>';

Further Edit: enter image description here

The relevant HTML

<!-- Main hero unit for a primary marketing message or call to action --> 
      <div class="hero-unit"> 

        <div class="row">

<table class="table">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<td>3</td>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>

   <table class="table table-bordered"> 
    <thead> 
    <tr> 
    <th>ID</th> 
    <th>Username</th> 
    <th>First Name</th> 
    <th>Last Name</th> 
    <th>Email Address</th>
    <th>Country</th>
    <th>Referrer</th>
    </tr>
    </thead>

       <tbody>
       <tr>

     <?php 

if (isset($content) && !empty($content) && is_array($content)) {//2
$i = 0;
$entries = array();


    while(array_key_exists($i, $content)) {//1

    $entries[$i] = "\n" .'<td>'.$id[$i] .'</td>';
    $entries[$i] .= "\n" .'<td>'.$username[$i].'</td>';
    $entries[$i] .= "\n" .'<td>'.$first_name[$i].'</td>';
    $entries[$i] .= "\n" .'<td>'.$last_name[$i].'</td>';
    $entries[$i] .= "\n" .'<td>'.$email_address[$i].'</td>';
    $entries[$i] .= "\n" .'<td>'.$country[$i].'</td>';
    $entries[$i] .= "\n" .'<td>'.$where_about[$i].'</td>';
    //$entries[$i] .= '<td>'.$sample[$i].'</td>';

    echo $entries[$i];


     $i++;


}//1

}//2


?>

</tr> 
</tbody>
</table> 
2012-04-03 20:44
by Ali Gajani
Have you been able to solve this problem with any of the answers below, or is it still open - sg3s 2012-04-08 12:20


4

concat "\n" to the front of the statement

Edit

$entries[$i] = "\n" . '<td>'.$id[$i] .'</td>';
$entries[$i] .= "\n" . '<td>'.$username[$i].'</td>';
$entries[$i] .= "\n" . '<td>'.$first_name[$i].'</td>';
2012-04-03 20:46
by David Nguyen
Thanks man, tried but it wont work for some reason. See my edit - Ali Gajani 2012-04-03 20:55
should work fine.. - David Nguyen 2012-04-03 20:57
Ok thanks great I thought it would fix my main problem. The source code is same as the one given in Bootstrap table example but mine wont resize to a different res where theirs would check the edit screensho - Ali Gajani 2012-04-03 21:00
you're just asking another question, originally you said the source code was hard to rea - David Nguyen 2012-04-03 21:08
I asked because the main reason behind it was as seen in the shot..:( it didn't help me solve it. I was sure it would but it hasnt yet - Ali Gajani 2012-04-03 21:09
You should use a different source code viewer - Kickaha 2013-12-27 23:40


2

A neat trick is to define a constant with a linebreak:

define('NL', "\r\n");

Then we just concat that constant not worrying about using single our double qoutes usage:

$entries[$i] = '<td>'.$id[$i] .'</td>' . NL .
               '<td>'.$username[$i].'</td>' . NL .
               '<td>'.$first_name[$i].'</td>' . NL;

Same trick ofcourse can be applied to tabs:

define('TAB', "\t");

So we can do something radical like this, for instance:

$entries[$i] = '<tr>' . NL . 
               TAB . '<td>' . $id[$i] . '</td>' . NL .
               TAB . '<td>' . $username[$i] . '</td>' . NL .
               TAB . '<td>' . $first_name[$i] . '</td>' . NL .
               '</tr>' . NL;

Spaces are up to personal preference ofcourse but when you're consistent it even keeps your code readable!

And in your case you could make your code even cooler by working through those columns (<td>) with a foreach loop through all fields.

$entries[$i] = '<tr>' . NL;

foreach(array('id', 'username', 'first_name') as $key) {
    $entries[$i] = TAB . '<td>' . ${$key}[$i] . '</td>' . NL;
}

$entries[$i] = '</tr>' . NL;

Now tell me that isn't cool!

Seems like your code simply lacks the intelligence to add each column in its own table row (tr), here is your relevant PHP code with the fixes and suggested additions, it should work but I can't test it.

<?php 

define('NL', "\r\n");
define('TAB', "\t");

if (isset($content) && !empty($content) && is_array($content)) {
    $i = 0;
    $entries = array();
    $fields = array('id', 'username', 'first_name', 'last_name', 'email_address', 'country', 'where_about');

    while(array_key_exists($i, $content)) {
        $entries[$i] = '<tr>' . NL;
        foreach($fields as $key) {
            $entries[$i] = TAB . '<td>' . ${$key}[$i] . '</td>' . NL;
        }
        $entries[$i] = '</tr>' . NL;

        echo $entries[$i];
        $i++;
    }

}
?>

As I am reading this, walking through $content with a while might not be good idea, try foreach($content as $index => $value) instead then would make $index the equivelant of $i but you wouldn't need to use $content[$index] because that exact value will be present in $value in a foreach.

2012-04-03 21:05
by sg3s
Thanks man..Check out my edit for the problem ..i thought \n would solve it but it hasn - Ali Gajani 2012-04-03 21:08
Ah I guess your problem is that the wider table cause it to overflow into the right of the screen... Now that is a different matter. Do you have an example, maybe a cut and pasted screenshot with what the result SHOULD be?.. - sg3s 2012-04-03 21:16
Well it should adjust to the size as the example does. Even the source is same - Ali Gajani 2012-04-03 21:18
The only difference is my scenario has 8 Ali Gajani 2012-04-03 21:19
And you forgot the <tr> tags which define the rows in an html table, check the edit for my suggested fix - sg3s 2012-04-03 21:27
Oh as a last note, you need to remove the <tr> and </tr> just before and after the php code; these will be added inside the loop in php instead for each row, making the <td>s fall in line neatly - sg3s 2012-04-03 21:32
I know this is an old thread but had to comment this -- there is already such a constant, named PHP_EOL - leftclickben 2013-02-05 11:29
@leftclickben PHP_EOL is based on the newlines used in the host OS which I do not like to depend on. Setting this, shorter, constant myself allows me to make sure it is always in \r\n format - sg3s 2013-02-05 12:42


1

Use '\n' after the td tag you echo/print

2012-04-03 20:47
by huysentruitw


1

<? 
     foreach($array as $single) { 
          echo "<td></td> \n";
     }
?>
2012-04-03 20:47
by wwwroth


1

You can add a line break to your print statement.

echo "My text\n";

You can also add tabs to get it indented neatly.

echo "\t\tMy text\n";

2012-04-03 20:47
by veiset


1

I would just like to add: If you're using Windows to read the code, you should use \r\n instead of just \n

2012-04-03 20:51
by Simon Forsberg


1

Not sure what you're asking but if you want to get rid of the :

$s .= '<td>' . $n . '</td>';

use ($co means close open)

$co = '</td><td>';
$row = '<tr><td>' . $data . $co . $data2 . $co . $data3 . '</td></tr>';
2012-04-03 20:55
by Andrew Briggs
didnt see your edit when i posted thi - Andrew Briggs 2012-04-03 20:56
If that's the case, please either edit or delete your answer so that it won't mislead the OP - Madara Uchiha 2012-04-03 20:59
Thanks guy, check out my edit. I thought ur help would solve the main problem at hand. - Ali Gajani 2012-04-03 21:05
Ads