5 Replies - 2912 Views - Last Post: 17 February 2011 - 01:22 PM

#1 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

PHP and HTML code separation

Posted 13 February 2011 - 10:00 AM

I've just finished reading the thread on good coding practice and have noticed various people talking about the importance of code separation. Now, I'm an advocate of code separation, utilising files like functions.php, scripts.js, and splitting files down into components. I also use a specific directory and file structure that emulates OOP (still don't know how to implement actual OOP in a heavy AJAX environment).

I think overall I'm doing alright (been coding 3 years, was coding in VBA/VBScript before), but I'm interested in this idea of code separation, especially in regards to HTML and PHP. Take for instance the following snippet of code from one of my files:
/* Draw standard controls */
echo '
	<p>Showing results ' . ($limit + 1) . '-' . min($rowCount,($limit + 25)) . '</p>
	<table>
		<tr>
			<td class="customButton" style="width:100px" id="AdminTools_SQLDefinitionsMenu_AddItem">Add Item</td>
';
if ($rowCount > 25)
{
	if ($limit > 0)
	{
		echo '
			<td class="customButton" style="width:100px" id="AdminTools_SQLDefinitionsMenu_First">First</td>
			<td class="customButton" style="width:100px" id="AdminTools_SQLDefinitionsMenu_Prev25">Previous 25</td>
		';
	}
	if ($rowCount - 25 > $limit)
	{
		echo '
			<td class="customButton" style="width:100px" id="AdminTools_SQLDefinitionsMenu_Next25">Next 25</td>
			<td class="customButton" style="width:100px" id="AdminTools_SQLDefinitionsMenu_Last">Last</td>
		';
	}
}
echo '
		</tr>
	</table>
';



How would you go about separating this? Is there a better way to go about doing something like this? What coding practices would you use?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP and HTML code separation

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4224
  • View blog
  • Posts: 13,389
  • Joined: 08-June 10

Re: PHP and HTML code separation

Posted 13 February 2011 - 10:15 AM

Id say, first remove the table as these are clearly not tabular data.

look into printf()/sprintf(). (can use imported template files (file_get_contents()))
Was This Post Helpful? 1
  • +
  • -

#3 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: PHP and HTML code separation

Posted 13 February 2011 - 10:53 AM

So something like this would be an improvement, or is this going too far?
/* Draw standard controls */
$id = "AdminTools_SQLDefinitionMenu_";
$class = "customButton w100px";
printf(file_get_contents('p.txt', '', '', 'Showing results ' . ($limit + 1) . '-' . min($rowCount,($limit + 25))));
printf(file_get_contents('span.txt', $class, $id . 'AddItem', 'Add Item'));
if ($rowCount > 25)
{
	if ($limit > 0)
	{
		printf(file_get_contents('span.txt', $class, $id . 'First', 'First'));
		printf(file_get_contents('span.txt', $class, $id . 'Prev25', 'Previous 25'));
	}
	if ($rowCount - 25 > $limit)
	{
		printf(file_get_contents('span.txt', $class, $id . 'Next25', 'Next 25'));
		printf(file_get_contents('span.txt', $class, $id . 'Last', 'Last'));
	}
}
printf('<br />');



p.txt
Spoiler


span.txt
Spoiler

This post has been edited by e_i_pi: 13 February 2011 - 10:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12557
  • View blog
  • Posts: 45,683
  • Joined: 27-December 08

Re: PHP and HTML code separation

Posted 13 February 2011 - 10:48 PM

From a PHP perspective, that's fine. However, I don't think your designers would be too happy with that setup. In general with programming, it is always good practice to separate the application and data from the display so that it is easy to extend and modify one without messing up the other. General practice is to create the HTML design, which acts as a template page. PHP then reads in the HTML, modifies the appropriate sections, and outputs it.

CTphpnwb also has a good tutorial you may find helpful. :)
Was This Post Helpful? 1
  • +
  • -

#5 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: PHP and HTML code separation

Posted 14 February 2011 - 04:02 PM

View Postmacosxnerd101, on 13 February 2011 - 10:48 PM, said:

From a PHP perspective, that's fine. However, I don't think your designers would be too happy with that setup. In general with programming, it is always good practice to separate the application and data from the display so that it is easy to extend and modify one without messing up the other. General practice is to create the HTML design, which acts as a template page. PHP then reads in the HTML, modifies the appropriate sections, and outputs it.

CTphpnwb also has a good tutorial you may find helpful. :)

I read through that tutorial, and yes it does help a little, but is quite basic and doesn't address some of the major problems I'm facing. The part in bold above is very helpful though. While I need to study up on methods such as OOP and PDO, it's theory like that which helps me understand the underlying principles of good code design.

Thanks Dormilich and macosxnerd101, your input combined is putting me on the right track :)
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3813
  • View blog
  • Posts: 13,857
  • Joined: 08-August 08

Re: PHP and HTML code separation

Posted 17 February 2011 - 01:22 PM

It is basic because it's aimed at beginners who tend to mix php and html heavily and then wonder why they can't find/fix their problems. That doesn't mean the principles can't be used in a large scale OOP/MVC system though. There's no reason your view code couldn't be a class that reads in and alters appropriate html files before sending the data to the browser!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1