Professional Web Applications Themes

Another "loop" question... - PHP Development

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 ;)...

  1. #1

    Default 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 ;)


    fartsniff Guest

  2. #2

    Default Re: Another "loop" question...


    "sniff" <sniff.com> wrote in message
    news:vgcqud1fnpbdf5corp.supernews.com...
    > 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 ;)
    >
    >
    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=$_POST;

    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_name1 = $item_name[item_name1]");

    or

    $whatever=$item_name['item_name1'];

    (note the single quotes)

    Will that work?



    Randell D. Guest

  3. #3

    Default Re: Another "loop" 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> wrote in message
    news:3XuNa.361180$ro6.8766234news2.calgary.shaw.c a...
    >
    > "sniff" <sniff.com> wrote in message
    > news:vgcqud1fnpbdf5corp.supernews.com...
    > > 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 ;)
    > >
    > >
    >
    > 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=$_POST;
    >
    > 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_name1 = $item_name[item_name1]");
    >
    > or
    >
    > $whatever=$item_name['item_name1'];
    >
    > (note the single quotes)
    >
    > Will that work?
    >
    >
    >

    fartsniff Guest

Similar Threads

  1. SMTP: "to" or/and "recipient" question.
    By Serguei.Goumeniouk@cibc.ca in forum PERL Modules
    Replies: 1
    Last Post: November 11th, 06:31 PM
  2. Jmail only sending "body" in first run of loop
    By baz0hara in forum Dreamweaver AppDev
    Replies: 1
    Last Post: February 16th, 10:33 AM
  3. "activation required" loop
    By Julot in forum Macromedia Freehand
    Replies: 1
    Last Post: April 1st, 01:21 PM
  4. Question about "Public Sub" vs "Private Sub" vs "Sub"
    By michaaal in forum ASP Database
    Replies: 1
    Last Post: October 18th, 07:15 PM
  5. "Start" "Program" "Menu" list is empty
    By Pete in forum Windows XP/2000/ME
    Replies: 2
    Last Post: July 10th, 10:42 PM

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
  •  

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