Solved

Architecture for PHP application

Posted on 2009-05-06
7
235 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 200 total points
Comment Utility
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
Comment Utility
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 100 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Ray_Paseur, routinet, NerdsOfTech.. Thanks a lot! :D
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now