Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Building a new iterated loop...recommendations?

Posted on 2011-09-18
6
Medium Priority
?
184 Views
Last Modified: 2012-06-27
I have an order line INSERT that I want to iterate through as many as 15 separate "order line items", but I'm not sure how to build my code to iterate cleanly through all the possible lines. The requirements:
1) I will check for at least one order line (from any camper) in the validation of the previous page's form. Don't need to worry about that here.
2) Every camper need not to have an order line. In the page's initial $_POST array below, camper[1] does not have any order lines.
3) I check for an isset() before processing an order line.
4) I have to assign two variables from the session ID because I use two instances in the processing loop.

Now, my code manages 6 loops. Not enough. And very clumsy. I can't do this 15 times! Please help,

Karl
$_POST Array
(
    [Camper_0_PK] => 490
    [Camper_0_Session_1_List] => 3
    [Camper_0_Session_2_List] => 3
    [Camper_0_Session_3_List] => 
    [Camper_1_PK] => 491
    [Camper_1_Session_1_List] => 
    [Camper_1_Session_2_List] => 
    [Camper_1_Session_3_List] => 
    [Marketing_FreeText] => 
    [Marketing_CouponCode] => 
    [SessionRegSubmit] => Submit
)


Current order line processing code:

// process order line items insert
			// TODO: figure out how to make this into an iterating loop
			if (isset($_POST['Camper_0_Session_1_List'])){
				
				// must retrieve the program FK for the selected session
				$SessionPK = $_POST['Camper_0_Session_1_List'];
				$OrderLISessionFK = $_POST['Camper_0_Session_1_List'];
				$OrderLICamperFK = $_POST['Camper_0_PK'];
				
				// run through line item insert
				require ('panels/panel.order_line_item_insert.php');
			}
			if (isset($_POST['Camper_0_Session_2_List'])){
				
				// must retrieve the program FK for the selected session
				$SessionPK = $_POST['Camper_0_Session_2_List'];
				$OrderLISessionFK = $_POST['Camper_0_Session_2_List'];
				$OrderLICamperFK = $_POST['Camper_0_PK'];
				
				// run through line item insert
				require ('panels/panel.order_line_item_insert.php');
			}
			if (isset($_POST['Camper_0_Session_3_List'])){
				
				// must retrieve the program FK for the selected session
				$SessionPK = $_POST['Camper_0_Session_3_List'];
				$OrderLISessionFK = $_POST['Camper_0_Session_3_List'];
				$OrderLICamperFK = $_POST['Camper_0_PK'];
				
				// run through line item insert
				require ('panels/panel.order_line_item_insert.php');
			}
			// process order line items insert
			if (isset($_POST['Camper_1_Session_1_List'])){
				
				// must retrieve the program FK for the selected session
				$SessionPK = $_POST['Camper_1_Session_1_List'];
				$OrderLISessionFK = $_POST['Camper_1_Session_1_List'];
				$OrderLICamperFK = $_POST['Camper_1_PK'];
				
				// run through line item insert
				require ('panels/panel.order_line_item_insert.php');
			}

...and so on...

Open in new window

0
Comment
Question by:kpisor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36557929
Just because the index is typically a constant string, that's not required.  You can build it.  Coming from C, I tend to use common functions to both, such as sprintf().  

We can have variables for the camper # and the session # which can be controlled by while loops or other means.  

$index = sprintf ( "Camper_%s_Session_%s", $camperNbr, $sessionNbr );
if (isset($_POST[$index]))
{
    ....
}

Open in new window


Does that make sense to you?
0
 
LVL 13

Accepted Solution

by:
Hugh McCurdy earned 1500 total points
ID: 36557941
I forgot to mention you could also build the string using . as well.

$index = "Camper_" . $camperNbr . "_Session_" . $sessionNbr;

Open in new window


This is most likely a better practice than my sprintf() approach.  Old habits are hard to break.
0
 

Author Comment

by:kpisor
ID: 36558023
hmccurdy:

Yes, quite simple. Just make the index as a string, then process against it. I'll give it a try. Anyone else with suggestions for processing this?
0
Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

 

Author Comment

by:kpisor
ID: 36585332
Okay, I've rewritten the code and it works clean with two FOR loops:

// process order line items insert; up to 5 campers and 3 line items per order
			for ($i = 0; $i < 4; $i++) {
				$camperIndex = "Camper_" .$i ."_PK";
				
				for ($j = 1; $j < 4; $j++) {
					$sessionIndex = "Camper_" .$i ."_Session_" .$j ."_List";
					
					// test if session is selected
					if (!empty($_POST[$sessionIndex])){
						
						// retrieve the program FK for the selected session
						// two separate sessionIndex variables will be used in the insert loop
						$SessionPK = $_POST[$sessionIndex];
						$OrderLISessionFK = $_POST[$sessionIndex];
						$OrderLICamperFK = $_POST[$camperIndex];
						
						// run through order line item insert
						require ('panels/panel.order_line_item_insert.php');
					}
					
				} // end session list iteration
					
			} // end camper iteration

Open in new window

0
 

Author Closing Comment

by:kpisor
ID: 36585336
I asked the question as I am new to FOR loops...so it might have helped to be more explicit in managing the FOR setups. Otherwise, thanks!
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36587117
Thanks for the points.  We can certainly help you with how to use any of the keyword operations if you help.  Loops, if/then, etc., are "bread and butter" to a lot of programmers and I expect hundreds if not thousands of experts on EE can help you with those concepts.

If you still need help with for loops, just ask.  If you need help with any other keyword concepts, just ask.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question