Cleaning up White Space

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • empiresolutions
    New Member
    • Apr 2006
    • 162

    Cleaning up White Space

    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
    Code:
    <table width="100%" border=0 cellspacing=0 cellpadding=5 style="text-align:left;padding:px;font-size:12px;">
    	</tr>
    
    		<?php
    		$x=0;
    		/*   get field names in Ranges tab  */
    		$_conn2 = db_query("
    		Select
    		sale_type_field.sale_type_field_name,
    		sale_type_field.sale_type_field_value,
    		sale_type_field.calc,
    		sale_type_field.calc1,
    		sale_type_field.calc2
    		From
    		sale_type_field
    		LEFT OUTER JOIN connector_sale_type_field ON sale_type_field.sale_type_field_id = connector_sale_type_field.sale_type_field_id
    		Where
    		connector_sale_type_field.sale_type_tab_id = 137 AND
    		connector_sale_type_field.sale_type_id = 34
    		Order By
    		connector_sale_type_field.stf_display_order Asc
    		");
    		while($conn2 = db_fetch_array($_conn2)){
    
    			/*   set underscored version of sale_type_field_name   */
    			$sale_type_field_name = implode('_',explode(' ',strtolower($conn2['sale_type_field_name'])));
    
    			/*   get sale type field names   */
    			if ($_vars['sale_id'] <> '') {
    				$_stfv = db_query("
    				Select
    				sale_type_subdivision.$sale_type_field_name,
    				connector_sale.sale_id,
    				connector_sale.ref_id
    				From
    				connector_sale
    				LEFT OUTER JOIN sale_type_subdivision ON sale_type_subdivision.sale_type_subdivision_id = connector_sale.ref_id
    				Where
    				connector_sale.sale_id = '".$_vars['sale_id']."'
    				limit 1
    				");
    				$stfv = db_fetch_array($_stfv);
    				$sale_type_field_value = $stfv[$sale_type_field_name];
    			}
    
    			/*   if there is no saved value, set to default   */
    			if ($stfv[$sale_type_field_name] == "") {
    				$sale_type_field_value = $conn2['sale_type_field_value'];
    			}
    
    			/*   set lot array   */
    			$number = "";
    			$lot_sales = array('First Lot Sale ','Second Lot Sale ','Third Lot Sale ','Home Sale '); //
    
    			/*   set some vars for determining what lot sale it is   */
    			if (in_array(substr($conn2['sale_type_field_name'],0,15),$lot_sales)){
    				$number = 15;
    			}elseif (in_array(substr($conn2['sale_type_field_name'],0,16),$lot_sales)){
    				$number = 16;
    			}elseif (in_array(substr($conn2['sale_type_field_name'],0,10),$lot_sales)){
    				$number = 10;
    			}
    
    			/*   re-set sale_type_field_name so that proper fielt type is set   */
    			$sale_type_field_name2 = $sale_type_field_name;
    			$sale_type_field_name = strtolower(substr($sale_type_field_name,$number));
    			?>
    
    
    			<?php  /*  if the field a calculation..    */ ?>
    			<?php
    			if ($conn2['calc'] != "") {
    
    				if ($conn2['calc1'] == "") {
    					$conn2['calc1'] = $sale_type_field_name2;
    				}else{
    					$_ft = db_query("
    					Select
    					sale_type_field.sale_type_field_name
    					From
    					sale_type_field
    					Where
    					sale_type_field.sale_type_field_id = '".$conn2['calc1']."'
    					limit 1
    					");
    					$ft = db_fetch_array($_ft);
    					$conn2['calc1'] = implode('_',explode(' ',strtolower($ft['sale_type_field_name'])));
    				}
    
    				/*   get low hi ave numbers   */
    				$_avg = db_query("
    				Select
    				Count(sale_type_lot_sale.$conn2[calc1]) AS count_$conn2[calc1],
    				Sum(sale_type_lot_sale.$conn2[calc1]) AS sum_$conn2[calc1],
    				Min(sale_type_lot_sale.$conn2[calc1]) AS min_$conn2[calc1],
    				Max(sale_type_lot_sale.$conn2[calc1]) AS max_$conn2[calc1]
    				From
    				sale_type_lot_sale
    				Where
    				sale_type_lot_sale.sale_id = '".$_vars['sale_id']."' AND
    				sale_type_lot_sale.$conn2[calc1] <> ''
    				limit 1
    				");
    				$avg = db_fetch_array($_avg);
    
    				if ($conn2['calc'] == "lot absorption") {
    
    					/*   total lot count minus   */
    					$absorption = $lot_count['count_lot_nbr'] - $avg['count_'.$conn2['calc1']];
    					$_value = $absorption;
    
    				}elseif ($conn2['calc'] == "lot absorb perc") {
    
    					/*   total lot count minus   */
    					$absorption = $avg['count_'.$conn2['calc1']] / $lot_count['count_lot_nbr'];
    					if ($absorption == 1) { $absorption = 100; }else{ $absorption = substr($absorption,0,4); }
    					$_value = $absorption."%";
    
    				}elseif ($conn2['calc'] == "lot count") {
    
    					$_value = $avg['count_'.$conn2['calc1']];
    
    				}elseif ($conn2['calc'] == "lot low") {
    
    					$_value = $avg['min_'.$conn2['calc1']];
    
    				}elseif ($conn2['calc'] == "lot high") {
    
    					$_value = $avg['max_'.$conn2['calc1']];
    
    				} ?>
    
    				<?php if ($x == 0) { ?><tr><?php } ?>
    				<th valign="top" align="right"><?php pv($conn2['sale_type_field_name']); ?>:</td>
    				<td valign="top"><?php pv($_value); ?></td>
    				<?php if ($x == 2) { $x=0; }else{ $x++; } ?>
    
    			<?php } ?>
    
    		<?php } ?>
    
    		<?php for ($i = 0; $i < 2-$x; $i++) { ?><td></td><?php } ?>
    
    	</tr>
    </table>
    Example View Source
    Code:
    <table width="100%" border=0 cellspacing=0 cellpadding=5 style="text-align:left;padding:px;font-size:12px;">
    	</tr>
    
    		
    
    						
    		
    
    						
    				<tr>				<th valign="top" align="right">First Lot Sale:</td>
    				<td valign="top">2004-09-03</td>
    				
    			
    		
    
    						
    								<th valign="top" align="right">No Closed Home Sales:</td>
    				<td valign="top">-3</td>
    				
    			
    		
    
    						
    		
    
    						
    								<th valign="top" align="right">Lots Sold:</td>
    				<td valign="top">4</td>
    				
    			
    		
    
    						
    				<tr>				<th valign="top" align="right">Last Lot Sale:</td>
    				<td valign="top">2005-01-19</td>
    				
    			
    		
    
    						
    		
    
    						
    								<th valign="top" align="right">Lots Left:</td>
    				<td valign="top">-4</td>
    				
    			
    		
    
    						
    								<th valign="top" align="right">Absob As Of:</td>
    				<td valign="top">2005-01-19</td>
    				
    			
    		
    
    						
    				<tr>				<th valign="top" align="right">Pct Absorb:</td>
    				<td valign="top">%</td>
    				
    			
    		
    
    						
    								<th valign="top" align="right">Homes Sold:</td>
    				<td valign="top">3</td>
    				
    			
    		
    		
    	</tr>
    </table>
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    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 ;)

    Comment

    • xwero
      New Member
      • Feb 2007
      • 99

      #3
      you could catch your output in a string and add the line breaks and indentation as hard code.

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

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

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

      $html = '';
      $html .= createLine('<ta ble>');
      $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.

      Comment

      Working...