Page 1 of 1

database array to variable name

Posted: Tue Jul 05, 2011 7:16 pm
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>";
}

Re: database array to variable name

Posted: Wed Jul 06, 2011 2:46 am
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 />";
?>

Re: database array to variable name

Posted: Wed Jul 06, 2011 1:26 pm
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

Re: database array to variable name

Posted: Wed Jul 06, 2011 1:50 pm
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.