...

View Full Version : I want to improve my coding style, any suggestions??



PHPycho
03-02-2007, 06:45 PM
Hello forums,
Here i am mentioning the style of my coding.
This is for the admin section,
Suppose i had any section(say portfolio in this case) which i had manage
ie


list | add | edit | delete

For that I always make the four files for every section
ie


listSection.php (for listing)
addSection.php (for add section)
editSection.php (for edit section)
deleteSection.php (for delete section)


and separate .tpl files for HTML section
ie


listSection.tpl
addSection.tpl
editSection.tpl


And i perform operations ie list | add | edit | delete in listSection.php
My listSection.php (say listPortfolio.php in this case)


<?php
/********************************************************
* PORTFOLIO OPERATIONS
********************************************************/
/* DELETE */
if(isset($_GET['mode']) &&
$_GET['mode'] == "deletePortfolio" &&
isset($_GET['deleteID']))
{
$deleteID = $_GET['deleteID'];
$portfolioObj->delete($deleteID);
}
/* ADD */
if(isset($_POST['mode']) &&
$_POST['mode'] == "addPortfolio")
{
$portfolio_name = $_POST['portfolio_name'];
//print_r($_POST);
$portfolioObj->insert($portfolio_name);
}
/* EDIT */
if(isset($_POST['mode']) &&
$_POST['mode'] == "editPortfolio" &&
isset($_GET['editID']))
{
$editID = $_GET['editID'];
$portfolio_name = $_POST['portfolio_name'];
$portfolioObj->update($editID,$portfolio_name);
}
?>

<?PHP
/* FETCH ALL THE PORTFOLIOS */
$listPortfolio = $portfolioObj->selectAll();
//echo $listPortfolio[0];
//print_r($listPortfolio[1]);
if($listPortfolio[0] == 0)
{
$smarty->assign("error","No existing portfolio !!");
$smarty->display("errorDisplay.tpl");
$smarty->display("addPortfolio.tpl");
}
else
{
$listPortfolio = $listPortfolio[1];
$smarty->assign("listPortfolio",$listPortfolio);
$smarty->display("listPortfolio.tpl");
}
?>


addPortfolio.php:

<?php
$smarty->display("addPortfolio.tpl");
?>

editPortfolio.php:

<?php
/* FETCH THE REQUIRED PORTFOLIO */
if(isset($_GET['editID']))
{
$editID = $_GET['editID'];
$getPortfolio = $portfolioObj->selectOne($editID);
$smarty->assign("getPortfolio",$getPortfolio);
$smarty->display("editPortfolio.tpl");
}
else
{
$smarty->assign("error","Error Occurred");
$smarty->display("errorDisplay.tpl");
}
?>
(here i didnt mention the .tpl files to reduce bulkiness)
----------------------------------------------------------------------------------------------------------------
My this style seems easy to maintain but its time consuming and doesnt look pro type coding..
Is there any suggestion ie any style for managing sections with ease creating less file ??

Thanks in advance to all of you !!

Inigoesdr
03-02-2007, 07:08 PM
The best way to improve your coding style is to practice, and read all of the lastest news/tutorials. Instead of making 4 different files you could make one file with if/else or even better switch() using $_SERVER['QUERY_STRING'] or if you have to $_GET['page']. Just make sure you sanitize the user input: preg_replace() or similar. You could combine this with mod_rewrite to make your urls /add/ or /list/.

aedrin
03-02-2007, 08:00 PM
I think what you need to set up is a generic module that allows you to edit/add/delete lists of elements.

The way I do it is I have a config file which can define a group, and each group details what kind of information applies to that list of elements. The generic admin I have automatically allows you to add/edit/delete those elements. So whenever I get a new type of data, I just add a group and I don't have to write a single line of code.

One of the easiest methods of improving your code: Whenever you feel you are copying code from somewhere else, it could probably be done a different way. Code something that will work for both situations, and if possible any other. Then make it available to both projects from a single place.

Inigoesdr
03-02-2007, 08:26 PM
One of the easiest methods of improving your code: Whenever you feel you are copying code from somewhere else, it could probably be done a different way. Code something that will work for both situations, and if possible any other. Then make it available to both projects from a single place.

Good advice. Also try to think of a better/easier way to do the same thing without sacrificing any security measures. You can't take for granted that the other person knew the best way to do something.

aedrin
03-02-2007, 09:39 PM
I just thought of another lifesaving tool.

Logging.

It doesn't cost a lot of effort to log things, but it can save your life and your reputation in a pinch.

When a client complains one of their customers placed an order and it isn't in the system. Without logs, all you can say is 'sorry, we'll do some more debugging'. But if you look important transactions like that? You can extract the data and tell the client 'No problem, we'll fix it all!'.

PHPycho
03-04-2007, 08:40 AM
I think what you need to set up is a generic module that allows you to edit/add/delete lists of elements.

The way I do it is I have a config file which can define a group, and each group details what kind of information applies to that list of elements. The generic admin I have automatically allows you to add/edit/delete those elements. So whenever I get a new type of data, I just add a group and I don't have to write a single line of code.


I am somewhat impressed by your this style..
Can you please show me the sample of code of how to achieve this ?
I would be very very greatful...I am in a road to improve my code anyway.
Thanks in advance...

PHPycho
03-05-2007, 05:28 PM
Hello fourm
knock knock
raising for help ?

aedrin
03-05-2007, 05:35 PM
It's too much code to show on a forum.

It's not that hard, you just have to keep thinking in reusable components. All my forms are generated and handled by a class. I plug my group file into that class to create the new/edit forms for the items. Then all you have left is a list page for each group. I seperate my groups into types. I have a type which is a named list, where each element is identified by a list (a flat page system). A hierarchal tree based type (using a component I wrote that does tree stuff) and a date based type (for news). There's probably many more types, but I make them as I need them.

PHPycho
03-05-2007, 06:21 PM
Mr. aedrin
Can you please help me by supplying some of your sample code so that i would get the better idea !!
Thank You !!

ClubCosmic
03-05-2007, 11:02 PM
another thing you can do for your admin script is the use of function()s. you can include() a page that has all of the admin functions separated. then, in the admin.php file you can call on an individual function as needed.

that way, when you make changes, you only need to make changes in the include()d file.

look up php function() on "the google"

good luck

Inigoesdr
03-05-2007, 11:18 PM
include() is useful for say a config file, or header/footer files, but you should not declare functions on every pageload that aren't going to be used. You should only declare functions if they are needed frequently and will be reused in the same pageload.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum