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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    144
    Thanks
    3
    Thanked 20 Times in 20 Posts

    The logic of a noob

    Hi guys, I am a beginner php coder, I have written some code below, basically it works - but it seems illogical to copy the code 21 times, I tried to write a function for it but the function doesnt like $_POST values as input, have a look and see if you can point me in the right direction of some learning material / concepts. I repeat, not after a way to fix the code - it works, just some reccomendations on how to program smarter!

    PHP Code:
    if (isset($_POST['week']) and ($_POST['week']=="week_1")) {
        if(isset(
    $_POST['week_1_1'])) {
            
    $item_1 $_POST['week_1_1'];
        }
        if(isset(
    $_POST['week_1_2'])) {
            
    $item_2 $_POST['week_1_2'];
        }
            if(isset(
    $_POST['week_1_3'])) {
            
    $item_3 $_POST['week_1_3'];
        }
        if(isset(
    $_POST['week_1_4'])) {
            
    $item_4 $_POST['week_1_4'];
        }
        if(isset(
    $_POST['week_1_5'])) {
            
    $item_5 $_POST['week_1_5'];
        }
        if(isset(
    $_POST['week_1_6'])) {
            
    $item_6 $_POST['week_1_6'];
        }
        if(isset(
    $_POST['week_1_7'])) {
            
    $item_7 $_POST['week_1_7'];
        } 
    This only the first 7, it continues for another 14 times! And to top it off I have to run it 6 times on the same page!

  • #2
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post
    You could use array for ordered data like that and avoid the repeating line of code of similar data

    PHP Code:
    //built array for valid $_POST key
    for ($i 1$i <= 21$i++) {
        
    $weeks[$i] = "week_1_$i";
    }
    //And then iterate it. The rest is up to you :D 

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,278
    Thanks
    12
    Thanked 342 Times in 338 Posts
    though I don’t see what the use/advantage of the (original) code would be. it literally does: "assign a given POST parameter to a certain variable if the former is set. if not, do nothing." that means that if $_POST['week_1_1'] is not set, using $item_1 would cause a warning (at least).

    despite the fact that just renaming hides the attack potential of the input data.

    example:
    PHP Code:
    $item $_POST['item'];

    // lots of code


    // looks safe? and KABOOOM you fell into the SQL Injection trap
    $sql "SELECT a_field FROM my_table WHERE item = $item";

    // this is not better, but you immediately SEE the problem
    $sql "SELECT a_field FROM my_table WHERE item = " $_POST['item']; 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Regular Coder
    Join Date
    Jun 2009
    Posts
    144
    Thanks
    3
    Thanked 20 Times in 20 Posts
    thanks for the feedback guys


  •  

    Posting Permissions

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