473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 ;)
Jul 16 '05 #1
2 2371

"fartsniff" <fa**@sniff.com> wrote in message
news:vg************@corp.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?

Jul 16 '05 #2
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." <yo**************************@yahoo.com> wrote in message
news:3X**********************@news2.calgary.shaw.c a...

"fartsniff" <fa**@sniff.com> wrote in message
news:vg************@corp.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?

Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sybren Stuvel | last post by:
Hi there, Is it possible to use an assignment in a while-loop? I'd like to do something like "loop while there is still something to be read, and if there is, put it in this variable". I've been...
11
by: Wayne Folta | last post by:
Two observations about PEP-315: 1. It's clever, addresses a definite "wart", and is syntactically similar to try/except. But it's syntax seems like an acquired taste to me. 2. It is a very...
15
by: Robin Eidissen | last post by:
What I try to do is to iterate over two variables using template metaprogramming. I've specialized it such that when it reaches the end of a row ot starts on the next and when it reaches the last...
1
by: deko | last post by:
This sub pulls Outlook Appointments into a table. The problem is I want to limit the import to Location = Boston I'm not sure how to code that into the For... To loop - As it is now, I get the...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
6
by: Alexnb | last post by:
Uhm, "string" and "non-string" are just that, words within the string. Here shall I dumb it down for you? string = "yes text1 yes text2 yes text3 no text4 yes text5+more Text yes text6 no...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.