954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Parse a concatenated variable and string?

Hi,

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.

<?php

//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['instrument'];
echo ": ";

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

 

So far so good.

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

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:

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


<?php

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

    while ($myrowInstrument_id = mysql_fetch_array($resultInstrument_id))
    {
    $instrument = $myrowInstrument_id['instrument'];

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.

Thanks,
Paul.

poobaah
Newbie Poster
1 post since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

To print $Flutenumber you would need to produce a variable like so:
${$instrument."number"};
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"};
SimonMayer
Junior Poster in Training
53 posts since Apr 2008
Reputation Points: 14
Solved Threads: 10
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You