Page 1 of 1
PHP to assign a variable as a variable
Posted: Wed Sep 06, 2006 3:42 am
by matth2004
Hi,
I'm having a problem here. This is my situation:
I have a page that gets generated with names of members in one column, and a text box next to it in the other column on the same row. The text box has that members number as its name. So say Anothony has member number 241 then the left column will say Anthony and the right will have a text box with the underlying html being <input type="text" size="5" name="241">
If your still with it and not confused keep reading. Ok, I want to extract the data from box 241 after it has been submitted to the php page. Yes I already know how to do this but this time it's different. I have a loop which goes from 1 to however many members there are.
$query="SELECT * FROM licenses";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
//code needed here
}
$i++;
Where the code needed here is where I need to extract the data from box 241, or whatever box I want for that matter. The $i is the current member number it is on. Yet I need to extract the data from the variable of that current member number because that is what the box is named. I can't go $memberscore = $i because i will be the current member number and not the score.
Say $i was currently at member 241, that's Anthony. I need to do something like $memberscore = $241 and convert the number in $i to a variable. If you get what I mean so far.
Any help would be much appreciated. I was thinking of something like $$i meaning the variable of the number inside $i but I don't know if that will work.
Regards,
Matt
Posted: Wed Sep 06, 2006 3:51 am
by Luke
http://us3.php.net/manual/en/language.v ... riable.php - Variable variables is what you are referring to... but I wouldn't use them for this... I wouldn't name the columns by the customer id in the first place. place a hidden input in the form with the id in it instead:
Code: Select all
<input type="hidden" name="person_id" value="241" />
Posted: Wed Sep 06, 2006 3:55 am
by matth2004
That doesn't matter because it's just a local tipping club. Also the text boxes need to be visible because the user needs to input the members score for that round in the tipping.
Thatks heaps for your help, I have been dieing to know an answer to this.
Regards,
Matt
Posted: Wed Sep 06, 2006 4:02 am
by Luke
matth2004 wrote:That doesn't matter because it's just a local tipping club.
What doesn't matter?
matth2004 wrote: Also the text boxes need to be visible because the user needs to input the members score for that round in the tipping.
I only posted the id portion of the form... you still would need to build all of your "visible" input elements
EDIT: OH and now that I think about it... what you are talking about wouldn't work anyway because you can't name a variable $241 because the name can't start with a number. You are going about this the wrong way
Posted: Wed Sep 06, 2006 4:07 am
by matth2004
The Ninja Space Goat wrote:I wouldn't name the columns by the customer id in the first place. place a hidden input in the form with the id in it instead:
Code: Select all
<input type="hidden" name="person_id" value="241" />
I'm not naming columns by the member number yet I'm naming the input of the form with the persons ID. What I showed was what it looks like after it has been generated.
Thanks for your help,
Regards,
Matt
Posted: Wed Sep 06, 2006 4:10 am
by Luke
matth2004 wrote:I'm not naming columns by the member number yet I'm naming the input of the form with the persons ID. What I showed was what it looks like after it has been generated.
What exactly are you trying to do? Like... what is the bigger picture... what does the form actually do? Maybe I can help you with your logic... did you read where I edited my post above?
Posted: Wed Sep 06, 2006 4:22 am
by matth2004
Don't worry about it. It's all working now and I don't want to get confused lol. Thanks for your help.
Regards,
Matt
Posted: Wed Sep 06, 2006 9:54 am
by Luke

why would you get confused?
What did you end up doing?
Posted: Wed Sep 06, 2006 6:21 pm
by matth2004
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I'm just a little new to it all in my only big project, yes I call it big, was setting up an admin panel that allows you to administer users in that you can add them, delete them, change their emails or passwords, send them an admin message, generate a keygen to add to the database, delete keys from the database, email the keys to the user etc.
Ok what I ended up doing for generating the page with all the members and boxes was:
Code: Select all
echo "<head>
<meta http-equiv='Content-Language' content='en-au'>
<meta name='GENERATOR' content='Matty H'>
<meta http-equiv='Content-Type' content='text/html; charset=windows-1252'>
<title>New Family Round</title>
</head>
<body bgcolor='#000000'>
<font face='Verdana' size='2' color='#FFFFFF'>
<form action='tippingtools.php' method='POST'>
<input type='hidden' name='function' value='processfamilyround'>
<table border='0' width='100%' cellpadding='1'>
<tr>
<td><font face='Verdana' size='2' color='#FFFFFF'><u><b>Member Number</b></u></font></td>
<td><font face='Verdana' size='2' color='#FFFFFF'><u><b>Name</b></u></font></td>
<td><font face='Verdana' size='2' color='#FFFFFF'><u><b>Add Score</b></u></font></td>
</tr>";
$query="SELECT * FROM families";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$member=mysql_result($result,$i,"member");
$name=mysql_result($result,$i,"name");
echo "
<tr>
<td><font face='Verdana' size='2' color='#FFFFFF'>$member</font></td>
<td><font face='Verdana' size='2' color='#FFFFFF'>$name</font></td>
<td><font face='Verdana' size='2' color='#FFFFFF'><input type='text' size='2' name=$member></font></td>
</tr>
";
$i++;
}
echo"
</table>
<br>
<br>
Please type the round number before clicking Submit: <input type='text' name='roundnum' size='3'><p>
<input type='submit' value='Submit'>
</form>
</body>
</html>";
And the form had a hidden variable which sent it to another function to process the results:
Code: Select all
$query="SELECT * FROM families";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$member=mysql_result($result,$i,"member");
$score=mysql_result($result,$i,"score");
$name=mysql_result($result,$i,"name");
$addtoscore=${$member};
$updatedscore = $score + $addtoscore;
mysql_query("UPDATE families SET score='$updatedscore' WHERE member='$member'") or die ('Sorry but there has been an error in updating the database.');
$email=mysql_result($result,$i,"email");
$from="emailaddresshere@cantsay.com"
$subject="Your Score: Round $roundnum";
$message="Hi $name,
We have just finished processing the results and here is your score:
Total from Last round: $score
What you scored this round: $addtoscore
Your new total: $updatedscore
Good luck in your tipping for next round,
Brian and Stacey Murphy";
mail("$email","$subject","$message","From: $from")
$i++;
}
echo"
<html>
<head>
<title>Successfully added</title>
</head>
<body bgcolor=#000000>
<font face='Verdana' size='2' color=#FFFFFF>
<center>
<p> </p>
<p> </p>
<p> </p>
The round has successfully been added and all families have been updated with the new scores
<br>
Have a nice day
</center>
</font>
</body>
</html>
";
Regards,
Matt
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]