473,698 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Best" coding practice?

Wm
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some
of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo $variable;
?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?

BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!

Thanx,
Wm


Jul 17 '05 #1
9 4516
"Wm" <LA*******@hotm ail.com> schreef in bericht
news:Ea******** **************@ news.easynews.c om...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo $variable; ?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?
Personally i prefer to have one big block of php code, and echoes to print
HTML.
BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!


I seperated my database query stuff from the rest of the code by putting
that code in classes and in seperate php files. I then include_once those
files. The major reason why i decided to work like this, is that when i
decide to change some things in the structure of my database, most of my
code won't have to change. Only those classes using that particular bit of
database.

Floris

Jul 17 '05 #2
You should really look into templating.. There's a nice templating class
called phpLibs; I personally use the phpBB forums template class, works
incredibly well and it's very easy to use.. Keeping your code and html
separate makes it much easier to read and modify..

Regards,
Tim
"Wm" <LA*******@hotm ail.com> wrote in message
news:Ea******** **************@ news.easynews.c om...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo $variable; ?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?

BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!

Thanx,
Wm

Jul 17 '05 #3
The goal is not to seperate HTML and PHP, it's to seperate display logic
from 'buisness logic'. Using a loop inside your HTML is not a bad thing,
as long as you don't fetch your data in there.

Adding a templating engine above PHP is not always necessary. PHP can be
used as a templating engine itself if you don't use every feature of the
language, and it can be quite simple, even for a non-programmer.

timmeh wrote:
You should really look into templating.. There's a nice templating class
called phpLibs; I personally use the phpBB forums template class, works
incredibly well and it's very easy to use.. Keeping your code and html
separate makes it much easier to read and modify..

Regards,
Tim
"Wm" <LA*******@hotm ail.com> wrote in message
news:Ea******** **************@ news.easynews.c om...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see


some
of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo


$variable;
?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the HTML
at one time?

BTW, I have this same debate on a couple of pages where I do mySQL queries
2-3 times on the same page... Advice, suggestions and recommendations are
greatly appreciated!

Thanx,
Wm



Jul 17 '05 #4
"Wm" <LA*******@hotm ail.com> wrote in message
news:Ea******** **************@ news.easynews.c om...
As an amateur wannabe-pro programmer, I am trying to learn not only how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML
and PHP, very interspersed. Example: A form with 20 fields where I echo a
variable in each field to show existing data. My question is really about
the best practice to do this, as I have seen some interesting use of
includes/etc.


Hi,

take a look at TemplateTamer, it is IDE and template engine, that let's you
keep html and php in completely separate files. On related wiki you will
find several examples.

rush
--
http://www.templatetamer.com/

Jul 17 '05 #5
"Louis-Philippe Huberdeau" <lp*********@sy mpatico.ca> wrote in message
news:Vm******** ***********@new s20.bellglobal. com...
The goal is not to seperate HTML and PHP, it's to seperate display logic
from 'buisness logic'. Using a loop inside your HTML is not a bad thing,
as long as you don't fetch your data in there.


Well, there are at least 2 way to look at that problem.

My prefered approach is to use template engine to separate html and php, and
MVC to separate presentation logic and bussines logic. So that are 2
separate problems, and stuffing too much into template in order to handle
whole presentation layer makes it fat (or dirty if you will), for my taste.
I prefer template to remain just an resource with markers, no form of
controls structures in it, and control structures should be in separate php
file.

But I guess it is also a matter of personal preferences, and one should use
approah that fits him the best.

rush
--
http://www.templatetamer.com/


Jul 17 '05 #6
I always use herdoc string output embedded into PHP, like this

<?php

.... code
.... code
.... code

print <<<BLOCKNAME

<html><head><ti tle>
<img src="thisone">
<img src="{$fromthed atabase}">

Hello, {$name}, this is my lovely website.

BLOCKNAME;

You get all the advantages of double qoutes, but none of the hassle of
escaping quotes in HTML. Its the best form of string output when
creating large areas of HTML to be sent.

Jul 17 '05 #7
On Fri, 24 Oct 2003 09:30:29 +0100, Dave Bell <dr**@kent.ac.u k> wrote:
I always use herdoc string output embedded into PHP, like this


Wow - never seen that before.
Is it like printqq in Perl?
--
Games, Gizmos, Gifts and Toys from 'I want one of those'
<http://www.bizorg.co.u k/shopping/>
Jul 17 '05 #8
EXactly ! Thats my favourite feture too.

sanjay

"Dave Bell" <dr**@kent.ac.u k> wrote in message
news:bn******** **@athena.ukc.a c.uk...
| I always use herdoc string output embedded into PHP, like this
|
| <?php
|
| ... code
| ... code
| ... code
|
| print <<<BLOCKNAME
|
| <html><head><ti tle>
| <img src="thisone">
| <img src="{$fromthed atabase}">
|
| Hello, {$name}, this is my lovely website.
|
| BLOCKNAME;
|
| You get all the advantages of double qoutes, but none of the hassle of
| escaping quotes in HTML. Its the best form of string output when
| creating large areas of HTML to be sent.
|
Jul 17 '05 #9
Wm wrote:
As an amateur wannabe-pro programmer, I am trying to learn not only
how to
use PHP but how to do it *efficiently*. (Trust me, you don't wanna
see some
of my stuff!!!) I'm noticing a number of my pages have a mixture of
HTML and PHP, very interspersed. Example: A form with 20 fields where
I echo a
variable in each field to show existing data. My question is really
about the best practice to do this, as I have seen some interesting
use of includes/etc.

Is it "better" to replicate my form in HTML and insert <?PHP echo
$variable; ?> in each tag?
Or would it be better to somehow put all my PHP up front and echo the
HTML at one time?

BTW, I have this same debate on a couple of pages where I do mySQL
queries 2-3 times on the same page... Advice, suggestions and
recommendations are greatly appreciated!

Thanx,
Wm

I don't mix php and html. Whenn generating an html form, I load a
single variable with all the html code. Then print $var. All the
database stuff is done with functions.

--
11:15am up 12 days, 10:31, 1 user, load average: 1.00, 1.01, 1.04
103 processes: 101 sleeping, 2 running, 0 zombie, 0 stopped
CPU states: 2.0% user, 0.7% system, 1.0% nice, 0.2% idle
To email me, change .com to .ca Linux Counter Registration #126647

Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2476
by: Aaron Davies | last post by:
I'm developing a collaborative whiteboard, in which all objects (shapes, clip art icons, etc.) are synchronized between all participants in a session. It's working well, but I'm running into a problem: if two people try to drag the same object at the same time, nothing prevents them from doing so, and whichever one them lets go first will have his move be the one that takes effect. (The other person's client will probably crash at the...
2
2188
by: Svein Erik Storkaas | last post by:
I am about to add security to a web project for the first time, and i'm wondering what the easiest, yet a good way to do this? The page is just for personal use, so it does not need to be "ultra" secure. Is it ok just to store 'usrName' and 'psWrd' in an Access db, and manage it from there? If so, how do i control if the user really IS authorized on all the 'protected' pages? Thanks!
5
2857
by: Achim Domma | last post by:
Hi, I have to convert a string to its "best possible" ascii representation. It's clear to me that this is not possible or sense full for all unicode characters. But for most European characters it should be possible. For example: "Müller" should become "Muller" and "é" should become "e".
8
7166
by: elias.farah | last post by:
Hello Everyone, I'm having some very weird behavior on a couple of Access forms. (Not all forms, just some of them). The forms have been working for years, under Access XP/2003 etc, and last week upgraded from Windows XP/Office 2003 to Vista x64/Office 2007. Under Access 2007, a couple of forms are now taking 60 seconds to
9
6251
by: =?Utf-8?B?QW1tZXI=?= | last post by:
I've read many incomplete opinions about the "Best Practice" for securely accessing SQL but what I really need to find the "Best Practice" that fits my applications needs. Currently (alpha stage) I am Using a .Net DSN-Less SQLConnection method in my client program (vb.net) and sending uid/pwd across the network. The client only calls upon the stored procedures to access the tables in SQL 2005. This is a semi commercial application...
2
7649
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but don't have any luck is to do a form validation. This script requires the files: db-file-to-disk.asp and _upload.asp. There is a DESCRIPTION field in the db-file-to-disk.asp file, what I want to do is the user has to field out this fied before...
2
1435
Haitashi
by: Haitashi | last post by:
I have a database that currently contains different albums. The way I had the database was that there is a "description" field in which I placed all the songs. When I access my site I see in information of a CD and a paragraph that contains all the data I put in the "description" field. I am thinking of adding an "advanced search" feature to the site where the user can select Artist or Song Title. However, how can I ensure that it looks into...
1
2011
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Hi everyone, I'd be interested in hearing suggestions as to the "best" way to drive a Python program step by step from another application. Details: --------- I have implemented a "Robot" that can be programmed by a user to perform certain actions. (see Reeborg below for a simple javascript-
4
3138
by: lumpybanana247 | last post by:
Now, I understand this could have several answers, but my simple question is: What is the best free C++ compiler for Windows? For a long time, I have been using MinGW (using Bloodshed DevC++) on Windows XP. I find it great. But, assuming that i hadn't put much time into learning about that compiler that i presently use, what would everyone recommend? (and "why?" if possible) I know others exist such as MSVC++, Cygwin, Borland
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.