1

this code

$table_rows = '';
foreach ($result as $row) {           
  $tr = '<tr>';
  $tr = $tr . '<td>' . $row['crn_name'] . '</td>';
  $tr = $tr . '<td>' . $row['crn_code'] . '</td>';
  $tr = $tr . '</tr>';            
  $table_rows = $table_rows . $tr;
}

generates this output

<tr><td>forint</td><td>Ft</td></tr><tr><td>euro</td><td>€</td></tr>

It works, but the source code of the page looks terrible. I would like to produce a code which is much more easy to read. Somthing like that:

<tr>
  <td>forint</td>
  <td>Ft</td>
</tr>
<tr>
  <td>euro</td>
  <td>€</td>
</tr>

what should I do?!

6
  • 1
    Have a look at Tidy. Commented Jan 5, 2014 at 21:53
  • 4
    Why do you want it to be pretty? Your browser's developer tools' tree view displays the DOM exactly the same with and without whitespace. Commented Jan 5, 2014 at 21:54
  • 1
    Also consider why you want it to be readable. In production, unless you have to support a use case that requires viewing of source code, whitespace does nothing but increase page weight. If you're requiring it for debug, then Firebug or a webkit inspector will automatically format your elements in the appropriate view. Commented Jan 5, 2014 at 21:55
  • 1
    Why on earth would you even care how badly formatted the rendered html is?? Commented Jan 5, 2014 at 22:01
  • ^^ + the original is is also smaller, therefore faster/ less bandwidth Commented Jan 5, 2014 at 22:06

5 Answers 5

3

Tidy is probably too much. Just use the alternate syntax:

<?php foreach ($result as $row): ?>
  <tr>
    <td> <?= $row['crn_name']; ?> </td>
    <td> <?= $row['crn_code']; ?> </td>
  </tr>
<?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

Comments

1

Add suitable whitespace to your output. This is easier if you output your HTML directly and only dip into PHP mode when you need PHP.

<?php foreach ($result as $row) { ?>
<tr>
  <td><?php echo $row['crn_name']; ?></td>
  <td><?php echo $row['crn_code']; ?></td>
</tr>
<?php } ?>

Comments

1

The easiest way to do this without meticulously inserting line breaks, tabs and spaces where they really stick out like a sore thumb would be to use tidy.

    $config = array(
            'indent'         => true,
            'output-xhtml'   => true,
            'show-body-only' => true,
            'wrap'           => false);

    $tidy = new tidy;
    $tidy->parseString($html, $config, 'utf8');
    $tidy->cleanRepair();

Comments

-1

You may use return \r or newline \n

Comments

-1

Yes, you can. Just put teal tab there you want spaces and \n back each line.

Something like that

    $table_rows = '';
    foreach ($result as $row) {           
      $tr = '\n';
      $tr = $tr . '     ' . $row['crn_name'] . '\n';
      $tr = $tr . '     ' . $row['crn_code'] . '\n';
      $tr = $tr . '\n';            
      $table_rows = $table_rows . $tr;
    }

or you can use only space. But \n <-- for new line. Good luck!

$table_rows = ''; foreach ($result as $row) { $tr = '<tr>\n'; $tr = $tr . ' <td>' . $row['crn_name'] . '</td>\n'; $tr = $tr . ' <td>' . $row['crn_code'] . '</td>\n'; $tr = $tr . '</tr>\n'; $table_rows = $table_rows . $tr; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.