database array to variable name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kenton
Forum Newbie
Posts: 2
Joined: Tue Jul 05, 2011 6:45 pm

database array to variable name

Post by kenton »

I use a database to create several form fields. The database gives me numbers that I add a couple of letters to and create radio buttons.
The database may give me rdid = 4, 8, 24. I use this to create radio buttons.

Code: Select all

$name=sc4; <INPUT Type='Radio' Name='$name'>
and repeat this with the other numbers.
I submit this to the next page and I want to get the value of these variables. I know rdid = 4, 8, 24 but I do not know how to get this variable. I have tried this but I am missing something.

Code: Select all

$query3 = "SELECT * FROM table where rid LIKE '$rid'"; 
$result3 = mysql_query($query3) or die(mysql_error());
while($rdid = mysql_fetch_array($result3)){ 
$sc="sc" . $rdid['rdid'];
$score=$_POST['sc'];
echo "$sc -  $score<br>";
}
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: database array to variable name

Post by social_experiment »

Code: Select all

<?php
 while($rdid = mysql_fetch_array($result3)){ 
$sc="sc" . $rdid['rdid'];
}
?>
What does $sc contain? The method you used is correct in retrieving the value from the database.

Code: Select all

<?php
echo "$sc  -  $score<br>";
// try
echo $sc . " - " . $score . "<br />";
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kenton
Forum Newbie
Posts: 2
Joined: Tue Jul 05, 2011 6:45 pm

Re: database array to variable name

Post by kenton »

I solved my problem. This gets values for all variables postes on the previous page.

Code: Select all

 $query_string = "";
 if ($_POST) {
   $kv = array();
   foreach ($_POST as $key => $value) {
     $kv[] = "$key=$value";
   }
   $query_string = join("&", $kv);
 }
 else {
   $query_string = $_SERVER['QUERY_STRING'];
 }
 echo $query_string;
http://php.net/manual/en/reserved.variables.post.php
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: database array to variable name

Post by AbraCadaver »

The second post doesn't seem relevant to the first post, but instead of the loop just use:

Code: Select all

$query_string = http_build_query($_POST);
Also notice that you can have a $_POST array and a get query string:
[text]<form method="post" action="index.php?val=1">
<input type="submit" name="submit" value="2">
</form>[/text]
Would give a query string of val=1 and the $_POST array would contain submit => 2.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply