Parse a concatenated variable and string?

Reply

Join Date: Oct 2004
Posts: 1
Reputation: poobaah is an unknown quantity at this point 
Solved Threads: 0
poobaah poobaah is offline Offline
Newbie Poster

Parse a concatenated variable and string?

 
0
  #1
Oct 29th, 2004
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

 <input name="<?php echo $myrowInstrument['instrument']; ?>" type="checkbox" value="<?php echo $myrowInstrument['instrument']; ?>">

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['instrument']; ?>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


<?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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 53
Reputation: SimonMayer is an unknown quantity at this point 
Solved Threads: 10
SimonMayer SimonMayer is offline Offline
Junior Poster in Training

Re: Parse a concatenated variable and string?

 
0
  #2
Aug 13th, 2009
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".

  1. print ${$instrument."number"};
Regards,

Simon Mayer
Website design by Ribbontree
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC