0

I have a php call I am doing, and the response is used in a foreach function to fill a table.

My table cell contains a huge amount of whitespace, which looks like it is coming from the php response.

Below is an extract and when you take a look in the TD elements, you can see the extra space.

Not sure whats causing this, or how to remove it, maybe a trim?

<div class="container-fluid" style="margin-top:2em;">
            <div class="row">
                <div class="col-lg-12" style="background: none;">
                    <h4>Test Drives</h4>
                                        <table class="table table-fixed table-sm table-hover" style="height: 100px; background:pink;">
                        <thead>
                            <tr>
                            <th>
                                Value
                            </th>
                            <th>
                                Date Submitted
                            </th>
                            <th>
                                 User submitted
                            </th>
                                <th>
                                 Market
                            </th>
                                <th>
                                 Notes
                            </th>
                                <th>
                                 Last Update by
                            </th>
                            </tr>
                        </thead>
                        <tbody>
                                                    <tr>
                                <td>
                                    2                                </td>
                                <td>
                                    2017-03-08                                </td>
                                <td>
                                    ADMIN                                </td>
                                <td>
                                    DE                                </td>
                                <td>
                                    This is a test                                </td>
                                <td>
                                    ADMIN                                </td>
                            </tr>
                                                    </tbody>
                    </table>
                </div>
            </div>
        </div>

Below is the PHP code within my html:

<div class="container-fluid" style="margin-top:2em;">
            <div class="row">
                <div class="col-lg-12" style="background: none;">
                    <h4>Test Drives</h4>
                    <?php include 'campaign_detail.inc.php'; ?>
                    <table class="table table-fixed table-sm table-hover" style="height: 100px; background:pink;">
                        <thead>
                            <tr>
                            <th>
                                Value
                            </th>
                            <th>
                                Date Submitted
                            </th>
                            <th>
                                 User submitted
                            </th>
                                <th>
                                 Market
                            </th>
                                <th>
                                 Notes
                            </th>
                                <th>
                                 Last Update by
                            </th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach($testdrives as $tdrrow) {?>
                            <tr>
                                <td>
                                <?php echo "$tdrrow[CAMPAIGN_DATA_VALUE]";?>
                                </td>
                                <td>
                                <?php echo "$tdrrow[CAMPAIGN_DATA_DATE_CREATED]";?>
                                </td>
                                <td>
                                <?php echo "$tdrrow[CAMPAIGN_DATA_USER_CREATED]";?>
                                </td>
                                <td>
                                <?php echo "$tdrrow[CAMPAIGN_DATA_MARKET]";?>
                                </td>
                                <td>
                                <?php echo "$tdrrow[CAMPAIGN_DATA_NOTES]";?>
                                </td>
                                <td>
                                <?php echo "$tdrrow[CAMPAIGN_DATA_USER_UPDATED]";?>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Chris
  • 125
  • 4
  • 15
  • Could you show us the sample code, that produces the HTML? – JustBaron Mar 08 '17 at 12:58
  • I added the PHP code within the html in my snippet. – Chris Mar 08 '17 at 13:00
  • 1
    The issue is that you have put a lot of whitespace in front of the PHP echo, and then again on the next line before the closing tag – Alister Bulman Mar 08 '17 at 13:08
  • You will find it very difficult to make the output HTML tidy without mangling your source PHP code, so in all honesty, you shouldn't stop to worry about this. The output HTML is not your source code, so it doesn't need to be tidy; you will never need to edit it, so it's irrelevant. Your source code is the PHP code. Having that code as tidy as possible will make it easier to maintain. – Simba Mar 08 '17 at 13:09

3 Answers3

5

You can use trim() like below:-

<?php echo trim("$tdrrow[CAMPAIGN_DATA_VALUE]");?> 

And so on for others

Also remove extra spaces created by you for indentation

Note:-

@Fred-ii- valuable comment:-

Sidenote: If you also want clean HTML which is good practice when it comes to having to probably debug from source code, is to add \n's.

I.e. : <?php echo trim("$tdrrow[CAMPAIGN_DATA_VALUE]") . "\n";?> - Otherwise, you may get clumps of code in one line.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
3

Your spaces are created by indentation in HTML.

Use <td><?= trim($tdrrow[CAMPAIGN_DATA_VALUE]);?></td>

Justinas
  • 41,402
  • 5
  • 66
  • 96
1

Copied from https://stackoverflow.com/a/2109339/7403455

For just spaces, use str_replace:

$string = str_replace(' ', '', $string);

For all whitespace, use preg_replace:

$string = preg_replace('/\s+/', '', $string);

As simba suggests also clean up the PHP that generates this HTML output as well.

Community
  • 1
  • 1