Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 22nd, 2005, 04:19 AM
Registered User
 
Join Date: Nov 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Assembling Variables' names in the code?

Greetings,

I have the following problem:

In one form I create elements' names with the following code:

Code:
for ($i=1; $i<=$UserDefinedNumber; $i++)
{
    echo ("<input type="checkbox" name=\"Married_$i\">");
}
Which means that If the user defined the $UserDefinedNumber = 3 it will create 3 checkboxes named: Married_1, Married_2 and Married_3.

Now if we go to the next page and want to retrieve the values of them but we have no idea how many they are, we get a problem. How to "Assemble" the variables' names.

For example I tried the following code:

Code:
for ($i=1; $i<=$UserDefinedNumber; $i++)
{
    if ("\$Married_" . $i == "on")
    {
        echo ("Yes");
    } else {
        echo ("No");
}
As most of you might have already assumed, I get an error. I’ve tried lots of variations in trying to "assemble" the variable's name but so far no success.

The point of doing this is not masochism but the fact that the loop might actually need to run lots of times (from 1 to 100 or even more) so I need to assemble the variable's name with a counter ($i) exactly like I created them in the previous page.

Does anyone know how to do it? I'd really appreciate any help!

- Pericles

P.S.: Using an array for this task [u]has failed</u>. Please check my previous post to see why: http://p2p.wrox.com/topic.asp?TOPIC_ID=34923

 
Old September 22nd, 2005, 09:24 AM
Registered User
 
Join Date: Nov 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yay!

I searched a lot thought the web for long but finally found an answer to my question and of course, I'm submitting it here too! Just in case someone encounters the same problem and wants the solution:

Code:
for ($i=1; $i<$UserDefinedNumber; $i++)
{ 
$AssembledVariable = ${"Married_". $i};

if (!empty($_POST[$AssembledVariable]))
{ 
...Some code here...
}
I'm feel happy for finally solving this out. "Yay!" for the Net and the mad seach skillz of google!

The answer was found at this site : http://www.webmasterworld.com/forum88/9719.htm

I hope this might be useful for others in the future; albeit, if someone comes up with a better idea he's most welcome to share. :)

---

Another PHP novice who cries out for help and feels no shame for it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Error assembling WAR: Deployment descriptor hendragd Spring 0 March 25th, 2008 10:16 AM
UserType Variables--loop thru their names? Paul Gerken VB How-To 10 December 4th, 2006 01:08 PM
Passing Public Variables in Code-Behind striker9 ASP.NET 2.0 Basics 7 February 11th, 2006 03:54 PM
Generate userform' s variables names joewidmer VB Databases Basics 0 September 17th, 2004 03:02 AM
Database names as variables bukky Classic ASP Databases 0 March 15th, 2004 04:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.