Jump to content

Recommended Posts

Ok this may sound strange but I'm wondering what is considered best practice, to have as many php files, each handling a single query or to have a single huge php file which handles multiple queries. Basically I mean what is better less or more and is there an advantage to either performance or anything else-wise?

 

At the moment I tend to use php files which retrieve data as their own php files i.e. I have a php file which gets all categories from my categories table then outputs this as xml and another php file which retrieves all items from my items table and outputs that data as xml.

 

But when it comes to inserting/updating/deleting data in a table I try to use if statements within 1 big php file based on a variable which I've named 'requesttype' which is basically what type of request the user has made eg if they click add the add php code will be used if they click update the update code etc.

 

Am I doing this along the right lines and is less indeed more when it comes to the amount of php files?

Hi there,

I will have a go at this and leave it open for others to follow. No doubt there will be a whole raft of answers telling me that I am wrong but here goes.

From the way you phrase your question, it would seem that you code is 'data driven'.

I would seriously suggest a look at object oriented coding and follow where that leads you.

Pete.

 

Well, for me personally I try to use as little PHP files as possible. Each executing as little code as possible. For example you dont want a file called "Functions.php" That you contain thousands of functions in when the page you are loading only needs one of those functions. Because of that your loading lets say.. an extra 15,000 lines of code that never get executed. Keep your code simple. And dont include things that are not relative to the script that is being run. Hope this helps.

If your server has an optimizer installed then it really doesn't matter how many or how few files are accessed or used by your PHP application.

 

However it sounds like your programs all execute within a global namespace without much use of functions or objects.  That style of programming tends to lead to buggy programs.

Thanks for the replies. To better explain what I mean here's a bit of my code from a file I've made called userrequest.php:-

 

if(($param_requesttype == "add") && ($param_loggedinuserid != "")){

$query = "UPDATE userlinks SET favourited = '1' WHERE linkid = '$param_linkid' AND userid = '$param_loggedinuserid'";

if ( !mysql_query($query, $mysql_connection) ){

   	 	die('ERROR: '. mysql_error() );

	}

	echo "LINK FAVOURITED";

} else if(($param_requesttype == "remove") && ($param_loggedinuserid != "")){

$query = "UPDATE userlinks SET favourited = '0' WHERE linkid = '$param_linkid' AND userid = '$param_loggedinuserid'";

if ( !mysql_query($query, $mysql_connection) ){

   	 	die('ERROR: '. mysql_error() );

	}

	echo "LINK UNFAVOURITED";

} else if(($param_requesttype == "savetwitterusername") && ($param_loggedinuserid != "")){

$query = "UPDATE users SET twitterusername = '$param_twitterusername' WHERE userid = '$param_loggedinuserid'";

if ( !mysql_query($query, $mysql_connection) ){

   	 	die('ERROR: '. mysql_error() );

	}

	echo "TWITTER USERNAME UPDATED";

}

 

My application itself allows users to do many things but the three things that are applicable to the above are that it allows users to add links to favourites within my application, remove links from favourites and save their twitter username. The above code used to be seperated into 3 different php files called addlink.php removelink.php and twitterservice.php but I've now condensed these into one file through the use of if loops as you can see in the example above. Is this better practice is basically what I'm wondering?

Ooooh boy.  You're in for some hurt with that script.

 

Instead of checking $param_loggedinuserid != "" every time, why not just have this at the top of your page:

if( $param_loggedinuserid != "" ) {
  return; // or exit();
}

 

You're not using mysql_real_escape_string() anywhere.

 

You're using die() and sending mysql errors directly to the browser.

 

Someone is going to attack your site eventually if you keep that stuff up.

Yep thats a good point and I may try that cheers. So just basically a big if loop surrounding all of the other if loops?

 

The thing is it's an Adobe Air desktop application that I'm using this for it only retrieves info via these php files it's not a website.

 

But besides that would you say the code is pretty effiecient in the way I've done it i.e. with mulitple if loops (or statements as I'm not sure if they're referred to as "if loops") rather than having multiple php files?

Ah right. No worries. I basically have a bunch of php files posted on a server and my application posts requests to these files which either does an update, insert, or delete to the database tables or gets the data from these tables in this database and outputs them as xml so this info can be displayed within my app depending on the type of request a user submits. The whole thing is pretty cool.

 

So if you were to do this how would you do it in your view? I mean if you needed to insert, delete and update a table would you recommend just 1 php file to do them all or should they be split into 3 files? What would operate most efficiently/is best practice regardless of whether it's in air or on a website as I imagine it's pretty much the same. That's all I want to know really

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.