First element of the array Form pass, but the next don't

Discussion in 'PHP' started by afonseca, Mar 5, 2010.

  1. #1
    Hi,

    I have a simple for with various text areas to fill in.
    3 of them are arrays. cod[], descri[] and qty[]

    I'm emailing this form to my e-mail, and all the code works fine except the array fields, that only show the 1st element of the array.

    To give you a prespective, this is the code where i pull the array:

    --

    $cod = $_POST['cod'];
    foreach($cod as $c)
    {
    if( isset($c) && $c<>'')
    {
    $EmailBody .="Codigo: $c\n";
    }
    }

    $cod = $_POST['descri'];
    foreach($descri as $d)
    {
    if( isset($d) && $d<>'')
    {
    $EmailBody .="Descricao: $d\n";
    }
    }


    $cod = $_POST['qty'];
    foreach($qty as $q)
    {
    if( isset($q) && $q<>'')
    {
    $EmailBody .="Numero de amostras: $q\n";
    }
    }

    --

    The above code is giving me the first element of the array.

    Is there something wrong with the code?
    Can you help please?

    Thank you.
    António
     
    afonseca, Mar 5, 2010 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi, please try this code:

    
    $cod = $_POST['cod[]'];
    foreach($cod as $c){
    $c=trim($c);
    if(!empty($c)) $EmailBody .= "Codigo: {$c}\n";
    }
    
    PHP:
    Regards :)
     
    koko5, Mar 5, 2010 IP
  3. fibi

    fibi Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think you should check this lines:

    $cod = $_POST['descri'];
    foreach($descri as $d){...
    PHP:
    may be the variable should be the same as the one in the foreach.
     
    fibi, Mar 5, 2010 IP
  4. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Those solutions are really bad - it runs on every index, even though you just need the first. That's waste of processing power (and in the end results in slower parsing of your script).

    Just put break 1; in the bottom of your foreach constructor. That will end the loop after first run.

    
    $cod = $_POST['cod[]'];
    foreach($cod as $c){
    $c=trim($c);
    $EmailBody .= "Codigo: {$c}\n";
    break 1;
    }
    
    PHP:
     
    elias_sorensen, Mar 5, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    If I understood right, problem is, that it grabs first only element.
    If we need only the first element, it is simply $_POST['cod'][0]
    Otherwise
    
    $EmailBody .= implode(' ',$_POST['cod']);
    
    PHP:
    Regards
     
    koko5, Mar 5, 2010 IP
  6. ceo.ahlul

    ceo.ahlul Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    To make and array with the form you must give the name like this:
    name="thisarray[]"
    Not like this:
    name="thisarray"

    Hope this help you :)
     
    ceo.ahlul, Mar 5, 2010 IP