I have created a class that will create an html input and i need the value and description for the input to be pre-existing php variables. Here is the function: function create_input($name) { echo '<div id="'.$name.'"><input type="radio" class="radio" name="cpu" value="<?='.$name.'_price;?>" checked="checked" /> <?=$'.$name.'_description;?> - <span id="'.$iname.'price"></span></div>'; } Code (markup): So what this does is take the name, which for example might be dog1, and creates an input that uses a pre-existing variable dog1_price and uses that as the value and uses the pre-existing variable dog1_description as the description. This works perfectly if I do not create the function and have the php code in the html code regularly, but because I have many inputs, this function is necessary. This code does not work because instead of the php code echoing the variable (lets say 99.99 for the value), it shows up as echoing the php code directly so the source code ends up looking like this: <div id="dog1"><input type="radio" class="radio" name="cpu" value="<?=dog1_price;?>" checked="checked" /> <?=$dog1_description;?> - <span id="price"></span></div> Code (markup): If someone could help me with this, echoing preexisting variables somehow, i would very greatly appreciate it.
That is not the problem, see if this makes more sense: Function: function create_input($name) { $name_price = $name.'_price'; $name_description = $name.'_price'; echo "<div id='$name'><input type='radio' class='radio' name='cpu' value='<?=$$name_price ?>' checked='checked' /> <?=$$name_description ?> - <span id='$iname'></span></div>"; } Code (markup): HTML Source Code: <div id='cpu1'><input type='radio' class='radio' name='cpu' value='<?=$cpu1_price?>' checked='checked' /> <?=$cpu1_description?> - <span id=''></span></div> Code (markup): Notice how the source code outputs the PHP code like text and doesnt access the pre-existing variables I have for $cpu1_price and $cpu1_description
I see now. Try changing the function to this instead: function create_input($name) { $name_price = $name.'_price'; $name_description = $name.'_price'; echo "<div id='$name'><input type='radio' class='radio' name='cpu' value='$name_price' checked='checked' /> $name_description - <span id='$name'></span></div>"; } PHP:
I haven't tested it, but I think you need $$name_price and $$name_description in the echo. Then if $name is equal to dog1 then $name_price will equal "dog1_price" and $$name_price will equal the value of $dog1_price. Don't forget, like I almost did, to add at about line 3 of your function global $$name_price; global $$name_description PHP:
echo "<div id='$name'><input type='radio' class='radio' name='cpu' value='${$name."_price"}' checked='checked' /> $name_description - <span id='$name'></span></div>"; I think the above should work..?
I think you need to fix that and i hope you can do it with the help of my friend from russia. Please visit his website: www.cyberhubweb.com
<?php function create_input($name) { global ${${name}."_price"}; global ${${name}."_description"}; echo "<div id='$name'><input type='radio' class='radio' name='cpu' value='${${name}.'_price'}' checked='checked' /> ${${name}.'_description'} - <span id='$name'></span></div>"; }?> Code (markup):
Try this:- function create_input($name) { // Allow access to variables inside function global ${$name.'_price'}, ${$name.'_description'}; // Construct variables $price = ${$name.'_price'}; $desc = ${$name.'_description'}; // Output HTML echo '<div id="'.$name.'"><input type="radio" class="radio" name="cpu" value="'.$price.'" checked="checked" />'.$desc.' - <span id=""></span></div>'; } PHP: (I wasn't really sure what exactly the id for the span was meant to be so I've removed it.) Example:- $dog1_price = 3.00; $dog1_description = 'A dog'; create_input('dog1'); PHP: Output:- <div id="dog1"><input type="radio" class="radio" name="cpu" value="3" checked="c hecked" />A dog - <span id=""></span></div> HTML: