1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Echo php code to be used among the html code?

Discussion in 'PHP' started by stompergames, Jun 7, 2009.

  1. #1
    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.
     
    stompergames, Jun 7, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Try changing this:
    value="<?=dog1_price;?>"

    to this:

    value="<?=$dog1_price;?>"
     
    jestep, Jun 7, 2009 IP
  3. stompergames

    stompergames Peon

    Messages:
    222
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    stompergames, Jun 7, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    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:
     
    jestep, Jun 7, 2009 IP
  5. TecBrat

    TecBrat Member

    Messages:
    31
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    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:
     
    TecBrat, Jun 8, 2009 IP
  6. optimeramera

    optimeramera Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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..?
     
    optimeramera, Jun 9, 2009 IP
  7. korquee

    korquee Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7


    Simple just visit the php.net website.
     
    korquee, Jun 9, 2009 IP
  8. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #8
    why would you need $$var
    This post is sorta goin <Dooo Deee Doo Deerrrrr>
     
    ezprint2008, Jun 10, 2009 IP
  9. korquee

    korquee Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9

    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
     
    korquee, Jun 10, 2009 IP
  10. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #10
    
    <?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):
     
    stOK, Jun 10, 2009 IP
  11. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #11
    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:
     
    clarky_y2k3, Jun 10, 2009 IP