Cleaning up White Space

Discussion in 'PHP' started by cesarcesar, Feb 13, 2007.

  1. #1
    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
    
    <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>
    
    Code (markup):
    Example View Source
    
    <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>
    
    Code (markup):
     
    cesarcesar, Feb 13, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Take out the spaces between your <?php tags.

    
    ?>
    
    This space here
    			<?php 
    
    PHP:
    Make one tag of it, or make them close together.

    
    <?php
    
    all php content here
    
    
    ?>
    
    PHP:
     
    nico_swd, Feb 13, 2007 IP
  3. cesarcesar

    cesarcesar Peon

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i understand the line breaks are due to them in the HTML. i am just looking for a way to remove them after/while the code is being rendered.
     
    cesarcesar, Feb 13, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    the source code of a page is never ever perfect unless it's static html, if you view the source of this page, you will notice the format comes and goes as the page progresses, this is achieved by using templates instead of echoing raw html to the console, format the templates properly and you'll get a sort of formatted output.
     
    krakjoe, Feb 13, 2007 IP
  5. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you were using a templating system it might be worth it but you currently are just directly outputting it - to clean it up you'll need to store it all in a variable first or use ob_ functions. That's a fair bit of overhead just to make your source code prettier and personally I would just manually tidy up your HTML in the script.
     
    rodney88, Feb 13, 2007 IP
  6. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #6
    RIght krakjoe, that what I am thinking. Why use print and echo so much, It really mess up code. Leave no boundary between UI and Logic.

    Use template engine like Smarty and TemplatePower, if templates are formatted the html source will also be formatted. Other than this, it will become much much easy for you to update layout of your website.
     
    designcode, Feb 13, 2007 IP