I am new to PHP and cant seem to find a way to parse a concatenated string and variable.

To explain:

I have a database to catalogue a composers works, one of the tables has a list of instruments which I then access to dynamically display on a wep page using checkboxes - if a particular instrument is used in the work then the checkbox is checked.


//Select all instruments from the instrumentTbl in the correct order

$resultInstrument = mysql_query("SELECT * FROM instrumentTbl ORDER BY sort_order");

//Load them into an array

 if ($myrowInstrument = mysql_fetch_array($resultInstrument)) {
do {
echo $myrowInstrument;
echo ": ";

//This creates a checkbox for each instrument in the database

 <input name="<?php echo $myrowInstrument; ?>" type="checkbox" value="<?php echo $myrowInstrument; ?>">

So far so good.

The problem I am having is when I want to insert the number of instruments used in the work.

<input name="<?php echo $myrowInstrument; ?>number" type="text" size="3">

This snipet of code produces a dynamically created form where the user can enter the number of instruments required. (Ie 2 flutes)  

Here is a part of the form created from the code above for the instrument Flute:

<input name="Flutenumber" type="text" size="3">

To process the information in this form I think I need to concatenate $instrument with the string 'number'.  the only problem is that PHP reads this literally and the output is just $Flutenumber and not the number actually entered.

Here is the code I have at the moment


$resultInstrument_id = mysql_query("SELECT * FROM instrumentTbl ORDER BY sort_order");

    while ($myrowInstrument_id = mysql_fetch_array($resultInstrument_id))
    $instrument = $myrowInstrument_id;

if (strlen ($$instrument !='0'))
//this works fine and gives a list of all the checked instruments
print $$instrument;

//this just outputs the literal interpretation ie it will print $Flutenumber not the actual number entered on the form.

print  "$${$instrument}number";



Any help would be greatly appreciated.


12 Years
Discussion Span
Last Post by SimonMayer

To print $Flutenumber you would need to produce a variable like so:
This is saying that the variable name (contained in curly braces) is made up of $instrument (in this case, Flute) and the text "number".

print  ${$instrument."number"};
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.