Solved

PHP Get Values from Associative Array

Posted on 2013-12-17
2
435 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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now