How to insert linebreaks in generated html code

2019-07-16 05:50发布

问题:

I am using codeigniter to generate an html table for insertion into a template view. Unfortunately this is coming out as a very long string on 1 line like:

<table id="myDataTable" class="table table-bordered table-striped" style="clear: both"><tbody><tr><td>id</td><td><a href="#" id="id" data-type="text" data-pk="2" data-url="/post" data-title="id">2</a></td></tr><tr><td>email</td><td><a href="#" id="email" data-type="text" data-pk="2" data-url="/post" data-title="email">A@B.COM</a></td></tr><br><tr><td>notes</td><td><a href="#" id="notes" data-type="text" data-pk="2" data-url="/post" data-title="notes">notes go here</a></td></tr><br> ...

I want it to look more like:

<tr>         
    <td>field1</td>
    <td><a href="#" id="field1" data-type="select" data-pk="1" data-value="" data-original-title="Select field1"></a></td>
    <td><span class="muted">Select, loaded from js array. Custom display</span></td>
</tr>

<tr>         
    <td>field2</td>
    <td><a href="#" id="field2" data-type="select" data-pk="1" data-value="" data-original-title="Select field2"></a></td>
    <td><span class="muted">Select, loaded from js array. Custom display</span></td>
</tr>

Here is my code:

$string='<table id="myDataTable" class="table table-bordered table-striped" style="clear: both"><tbody>';

foreach ($array as $key => $value) {
    $string=$string."<tr><td>$key</td>";  
    $element='<td><a href="#" id="'.$key.'" data-type="text" data-pk="'.$rowID.'" data-url="/post" data-title="'.$key.'">'.$value.'</a>'.'</td></tr>'."<br>";
    $string=$string.$element;
}

$string=$string.'</tbody></table>';

How can I fix this?

回答1:

You can add newlines (\n) and tabs (\t) to your $string variable.

E.g.:

$string='<table id="myDataTable" class="table table-bordered table-striped" style="clear: both">\n\t<tbody>\n';
foreach ($array as $key => $value) {
    $string = $string."<tr>\n\t<td>$key</td>\n";  
    $element = '\t<td><a href="#" id="'.$key.'" data-type="text" data-pk="'
            .$rowID.'" data-url="/post" data-title="'.$key.'">'.$value.'</a>'
            .'</td>\n</tr>'."<br>";
    $string = $string.$element;
}
$string=$string.'\n\t</tbody>\n</table>';


回答2:

You could use the \n and \t characters in your loop.

For example:

$string=$string."<tr>\n\t<td>$key</td>\n"; 
$element='\t<td><a href="#" id="'.$key.'" data-type="text" data-pk="'.$rowID.'" data-url="/post" data-title="'.$key.'">'.$value.'</a>'.'</td>\n</tr>'."<br>";

Also, you shouldn't have a line break after the </tr>.



回答3:

You have to add the line breaks with \n and \t for tabs

$string='<table id="myDataTable" class="table table-bordered table-striped" style="clear: both">\n\t<tbody>\n';

        foreach ($array as $key => $value) {

        $string=$string."\t\t<tr><td>$key</td>\n";  
        $element='\t\t<td><a href="#" id="'.$key.'" data-type="text" data-pk="'.$rowID.'" data-url="/post" data-title="'.$key.'">'.$value.'</a>'.'</td></tr>\n';
        $string=$string.$element;
        }


          $string=$string.'\t</tbody>\n</table>';

Also br tag is not allowed directly inside table, so I removed it. It doesn't serve any purpose anyways since rows automatically appear on new lines



回答4:

You could add the line breaks with \n and \t to your $string variable:

$string='<table id="myDataTable" class="table table-bordered table-striped" style="clear: both">\n\t<tbody>\n';

        foreach ($array as $key => $value) {

        $string=$string."\t\t<tr><td>$key</td>\n";  
        $element='\t\t<td><a href="#" id="'.$key.'" data-type="text" data-pk="'.$rowID.'" data-url="/post" data-title="'.$key.'">'.$value.'</a>'.'</td></tr>\n'."<br>";
        $string=$string.$element;
        }


回答5:

I suggest experimenting with \n and spaces in the echoes, for example:

$string=$string."<tr>\n    <td>$key</td>";

I believe the result will be:

<tr>
    <td>field1</td>

Edit: Michael's response using \t and \n is way better.