Jump to content


Photo

variables in variable names


  • Please log in to reply
7 replies to this topic

#1 JJBlaha

JJBlaha
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 26 October 2006 - 08:51 PM

i want to make a variable with a variable name in it. something like
$n = 1;
while (whatever){

$b = $Array['b']; 
${$n}a = $b

$n++
}

then later on in the script to get the value of $b at the first run of the while statement i would use

$1a

and

echo "$5a';

should output the fifth part of $Array['b']; 


Is there any way i can do this?

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 October 2006 - 08:56 PM

You want to use variable variables. Read the section on variables.  Variable names can not start with a number.

Ken

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 26 October 2006 - 08:57 PM

I am sorry, but a variable name cannot start with a number as far as I know.

Orio.
Think you're smarty?

(Gone until 20 to November)

#4 JJBlaha

JJBlaha
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 26 October 2006 - 08:59 PM

I forgot that, then something along the lines of

$n = 1;
while (whatever){

$b = $Array['b'];
$a{$n} = $b

$n++
}

echo "$a5';

should output the fifth part of $Array['b'];




#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 October 2006 - 09:03 PM

You can do something like that, but why would you want to? You can access the fifth part of your example array by doing:
<?php echo $b[5] ?>

Ken

#6 JJBlaha

JJBlaha
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 26 October 2006 - 09:19 PM

sorry if i was not clear enough, ill try to explain exactly what i need to do.

<?
$n1 = 0;

include('dbconn.php');
$sql = "select * from formfields WHERE category='$category' order by formfield";
$result = @mysql_query($sql, $dbh);
while ($Array = @mysql_fetch_array($result)){

    $formfield = $Array['formfield']; 
$n1 = ($n1 + 1);

echo "<tr><td>$formfield: <br><select name=\"title$n1\">";
$sql2 = "select * from formoptions WHERE formfield='$formfield' order by formoption";
$result2 = @mysql_query($sql2, $dbh);
while ($Array = @mysql_fetch_array($result2)){

$formoption = $Array['formoption'];
echo "<option name=\"$formoption\">$formoption</option>";
}

echo "</select></br>";
}


?>
which creates a bunch of forms that looks like
ExampleForm1: <select name="info1"> bunch of options </select>
ExampleForm2: <select name="info2"> bunch of options </select>
ExampleForm3: <select name="info3"> bunch of options </select>

etc...

the variable $formfield is the name of the form. in this case ExampleForm1, ExampleForm2, etc
after the user has filled in the forms i want to input both the name of the form and the user input into a database.  so for each form made i would do something similiar to

<?
$info = $_POST['info1'];
$formfield = ??????;


$sql3 = "insert into forms (title, info) values ('$formfield', 'info')";
?>

I hope this makes it clearer :)

#7 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 27 October 2006 - 08:08 AM

change <select name=\"title$n1\">"; to <select name=\"title[$formfield]\">";
in result page try
foreach ($_POST['title'] as $key => $value) echo "In $key is selected $value<br />";


#8 JJBlaha

JJBlaha
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 27 October 2006 - 07:21 PM

Thanks alot, thats exactly what i needed :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users