Calling function from within variable?

Discussion in 'PHP' started by PoPSiCLe, Mar 29, 2009.

  1. #1
    Hey.

    I've got a variable which changes depending on the conditions. That's normal, ofc.

    This variable prints some HTML. Within this HTML is a select-box, with the options (hopefully) pulled from a function - but how do I do that?

    Currently, I have as follows:
    
    $date_paid = "<td class='a_right'>
    			<form>
    			<select class='formlook' name='paid_day'>
                            option_values(31);
    			</select>
    			<select class='formlook' name='paid_month'>
    			option_months();
    			</select>
    			<input class='formlook' type='text' name='paid_year' size='4' value='' />
    			</form>
    		</td>";
    
    PHP:
    What I want is of course for the option_value() and the option_month() functions to be parsed - and I can't seem to get it to work.

    Anyone have an idea as to how I can do this?

    (Yes, I know I could echo the if-statement, but that won't work, as it has to be contained inside that $date_paid variable
     
    PoPSiCLe, Mar 29, 2009 IP
  2. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $date_paid = "<td class='a_right'>
    <form>
    <select class='formlook' name='paid_day'>".option_values(31)."
    </select>
    <select class='formlook' name='paid_month'>".option_months()."
    </select>
    <input class='formlook' type='text' name='paid_year' size='4' value='' />
    </form>
    </td>";
     
    NatalicWolf, Mar 29, 2009 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Tried that, does not work. For some reason, I'm not sure why - that was the first thing I tried. It prints the values, all right, but for some reason it leaves the other parts out - ie. if I use your method, it prints the option_values(31) out as <option>1</option><option>2</option> etc. (which of course is what it is supposed to do) but for some reason the rest of the code (the <td><select> etc.) is not displayed. I'm not sure why...

    I'll add some of the code:
    
    if ($members[date_paid] == 00-00-0000) {
    		$date_paid = "<td class='a_right'>
    		<form>
    			<select class='formlook' name='paid_day'>".option_values(31)."
    			</select>
    			<select class='formlook' name='paid_month'>".option_months()."
    			</select>
    			<input class='formlook' type='text' name='paid_year' size='4' value='' />
    		</form>
    		</td>";
    	$sum_paid = "<td class='a_right'><form method='post' action='$domain/update_list.php'>
    			<input class='hidden' name='uniqueID' value='$uniqueID' />
    <input class='formlook' type='text' name='sum_paid' size='4' maxlength='4' value='$amount' /><input type='submit' value='Oppdater' /></form></td>";
    
    		}
    	echo "<tr>
    		$name$address$mail$phone$birthdate$date_paid$sum_paid
    		</tr>";
    
    PHP:
    The rest of the variables in the echo-statement is defined earlier, and is working as intended
     
    PoPSiCLe, Mar 29, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There's no reason NatalicWolf's suggestion shouldn't work. Have you looked at the HTML source that was generated? Maybe your option_values() function is leaving an HTML tag or a quotation mark open.
     
    SmallPotatoes, Mar 29, 2009 IP
  5. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think its echoing not returning. Please post the full code.
     
    NatalicWolf, Mar 30, 2009 IP
    PoPSiCLe likes this.
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    You might very well be right on that one, yes.

    The code for the option_values()-function is as follows:
    
    function option_values($max)
    {
    $options = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31');
    $options_limited = array_slice($options,0,$max);
    
    foreach ($options_limited as $value) {
    	echo "<option>$value</option>";
    }}
    
    PHP:
     
    PoPSiCLe, Mar 30, 2009 IP
  7. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Score one more for NatalicWolf. Instead of echoing, concatenate the stuff in a variable and then return that variable.

    $r = '';
    foreach (blahblah)
       $r .= "blahblah";
    return $r;
    Code (markup):
     
    SmallPotatoes, Mar 30, 2009 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    Ah. Superb, both of you! +rep added!
     
    PoPSiCLe, Mar 30, 2009 IP