Simple Array question

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Simple Array question

    Hi,
    If i am posting something like this from a form:
    <input type=checkbox name='id[sh1]' value=3>
    <input type=checkbox name='id[sh2]' value=4>

    How do i get the value of id[]?
    eg:
    I dont want the "sh1" and "sh2", i just want the "3" and "4"

    The reason i dont wan the sh1 and sh2 is those are dynamic and will be
    changing, I just need the values (in this case 3 and 4) to do some
    comparasion and assignment operations

    Kindly reply.

    Thank,
    -Ryan

    Ryan A Guest

  2. Similar Questions and Discussions

    1. Need Simple Answer to Simple Contribute/Firefox question
      Hello all; I've tried the Adobe help in CS3, tech support, phone support, this forum, other forums, Mozilla, and nowhere can I get a straight...
    2. simple question about passing array to document
      Hi.I have a question I was hoping someone could help me with. I have created a simple page - page1.mxml. In that page I have an unnamed...
    3. XML::Simple array size
      In my sample XML file... <main> <level1> <level2>whatever</level2> <level2>whatever</level2> <level2>whatever</level2>...
    4. simple php question: mysql_fetch_row display array contents
      I am trying to do something in PHP that I thought would be fairly easy but has become rather troublesome. I'm trying to retrieve a list of...
    5. simple array question?
      On 12-Aug-2003, "Randell D." <you.can.email.me.at.randelld@yahoo.com> wrote: http://www.php.net/manual/en/function.list.php -- Tom...
  3. #2

    Default Re: [PHP] Simple Array question

    I think what you want is:

    <input type="checkbox" name="id[]" value="3">
    <input type="checkbox" name="id[]" value="4">

    Then on your next page, you can just do:

    foreach($_POST['id'] AS $row)
    echo $row;

    Or whatever, and that way you only get what you need.


    On Tue, 2003-07-29 at 13:08, Ryan A wrote:
    > Hi,
    > If i am posting something like this from a form:
    > <input type=checkbox name='id[sh1]' value=3>
    > <input type=checkbox name='id[sh2]' value=4>
    >
    > How do i get the value of id[]?
    > eg:
    > I dont want the "sh1" and "sh2", i just want the "3" and "4"
    >
    > The reason i dont wan the sh1 and sh2 is those are dynamic and will be
    > changing, I just need the values (in this case 3 and 4) to do some
    > comparasion and assignment operations
    >
    > Kindly reply.
    >
    > Thank,
    > -Ryan
    --
    Adam Voigt (adam@cryptocomm.com)
    Linux/Unix Network Administrator
    The Cryptocomm Group

    Adam Voigt Guest

  4. #3

    Default Re: Simple Array question

    Normally array values start at 0 (zero), but let's say sh1=1 and sh2=2. then
    you could echo the values out like this: echo "ID 1: $id[1]<br>ID 2:
    $id[2]<br>";

    Irvin.
    _____________________________________

    "Ryan A" <ryan@jumac.com> wrote in message
    news:002f01c355f4$0e3b3bd0$f081aa51@l2zcaxu7emppqh ...
    > Hi,
    > If i am posting something like this from a form:
    > <input type=checkbox name='id[sh1]' value=3>
    > <input type=checkbox name='id[sh2]' value=4>
    >
    > How do i get the value of id[]?
    > eg:
    > I dont want the "sh1" and "sh2", i just want the "3" and "4"
    >
    > The reason i dont wan the sh1 and sh2 is those are dynamic and will be
    > changing, I just need the values (in this case 3 and 4) to do some
    > comparasion and assignment operations
    >
    > Kindly reply.
    >
    > Thank,
    > -Ryan
    >

    Irvin Amoraal Guest

  5. #4

    Default Re: [PHP] Re: Simple Array question

    Hi,
    Thanks for replying.
    I tried that and have 2 numbers like so:

    <input name="id[sh123]" type="hidden" value="32">
    <input name="id[sh1sd]" type="hidden" value="563">

    and tried to echo it like so:
    <?php
    $id=$_POST['id'];
    foreach($_POST['id'] AS $row)
    echo $row;
    ?>
    Echo of one is <?php echo $row[0]; ?> and echo of two is:<b> <?php echo
    $row[1]; ?></b><br><br>
    Echo of one is <?php echo $id[0]; ?> and echo of two is:<b> <?php echo
    $id[1]; ?></b>

    the output i got was:
    ******************
    32563Echo of one is 5 and echo of two is: 6

    Echo of one is and echo of two is:
    *****************


    AS you can see its totally wrong.Any other ideas?

    cheers,
    -Ryan

    > Normally array values start at 0 (zero), but let's say sh1=1 and sh2=2.
    then
    > you could echo the values out like this: echo "ID 1: $id[1]<br>ID 2:
    > $id[2]<br>";
    >
    > Irvin.



    > > Hi,
    > > If i am posting something like this from a form:
    > > <input type=checkbox name='id[sh1]' value=3>
    > > <input type=checkbox name='id[sh2]' value=4>
    > >
    > > How do i get the value of id[]?
    > > eg:
    > > I dont want the "sh1" and "sh2", i just want the "3" and "4"
    > >
    > > The reason i dont wan the sh1 and sh2 is those are dynamic and will be
    > > changing, I just need the values (in this case 3 and 4) to do some
    > > comparasion and assignment operations
    > >
    > > Kindly reply.
    > >
    > > Thank,
    > > -Ryan
    > >
    >
    >
    >
    > --
    > PHP General Mailing List ([url]http://www.php.net/[/url])
    > To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    >
    Ryan A Guest

  6. #5

    Default Re: Simple Array question (conclusion)

    I GOT IT!!!!

    this is where the data was coming from:
    <input name="id[sh123]" type="hidden" id="id[sh123]" value="32">
    <input name="id[sh1sd]" type="hidden" id="id[sh1sd]" value="563">

    and this is where i take the values and dump it into an array/variables that
    i can call when and where i please:

    <?php
    $nn=0;
    foreach($id as $nname => $pppno)
    {$asdf[$nn]=$pppno;
    $nn++; }
    ?>
    <br><br><?php echo $asdf[0]; ?><br>
    <?php echo $asdf[1]; ?>


    I just took out some of the breaks so it will take less space but you get
    the idea.

    Thank you to everyone who tried to help, but maybe i didnt explain the
    problem well enough that you didnt get the answer or just didnt want to :-D

    Cheers,
    -Ryan



    ----- Original Message -----
    From: "Irvin Amoraal" <irvin@cabletv.on.ca>
    To: <php-general@lists.php.net>
    Sent: Tuesday, July 29, 2003 11:59 PM
    Subject: [PHP] Re: Simple Array question

    > Normally array values start at 0 (zero), but let's say sh1=1 and sh2=2.
    then
    > you could echo the values out like this: echo "ID 1: $id[1]<br>ID 2:
    > $id[2]<br>";
    >
    > Irvin.
    > _____________________________________
    >
    > "Ryan A" <ryan@jumac.com> wrote in message
    > news:002f01c355f4$0e3b3bd0$f081aa51@l2zcaxu7emppqh ...
    > > Hi,
    > > If i am posting something like this from a form:
    > > <input type=checkbox name='id[sh1]' value=3>
    > > <input type=checkbox name='id[sh2]' value=4>
    > >
    > > How do i get the value of id[]?
    > > eg:
    > > I dont want the "sh1" and "sh2", i just want the "3" and "4"
    > >
    > > The reason i dont wan the sh1 and sh2 is those are dynamic and will be
    > > changing, I just need the values (in this case 3 and 4) to do some
    > > comparasion and assignment operations
    > >
    > > Kindly reply.
    > >
    > > Thank,
    > > -Ryan
    > >
    >
    >
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    Ryan A Guest

  7. #6

    Default Re: [PHP] Re: Simple Array question

    Ryan A wrote:
    > Hi,
    > Thanks for replying.
    > I tried that and have 2 numbers like so:
    >
    > <input name="id[sh123]" type="hidden" value="32">
    > <input name="id[sh1sd]" type="hidden" value="563">
    >
    > and tried to echo it like so:
    > <?php
    > $id=$_POST['id'];
    > foreach($_POST['id'] AS $row)
    > echo $row;
    > ?>
    > Echo of one is <?php echo $row[0]; ?> and echo of two is:<b> <?php echo
    > $row[1]; ?></b><br><br>
    > Echo of one is <?php echo $id[0]; ?> and echo of two is:<b> <?php echo
    > $id[1]; ?></b>
    >
    > the output i got was:
    > ******************
    > 32563Echo of one is 5 and echo of two is: 6
    >
    > Echo of one is and echo of two is:
    > *****************
    >
    >
    > AS you can see its totally wrong.Any other ideas?
    No... you're getting exactly what you ask for. You rely on this list to
    much.

    1. You have no braces so your foreach() is taking the next line as the
    part that should be executed on each loop.

    2. Loop 1 sets $row = 32. Then it's displayed

    3. Loop 2 sets $row = 563. Then it's displayed

    4. So now $row = 563. When you display $row[0], you're asking for the
    first character of $row, so 5 is displayed. $row[1] displays 6.

    5. Now you go back to $id. Well, $id = array('sh123' => 32, 'sh1sd' =>
    563). So, when you try to display $id[0] and $id[1], there are no
    cooresponding values, so nothing is displayed.

    Now, how do you do this correctly?

    foreach($_POST['id'] as $key => $value)
    { echo "The random key is $key, the value is $value.<br />"; }

    which will give you:

    The random key is sh123, the value is 32.
    The random key is sh1sd, the value is 563.

    What do you want, besides that?

    --
    ---John Holmes...

    Amazon Wishlist: [url]www.amazon.com/o/registry/3BEXC84AB3A5E/[/url]

    PHP|Architect: A magazine for PHP Professionals – [url]www.phparch.com[/url]




    John W. Holmes Guest

  8. #7

    Default Re: [PHP] Re: Simple Array question (conclusion)

    Ryan A wrote:
    > I GOT IT!!!!
    >
    > this is where the data was coming from:
    > <input name="id[sh123]" type="hidden" id="id[sh123]" value="32">
    > <input name="id[sh1sd]" type="hidden" id="id[sh1sd]" value="563">
    >
    > and this is where i take the values and dump it into an array/variables that
    > i can call when and where i please:
    >
    > <?php
    > $nn=0;
    > foreach($id as $nname => $pppno)
    > {$asdf[$nn]=$pppno;
    > $nn++; }
    > ?>
    > <br><br><?php echo $asdf[0]; ?><br>
    > <?php echo $asdf[1]; ?>
    >
    >
    > I just took out some of the breaks so it will take less space but you get
    > the idea.
    >
    > Thank you to everyone who tried to help, but maybe i didnt explain the
    > problem well enough that you didnt get the answer or just didnt want to :-D
    Seriously?

    You could just replace the above with:

    $asdf = array_values($_POST['id']);

    I think you need to be cut off from the list for a while. Trial by fire,
    I say!!

    --
    ---John Holmes...

    Amazon Wishlist: [url]www.amazon.com/o/registry/3BEXC84AB3A5E/[/url]

    PHP|Architect: A magazine for PHP Professionals – [url]www.phparch.com[/url]




    John W. Holmes Guest

  9. #8

    Default John->Re: [PHP] Re: Simple Array question (conclusion)

    Hey John,
    I guess you are right, I do rely on this list quite a bit but am not the
    only one.
    Am learning by my mistakes though and finding solutions...they may not be
    the best solutions but they still work, everyone has to learn.
    Thanks for replying to the question though. And i didnt know about the
    "array_values" thing....see? the more you reply to me the more i learn :-D
    Cheers,
    -Ryan


    > Ryan A wrote:
    >
    > > I GOT IT!!!!
    > >
    > > this is where the data was coming from:
    > > <input name="id[sh123]" type="hidden" id="id[sh123]" value="32">
    > > <input name="id[sh1sd]" type="hidden" id="id[sh1sd]" value="563">
    > >
    > > and this is where i take the values and dump it into an array/variables
    that
    > > i can call when and where i please:
    > >
    > > <?php
    > > $nn=0;
    > > foreach($id as $nname => $pppno)
    > > {$asdf[$nn]=$pppno;
    > > $nn++; }
    > > ?>
    > > <br><br><?php echo $asdf[0]; ?><br>
    > > <?php echo $asdf[1]; ?>
    > >
    > >
    > > I just took out some of the breaks so it will take less space but you
    get
    > > the idea.
    > >
    > > Thank you to everyone who tried to help, but maybe i didnt explain the
    > > problem well enough that you didnt get the answer or just didnt want to
    :-D
    >
    > Seriously?
    >
    > You could just replace the above with:
    >
    > $asdf = array_values($_POST['id']);
    >
    > I think you need to be cut off from the list for a while. Trial by fire,
    > I say!!
    >
    > --
    > ---John Holmes...
    >
    > Amazon Wishlist: [url]www.amazon.com/o/registry/3BEXC84AB3A5E/[/url]
    >
    > PHP|Architect: A magazine for PHP Professionals – [url]www.phparch.com[/url]
    >
    >
    >
    >
    Ryan A Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139