SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: arry

  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    arry

    Greetings!

    My code below are having trouble in showing its value, I've checked already the names in html page and get its value using post, but when I tried to echo values, only one array value is showing. I used print_r and var_dump function but it also show the same single value.
    Code:
    $accnum = $_POST['accnum'];
    $copy = $_POST['copy'];
    $loc = $_POST['loc'];
    $fund = $_POST['funds'];
    $stat = $_POST['stat'];
    $remarks = $_POST['remarks'];
    
      foreach($accnum as $acc => $accnum){ 
       echo $accnum.'<br/>';
       }foreach($copy as $cpy => $copy){ 
        echo $copy.'<br/>';
       }foreach($loc as $loca=> $loc){ 
        echo $loc.'<br/>';
       }foreach($fund as $fun => $fund){ 
        echo $fund.'<br/>';
       }foreach($stat as $sts => $stat){ 
        echo $stat.'<br/>';
       }foreach($remarks as $marks => $remarks){ 
        echo $remarks.'<br/>';
       }
    Other array except $loc and $fund are working. Am I using a reserved word? Thank you!

  2. #2
    SitePoint Addict FizixRichard's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    372
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Whats in $accnum, not an Array I suspect


    Also, for each loops:

    PHP Code:

    $anArray 
    = Array('value1''value2''value3''value4');

    foreach(
    $anArray as $arrayItem)
    {
       echo(
    "Item: " $arrayItem "<br />");

    FIZIX - Full Service Digital Agency - Engaging websites, apps and games.
    Follow us @FIZIXAgency

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can Frank and I see the HTML form please 'arry?



    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you show your HTML code ? I think the problem is in the HTML form when send data by POST method

  5. #5
    SitePoint Addict FizixRichard's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    372
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Can Frank and I see the HTML form please 'arry?

    LOL


    Anyway, looking at this code again...

    Even if those items are arrays, that looks like a terrible way to achieve an end result.

    Also the => is for multi dimensional arrays.

    And you can't have arrays in form values, so $_POST['whatever'] isn't going to be an array, its going to be a single value, not an array of values. Thats assuming those POST's are picking up form values.



    Quote Originally Posted by outlier View Post
    Do you show your HTML code ? I think the problem is in the HTML form when send data by POST method
    Yes, most definitely as you can't have arrays in a form value so treating the value as an array in PHP isn't going to work.
    FIZIX - Full Service Digital Agency - Engaging websites, apps and games.
    Follow us @FIZIXAgency

  6. #6
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Here is my html code.

    <tbody id="inputtable">
    <tr><td>
    <input type = 'text' name='accnum[]' placeholder = 'Accession number' />
    <input type = 'text' name = 'copy[]' placeholder = 'Copies' />
    <input type = 'text' name = 'loc[]' placeholder = 'Book Location' />
    <input type = 'text' name = 'funds[]' placeholder = 'Source fund' />
    <input type = 'text' name = 'stat[]' placeholder = 'Status' />
    <input type = 'text' name = 'remarks[]' placeholder = 'Remarks' />
    <input type="button" value="Add new" onclick="addRow();" /></td></tr>
    </tbody>


  7. #7
    SitePoint Addict FizixRichard's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    372
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by claro View Post
    Here is my html code.
    OK...

    Code:
    <input type="text" name="accnum[]" placeholder="Accession number" />
    The name attribute wants to be "accnum" NOT "accnum[]" unless some jQuery is requiring it (cant think of why or what but I suspect thats wrong).

    Form fields are single values NOT arrays, so do not treat them as so.

    Then your collection would be

    Code:
    $accnum = $_POST['accnum'];
    Note that the variable $accnum is not an array, this is because accnum in the form is not an array.

    An array is a variable that contains multiple values, your form holds a single value.

    Your for each loop is redundant as you don't need it, this is because for each is for arrays.

    So you would access the $accnum variables value explicitly like so:

    Code:
    echo("Acc Num: " . $accnum);
    This rule applies to all of your form variables.


    Now, I figure there is some jQuery in there, of which I do not know what plugin you are using.

    If in the unlikely event that the way you have coded it and treating the variables as arrays is something to do with that jQuery (in other words, you are doing it like you are because a jQuery plugin told you to) you'll need to look at the jQuery plugins documentation as forms and the results from forms are not arrays, so in that case the jQuery would be doing things that don't follow standards.

    Thought I suspect if the jQuery plugin has told you to do it that way, the answer will be that the values are not arrays and you're trying to collect them incorrectly.


    Well actually there are exceptions, well sort of.

    But anyway, if you are trying to do this kind of thing:
    http://www.johnrockefeller.net/html-...-array-in-php/

    ... if thats the case your form doesn't fit that kind of model and your still collecting incorrectly.

    You see, that method isn't actually an array in the field but an array of fields. Which is different to what your form is trying to do (which looks like your expecting a field containing an array, which is not correct)

    Also remember to cleanse your inputs from the form for security and validation reasons.


    Edit:
    Actually, that last comment I made, is that what you are trying to do?

    As in, is your form actually a list of "users" with a bunch of in-line fields (one per row) with a submit button at the end of each row?

    If so, you could do something like that second link, but you wouldnt need a button next to each row in that context but one submit at the end of the form to process them all.

    In that case you would do something like this:

    PHP Code:
    foreach ($_POST['accnum'] as $accnum
    {
         
    // Do whatever you need to do with each accnum ...

    But also you wouldn't nest the other for each' in the first for each. You can pick up the array position in the first for each and use that to reference the others easily (so you can keep track of where you are in the list for each field properly).

    BUT I still suspect using the form layout you have, even if this is the case, that my first example is what you need and would suffice.
    FIZIX - Full Service Digital Agency - Engaging websites, apps and games.
    Follow us @FIZIXAgency

  8. #8
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Form fields are single values NOT arrays, so do not treat them as so.
    What if the values are arrays, not in single values? I put it accnum[] so that form will read it as array, (I assume I write it right) and other part of my form works well, I'm way to believe it is right.

    Now, I figure there is some jQuery in there, of which I do not know what plugin you are using, if in the unlikely event that the way you have coded it and treating the variables as arrays is something to do with that jQuery (in other words, you are doing it like you are because a jQuery plugin told you to) you'll need to look at the jQuery plugins documentation as forms and the results from forms are not arrays, so in that case the jQuery would be doing things that don't follow standards.
    I'm using plain javascript that adds new row of textbox.

  9. #9
    SitePoint Addict FizixRichard's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    372
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by claro View Post
    What if the values are arrays, not in single values? I put it accnum[] so that form will read it as array, (I assume I write it right) and other part of my form works well, I'm way to believe it is right.



    I'm using plain javascript that adds new row of textbox.
    Ah!

    OK, no...

    You cannot put an array into a form field.

    Code:
    <?php
    $someArray = Array('value1', 'value2', 'value3', 'value4');
    
    ?>
    
    <input type="next" name="someArray" value="<?=$someArray?>" />
    That there, is invalid, even if you set the name="someArray" to name="someArray[]". Doing what I just did above would do this (effectively):

    Code:
    <input type="text" name="someArray" value="value1" />
    So it would pick up the first value in the array, or it could pick up ARRAY() or it could pick up 0. Trying to put arrays into form fields isn't something I tend to do, so the actual result I'm not sure, but it will be something like one of those.


    if you wanted each item in the array to be entered into a field (say you had a list of fields):

    PHP Code:
    $someArray = Array('value1''value2''value3');

    for 
    each($someArray as $item)
    {
       echo(
    '<input type="text" name="someArray[]" value="' $item '" />);


    That would create an array called someArray in the POST, but its not an array in the field, but an array of fields sharing the same name (hook). Even in that case, you arent putting an array into the field.

    So in other words, the individual form fields (the inputs) are single values NOT arrays. So lets say you have 5 fields that are all accnum, the first having 1 in it, the second 2 through to the 5th having 5.

    The POST ($_POST['accnum']) however is an array in those circumstances but an array of those 5 fields not one of those fields holding an array.

    A single form field cannot be an array and cannot hold an array at all.


    PHP Code:

    $accnum = "100";

    ?>

    <input type="text" name="accnum" value="<?=$accnum?>" />
    <?
    Thats valid and collected using $accnum = $_POST['accnum'];


    Understand?
    FIZIX - Full Service Digital Agency - Engaging websites, apps and games.
    Follow us @FIZIXAgency

  10. #10
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    how about getting the values of my array ? foreach?

  11. #11
    SitePoint Addict FizixRichard's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    372
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by claro View Post
    how about getting the values of my array ? foreach?
    Well if its a single field, with a single name and a single value:

    Code:
    The Form Field:
    <input name="fieldname" value="thefieldvalue" />
    
    To collect:
    $fieldname = $_POST['fieldname'];
    
    // $fieldname contains the form field "fieldname"'s value.

    If it is an array of fields (not a field holding an array, as you cant do that):
    Code:
    The form:
    <input type="text" name="fieldname[]" value="1" />
    <input type="text" name="fieldname[]" value="2" />
    <input type="text" name="fieldname[]" value="3" />
    <input type="text" name="fieldname[]" value="4" />
    <input type="text" name="fieldname[]" value="5" />
    
    
    To collect them
    foreach($_POST['fieldname'] as $item)
    {
       // To output it
       echo("Item: " . $item . "<br />");
    
       // To set it
       $somevar = $item // Though this would reset somevar on each iteration, so you'd do what you needed to do with it in the iteration
    }
    
    The PHP here would output
    
    Item 1
    Item 2
    Item 3
    Item 4
    Item 5
    FIZIX - Full Service Digital Agency - Engaging websites, apps and games.
    Follow us @FIZIXAgency

  12. #12
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    actually I've got already the values, since I am using an array, I have rows of textboxes that name a, b and c, that when I click the button, a row is added. My array now is a[1], b[1], c[1], when I click again the button it adds the row and array as well so I must have a[2], b[2], c[2].

    My problem now is when I echo the result it only shows that a[0-1-2], b[0], c[0-1-2]. b shows only its single value which has also [1-2]. Thank you for your time.

  13. #13
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I used already var_dump and print_r function, they both shows the same answer. My array b contains only single value.How can I get rid of these?


Bookmarks

Posting Permissions

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