Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Get Values from Associative Array

Posted on 2013-12-17
2
Medium Priority
?
460 Views
Last Modified: 2013-12-17
I have a form that collects amounts paid here...

http://buildmomentum.org/register/lc/group_payment.php?id_church=ORG101&total_group_payment=100

When the button is clicked, I need to give the person entering the amounts an opportunity to check their entry.  How do I convert the associative array into variables I can show the user, then pass on to the next page for processing?

I could post code I've tried, but I've tried so many different ways of doing a "For Each" loop and "Implode" from $_POST that I don't know what code to post.  At this point, I think it's easier to start from scratch.

Thanks,

Steve
0
Comment
Question by:stkoontz
2 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 39724054
try:
if(isset($_POST['payment']) && $_POST['payment']=='Confirm totals and make payment.')
{
	foreach($_POST['id_registrant'] as $key=>$value)
	{
		$fieldname=htmlentities($key,ENT_QUOTES,'UTF-8');
		$fieldvalue=intval($value);
		echo sprintf('<div>%s: %d<input type="hidden" name="id_registrant[%s]" value="%d"/></div>',$fieldname,$fieldvalue,$fieldname,$fieldvalue);
	}
}

Open in new window


NOTE: If you were to change the names of your input elements from:
<input type="text" name="id_registrant[lastname63700]" id="id_amount1">

to something like:
<input type="text" name="id_registrant[firstname lastname__63700]" id="id_amount1">

then you could split $key at the double underline to get the first and last names:
if(isset($_POST['payment']) && $_POST['payment']=='Confirm totals and make payment.')
{
	foreach($_POST['id_registrant'] as $key=>$value)
	{
		$temp=explode('__',$key);
		$fullname=htmlentities($temp[0],ENT_QUOTES,'UTF-8');
		$fieldname=htmlentities($key,ENT_QUOTES,'UTF-8');
		$fieldvalue=intval($value);
		echo sprintf('<div>%s: %d<input type="hidden" name="id_registrant[%s]" value="%d"/></div>',$fullname,$fieldvalue,$fieldname,$fieldvalue);
	}
}

Open in new window

0
 
LVL 2

Author Closing Comment

by:stkoontz
ID: 39725256
Thanks for the help.  That worked!

Steve
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

916 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