| | |
Parse a concatenated variable and string?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2004
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2008
Posts: 53
Reputation:
Solved Threads: 10
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".
${$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".
PHP Syntax (Toggle Plain Text)
print ${$instrument."number"};
![]() |
Similar Threads
- Parse JavaScript Variable to a XSLT Variable (XML, XSLT and XPATH)
- setting a string as a variable name (Python)
- Convert string variable into form object in VB6 (Visual Basic 4 / 5 / 6)
- How to Define size of variable string during runtime (Visual Basic 4 / 5 / 6)
- Making hashes on the fly with name dictated by a variable string (Perl)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
Other Threads in the PHP Forum
- Previous Thread: Create file '$tablename'.php
- Next Thread: Dynamic form generation using php...?
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube





