By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

Cleaning up White Space

100+
P: 162
Hello,

I am trying to clean up my "view source" code. Currently, due to the way i format my php/html code, the end viewable source looks really spread out with lots of white space between lines.

Is there a way via CSS or using /n that will clean my code up a little.

I have looked into Code Beautifing programs and am NOT intrested in them.

Thanks.

Example Code
Expand|Select|Wrap|Line Numbers
  1. <table width="100%" border=0 cellspacing=0 cellpadding=5 style="text-align:left;padding:px;font-size:12px;">
  2.     </tr>
  3.  
  4.         <?php
  5.         $x=0;
  6.         /*   get field names in Ranges tab  */
  7.         $_conn2 = db_query("
  8.         Select
  9.         sale_type_field.sale_type_field_name,
  10.         sale_type_field.sale_type_field_value,
  11.         sale_type_field.calc,
  12.         sale_type_field.calc1,
  13.         sale_type_field.calc2
  14.         From
  15.         sale_type_field
  16.         LEFT OUTER JOIN connector_sale_type_field ON sale_type_field.sale_type_field_id = connector_sale_type_field.sale_type_field_id
  17.         Where
  18.         connector_sale_type_field.sale_type_tab_id = 137 AND
  19.         connector_sale_type_field.sale_type_id = 34
  20.         Order By
  21.         connector_sale_type_field.stf_display_order Asc
  22.         ");
  23.         while($conn2 = db_fetch_array($_conn2)){
  24.  
  25.             /*   set underscored version of sale_type_field_name   */
  26.             $sale_type_field_name = implode('_',explode(' ',strtolower($conn2['sale_type_field_name'])));
  27.  
  28.             /*   get sale type field names   */
  29.             if ($_vars['sale_id'] <> '') {
  30.                 $_stfv = db_query("
  31.                 Select
  32.                 sale_type_subdivision.$sale_type_field_name,
  33.                 connector_sale.sale_id,
  34.                 connector_sale.ref_id
  35.                 From
  36.                 connector_sale
  37.                 LEFT OUTER JOIN sale_type_subdivision ON sale_type_subdivision.sale_type_subdivision_id = connector_sale.ref_id
  38.                 Where
  39.                 connector_sale.sale_id = '".$_vars['sale_id']."'
  40.                 limit 1
  41.                 ");
  42.                 $stfv = db_fetch_array($_stfv);
  43.                 $sale_type_field_value = $stfv[$sale_type_field_name];
  44.             }
  45.  
  46.             /*   if there is no saved value, set to default   */
  47.             if ($stfv[$sale_type_field_name] == "") {
  48.                 $sale_type_field_value = $conn2['sale_type_field_value'];
  49.             }
  50.  
  51.             /*   set lot array   */
  52.             $number = "";
  53.             $lot_sales = array('First Lot Sale ','Second Lot Sale ','Third Lot Sale ','Home Sale '); //
  54.  
  55.             /*   set some vars for determining what lot sale it is   */
  56.             if (in_array(substr($conn2['sale_type_field_name'],0,15),$lot_sales)){
  57.                 $number = 15;
  58.             }elseif (in_array(substr($conn2['sale_type_field_name'],0,16),$lot_sales)){
  59.                 $number = 16;
  60.             }elseif (in_array(substr($conn2['sale_type_field_name'],0,10),$lot_sales)){
  61.                 $number = 10;
  62.             }
  63.  
  64.             /*   re-set sale_type_field_name so that proper fielt type is set   */
  65.             $sale_type_field_name2 = $sale_type_field_name;
  66.             $sale_type_field_name = strtolower(substr($sale_type_field_name,$number));
  67.             ?>
  68.  
  69.  
  70.             <?php  /*  if the field a calculation..    */ ?>
  71.             <?php
  72.             if ($conn2['calc'] != "") {
  73.  
  74.                 if ($conn2['calc1'] == "") {
  75.                     $conn2['calc1'] = $sale_type_field_name2;
  76.                 }else{
  77.                     $_ft = db_query("
  78.                     Select
  79.                     sale_type_field.sale_type_field_name
  80.                     From
  81.                     sale_type_field
  82.                     Where
  83.                     sale_type_field.sale_type_field_id = '".$conn2['calc1']."'
  84.                     limit 1
  85.                     ");
  86.                     $ft = db_fetch_array($_ft);
  87.                     $conn2['calc1'] = implode('_',explode(' ',strtolower($ft['sale_type_field_name'])));
  88.                 }
  89.  
  90.                 /*   get low hi ave numbers   */
  91.                 $_avg = db_query("
  92.                 Select
  93.                 Count(sale_type_lot_sale.$conn2[calc1]) AS count_$conn2[calc1],
  94.                 Sum(sale_type_lot_sale.$conn2[calc1]) AS sum_$conn2[calc1],
  95.                 Min(sale_type_lot_sale.$conn2[calc1]) AS min_$conn2[calc1],
  96.                 Max(sale_type_lot_sale.$conn2[calc1]) AS max_$conn2[calc1]
  97.                 From
  98.                 sale_type_lot_sale
  99.                 Where
  100.                 sale_type_lot_sale.sale_id = '".$_vars['sale_id']."' AND
  101.                 sale_type_lot_sale.$conn2[calc1] <> ''
  102.                 limit 1
  103.                 ");
  104.                 $avg = db_fetch_array($_avg);
  105.  
  106.                 if ($conn2['calc'] == "lot absorption") {
  107.  
  108.                     /*   total lot count minus   */
  109.                     $absorption = $lot_count['count_lot_nbr'] - $avg['count_'.$conn2['calc1']];
  110.                     $_value = $absorption;
  111.  
  112.                 }elseif ($conn2['calc'] == "lot absorb perc") {
  113.  
  114.                     /*   total lot count minus   */
  115.                     $absorption = $avg['count_'.$conn2['calc1']] / $lot_count['count_lot_nbr'];
  116.                     if ($absorption == 1) { $absorption = 100; }else{ $absorption = substr($absorption,0,4); }
  117.                     $_value = $absorption."%";
  118.  
  119.                 }elseif ($conn2['calc'] == "lot count") {
  120.  
  121.                     $_value = $avg['count_'.$conn2['calc1']];
  122.  
  123.                 }elseif ($conn2['calc'] == "lot low") {
  124.  
  125.                     $_value = $avg['min_'.$conn2['calc1']];
  126.  
  127.                 }elseif ($conn2['calc'] == "lot high") {
  128.  
  129.                     $_value = $avg['max_'.$conn2['calc1']];
  130.  
  131.                 } ?>
  132.  
  133.                 <?php if ($x == 0) { ?><tr><?php } ?>
  134.                 <th valign="top" align="right"><?php pv($conn2['sale_type_field_name']); ?>:</td>
  135.                 <td valign="top"><?php pv($_value); ?></td>
  136.                 <?php if ($x == 2) { $x=0; }else{ $x++; } ?>
  137.  
  138.             <?php } ?>
  139.  
  140.         <?php } ?>
  141.  
  142.         <?php for ($i = 0; $i < 2-$x; $i++) { ?><td></td><?php } ?>
  143.  
  144.     </tr>
  145. </table>
  146.  
Example View Source
Expand|Select|Wrap|Line Numbers
  1. <table width="100%" border=0 cellspacing=0 cellpadding=5 style="text-align:left;padding:px;font-size:12px;">
  2.     </tr>
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.                 <tr>                <th valign="top" align="right">First Lot Sale:</td>
  11.                 <td valign="top">2004-09-03</td>
  12.  
  13.  
  14.  
  15.  
  16.  
  17.                                 <th valign="top" align="right">No Closed Home Sales:</td>
  18.                 <td valign="top">-3</td>
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.                                 <th valign="top" align="right">Lots Sold:</td>
  28.                 <td valign="top">4</td>
  29.  
  30.  
  31.  
  32.  
  33.  
  34.                 <tr>                <th valign="top" align="right">Last Lot Sale:</td>
  35.                 <td valign="top">2005-01-19</td>
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.                                 <th valign="top" align="right">Lots Left:</td>
  45.                 <td valign="top">-4</td>
  46.  
  47.  
  48.  
  49.  
  50.  
  51.                                 <th valign="top" align="right">Absob As Of:</td>
  52.                 <td valign="top">2005-01-19</td>
  53.  
  54.  
  55.  
  56.  
  57.  
  58.                 <tr>                <th valign="top" align="right">Pct Absorb:</td>
  59.                 <td valign="top">%</td>
  60.  
  61.  
  62.  
  63.  
  64.  
  65.                                 <th valign="top" align="right">Homes Sold:</td>
  66.                 <td valign="top">3</td>
  67.  
  68.  
  69.  
  70.  
  71.     </tr>
  72. </table>
  73.  
Feb 13 '07 #1
Share this Question
Share on Google+
2 Replies


Motoma
Expert 2.5K+
P: 3,235
There are a number of ways to do this:
Use Templates.
Explicitly echo all HTML(i.e. entire php file is in one large set of <?php ?> brackets).
Don't be such a messy coder.

The first is most preferable, as it separates your user interface from your actual information.
The second takes a little bit of work, and the gains aren't that great for large unique pages.
The third is a horrible solution and should never be considered ;)
Feb 13 '07 #2

P: 99
you could catch your output in a string and add the line breaks and indentation as hard code.

[PHP]
$output = '';
$output .= '<table>'.chr(10);
$output .= chr(32).chr(32).'<tr>'.chr(10);
[/PHP]

This is a monkey job so maybe a function is more usable

[PHP]
function createLine($html,$indentation = 0){
$output = '';
for($i=0;$i<$indentation;$i++){ $output .= chr(32); }
$output .= $html.chr(10);
return $output;
}

$html = '';
$html .= createLine('<table>');
$html .= createLine('<tr>',2);
[/PHP]


or you can skip this all together and seperate as much php code from your html code and even go the template way.
Feb 13 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.