Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    I want to improve my coding style, any suggestions??

    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

    Code:
    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

    Code:
    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 Code:
    <?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 Code:
    <?php
    $smarty
    ->display("addPortfolio.tpl");
    ?>
    editPortfolio.php:
    PHP Code:
    <?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 !!
    Last edited by PHPycho; 03-02-2007 at 06:47 PM.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    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/.

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by aedrin View Post
    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.

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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!'.

  • #6
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...

  • #7
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hello fourm
    knock knock
    raising for help ?

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

  • #9
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Mr. aedrin
    Can you please help me by supplying some of your sample code so that i would get the better idea !!
    Thank You !!
    Last edited by PHPycho; 03-05-2007 at 07:12 PM.

  • #10
    Regular Coder
    Join Date
    May 2004
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #11
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •