?
Solved

Architecture for PHP application

Posted on 2009-05-06
7
Medium Priority
?
247 Views
Last Modified: 2013-12-12
I have to design a PHP application. This application will have a GUI. Into this GUI a string will be entered. This string will be processed and persisted in a MySQL Database. Then the application will go and fetch contents of another MySQL table.

I was wondering, in terms of block diagram, how should it be optimally designed?
0
Comment
Question by:ashima_mahajan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 51

Assisted Solution

by:Steve Bink
Steve Bink earned 800 total points
ID: 24322335
Say you have a form:

<form action="my-string-handler.php" method="POST">
  <input type="textbox" name="mystring" />
  <input type="submit" />
</form>

The application logic would look like this:
<?
// my-string-handler.php
 
if (!isset($_POST['mystring'])) {
  // no string...nothing to do?
}
 
// you'll need code to connect to the server and select the database
// see mysql_connect() and mysql_selectdb()
 
// process the string here, then insert into database
$string = mysql_real_escape_string($_POST['mystring']);
$query = "INSERT INTO MyTable (FieldName) VALUES ('$string')";
if (!($result=mysql_query($query))) {
  // query failed. report and ...
  die();
}
 
// now select another table
$query = "SELECT * FROM MyOtherTable";
if (!($result=mysql_query($query))) {
  // query failed. report and ...
  die();
}
?>

Open in new window

0
 

Author Comment

by:ashima_mahajan
ID: 24323018
Thanks routinet!

My concern here is, how good an idea is it to directly connect to the database from 1 central bit of code. Pardon me if it is a stupid question. I am a Java developer and am used to structured layers. Like in Java we would write a Data access object to just acess the database and this dao would be then used by the business logic.
0
 
LVL 19

Assisted Solution

by:NerdsOfTech
NerdsOfTech earned 400 total points
ID: 24323237
This is acceptable practice in PHP since the code is typically custom and is usually used uniquely.

Only when the code is to be reused several times would it be best practice to create code blocks, functions, or the typical include.

As you can see, coding in PHP is quite different from the explicit drawn-out OO coding side; as most of the coding is BUILT-INTO PHP. Easy!

=NerdsOfTech
0
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
LVL 51

Expert Comment

by:Steve Bink
ID: 24323444
I agree with NerdsOfTech.  OOP is fine in its place, and PHP does have some OO constructs available to it, but it is not generally needed.  In your circumstance, removing the database connection to an include file might be a good idea, but that's as far as I would go for something as simple as you are describing.

You're certainly able to create class structures and utilize them in the rest of the app, but the utility in that approach depends on how large of a project you have and how extensible it should be moving forward.  I find that it is best to plan for that in the beginning.  In other words, if you think you'll need it later, code towards that end now.

If you interested in pre-made database classes, there are many available.  I generally find them more trouble than they are worth since they restrict me to coding to someone else's idea of how my database access should work.  They work better for people who like to code, but do not like to fiddle much with the database structures or queries.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 total points
ID: 24328206
Regarding this, "My concern here is, how good an idea is it to directly connect to the database from 1 central bit of code. "  I would answer this way...

I start EVERY PHP script with a set of statements like this:
<?php // RAY_config_page_usage.php
 
// LOAD THE CONFIG PAGE FROM THE DOCUMENT ROOT
$config_page = $_SERVER["DOCUMENT_ROOT"] . '/config.php';
@include_once($config_page);
 
// LOAD THE CONFIG PAGE FROM THE CURRENT WORKING DIRECTORY
$config_page = getcwd() . '/config.php';
@include_once($config_page);
 
 
 
// REST OF SCRIPT GOES HERE...

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24328236
The config pages looks like this.  It performs all the initialization I need and takes care of all the common functions and data definitions.

In some cases, you can look to autoloaders with OOP PHP, but I find that my biggest config pages are rarely so large that you can notice the time it takes to load them, and it helps to know in advance that all my "basic" stuff is take care of.

HTH, ~Ray
<?php // RAY_config_page_example.php - SAMPLE CONFIG SCRIPT
 
// CODE TO PROTECT CONFIG PAGE FROM RUNNING THE SCRIPT STANDALONE
if (count(get_included_files()) < 2)
{
   header("HTTP/1.1 301 Moved Permanently");
   header("Location: /");
   exit;
}
 
// SEE ALL THE ERRORS - NO FALSE ASSUMPTIONS
error_reporting(E_ALL);
 
// START THE SCRIPT TIMER
list ($x, $y) = explode(" ", microtime()); $script_start_time = $x + $y;
 
// ALWAYS START THE SESSION
session_start();
 
// DEFINE MY LOCAL CONSTANTS
DEFINE("API_ID_TOKEN", "PuhCIzE7m2MKraU6HwEwsKu7dNXrCK707tjO7SMLcHy1JZ2CdS");
DEFINE("MAX_FILE_SIZE", "8192000");
 
// ESTALISH MY ENTRY POINT
if (empty($_SERVER["HTTPS"]))
{
	$protocol = 'http://';
}
else
{
	$protocol = 'https://';
}
$my_entry_uri		= $protocol . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
 
// HOOK UP THE DATA BASE
$db_host	= "localhost";
$db_name	= "acct_dbn";
$db_user	= "acct_uid";
$db_word	= "password";
 
// CONNECT TO THE DATA BASE
if (!$db_connection = @mysql_connect("$db_host", "$db_user", "$db_word")) {
	$errmsg	= mysql_errno() . ' ' . mysql_error();
	echo "\n\n\n\n<!-- ! db_connection -->";
	echo "\n<!-- $errmsg -->\n\n\n\n";
	warning_message($errmsg);
}
 
// SELECT THE DATA BASE
if (!$db_sel = @mysql_select_db($db_name, $db_connection)) {
	$errmsg	= mysql_errno() . ' ' . mysql_error();
	echo "\n\n\n\n<!-- ! db_sel -->";
	echo "\n<!-- $errmsg -->\n\n\n\n";
	warning_message($errmsg);
}
 
// LOCAL FUNCTIONS FOLLOW
 
// SEND A WARNING MESSAGE BY EMAIL
function warning_message($subj, $comment="NIL")
{
	$comment .= "\n\n";
	$comment .= "HTTP_USER_AGENT ";
	$comment .= $_ENV["HTTP_USER_AGENT"];
 
	$comment .= "\n\n";
	$comment .= "REMOTE_ADDR ";
	$comment .= $_ENV["REMOTE_ADDR"] . ':' . $_ENV["REMOTE_PORT"];
 
	$comment .= "\n\n";
	$comment .= "REQUEST_URI ";
	$comment .= $_ENV["REQUEST_URI"];
 
	$from	= "From: you@your.org\r";
	mail ( 'you@you.org',	$subj, $comment, $from );
return;
}
 
// FORCE IT TO CHARACTERS ONLY
function clean_alphanum_string($string)
{
	return trim(ereg_replace('[^a-zA-Z0-9_\.\-]', '', $string));
}
 
// ETC. ETC...

Open in new window

0
 

Accepted Solution

by:
ashima_mahajan earned 0 total points
ID: 24334590
Ray_Paseur, routinet, NerdsOfTech.. Thanks a lot! :D
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question