Another "loop" question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fartsniff

    Another "loop" question...

    I swear, these loops and for each's are going to kill me (if regex doesn't
    first ;)

    anyways, this is what i have:

    if ($_POST['item_name1'] != "")
    {
    @$item_name1 = $_POST['item_name1'];
    @$item_number1 = $_POST['item_number1'];
    }

    i am trying to come up with the best way (i.e. for each or something) to
    process these.
    basically, item_name can be anything from item_name1 to item_name99,
    and i surely dont want to have a bunch of copy/pastes 99 times (or more,
    the number depends on how much someone buys from me ;)


  • Randell D.

    #2
    Re: Another "loop&quot ; question...


    "fartsniff" <fart@sniff.com > wrote in message
    news:vgcqud1fnp bdf5@corp.super news.com...[color=blue]
    > I swear, these loops and for each's are going to kill me (if regex doesn't
    > first ;)
    >
    > anyways, this is what i have:
    >
    > if ($_POST['item_name1'] != "")
    > {
    > @$item_name1 = $_POST['item_name1'];
    > @$item_number1 = $_POST['item_number1'];
    > }
    >
    > i am trying to come up with the best way (i.e. for each or something) to
    > process these.
    > basically, item_name can be anything from item_name1 to item_name99,
    > and i surely dont want to have a bunch of copy/pastes 99 times (or more,
    > the number depends on how much someone buys from me ;)
    >
    >[/color]

    Why assign 99 values to 99 different variables when you could assign the
    values to an array... one possible solution could be

    foreach($_POST as $key=>$value)
    {
    $item_name[$key]="$value";
    }

    or perhaps better still

    $item_name=$_PO ST;

    Thus, if you have a form field named item_name1 which has a value "randell"
    then you can print this element of the array like so:

    print("item_nam e1 = $item_name[item_name1]");

    or

    $whatever=$item _name['item_name1'];

    (note the single quotes)

    Will that work?



    Comment

    • fartsniff

      #3
      Re: Another &quot;loop&quot ; question...

      sweet - that works perfect. thanks for helping out. i keep getting stuck
      doing
      "simple" things. my code always look clunky, works fine, just clunky and not
      the most efficient.

      thanks again.

      "Randell D." <you.can.email. me.at.randelld@ yahoo.com> wrote in message
      news:3XuNa.3611 80$ro6.8766234@ news2.calgary.s haw.ca...[color=blue]
      >
      > "fartsniff" <fart@sniff.com > wrote in message
      > news:vgcqud1fnp bdf5@corp.super news.com...[color=green]
      > > I swear, these loops and for each's are going to kill me (if regex[/color][/color]
      doesn't[color=blue][color=green]
      > > first ;)
      > >
      > > anyways, this is what i have:
      > >
      > > if ($_POST['item_name1'] != "")
      > > {
      > > @$item_name1 = $_POST['item_name1'];
      > > @$item_number1 = $_POST['item_number1'];
      > > }
      > >
      > > i am trying to come up with the best way (i.e. for each or something) to
      > > process these.
      > > basically, item_name can be anything from item_name1 to item_name99,
      > > and i surely dont want to have a bunch of copy/pastes 99 times (or more,
      > > the number depends on how much someone buys from me ;)
      > >
      > >[/color]
      >
      > Why assign 99 values to 99 different variables when you could assign the
      > values to an array... one possible solution could be
      >
      > foreach($_POST as $key=>$value)
      > {
      > $item_name[$key]="$value";
      > }
      >
      > or perhaps better still
      >
      > $item_name=$_PO ST;
      >
      > Thus, if you have a form field named item_name1 which has a value[/color]
      "randell"[color=blue]
      > then you can print this element of the array like so:
      >
      > print("item_nam e1 = $item_name[item_name1]");
      >
      > or
      >
      > $whatever=$item _name['item_name1'];
      >
      > (note the single quotes)
      >
      > Will that work?
      >
      >
      >[/color]


      Comment

      Working...