简体   繁体   English

在php表单模板变量中嵌入php函数

[英]Embedding a php function within a php form template variable

I'm trying to use a php function within a template html form that is being assigned as a php variable. 我正在尝试在被分配为php变量的模板html表单中使用php函数。 I know that I can use standard $ variables and they will display ok, but I don't know how to use a function. 我知道我可以使用标准$变量,它们将显示为ok,但我不知道如何使用函数。

Essentially, I have a database with a set of values that I want to use to populate a drop down select form. 本质上,我有一个数据库,其中包含一组要用于填充下拉选择表单的值。 I need to display the form in a couple of places so it makes sense to template the form with a variable. 我需要在几个地方显示表单,因此用变量来模板化模板是有意义的。

So far I have the following PHP. 到目前为止,我有以下PHP。 Any help would be gratefully received! 任何帮助将不胜感激! The array works fine - but displaying it within the form I'm struggling with. 该数组可以正常工作-但可以在我苦苦挣扎的表格中显示它。

 $query = "SELECT property.propertyid, property.reference, property.userid, users.userid, users.username FROM property LEFT JOIN users ON property.userid = users.userid WHERE property.userid = ".$varuserid." "; $result = mysql_query($query) or die(mysql_error()); $listarr=array(); while($qy = mysql_fetch_array($result)){ $listarr[] = "<option value=\\"".$qy['propertyid']."\\">".$qy['reference']."</option>"; } $form = <<<HTML <form name="insert" method="post" action="?page=account&amp;a=insert&amp;form=1"> <table width="100%" border="0" cellpadding="4" cellspacing="0"> <tr> <td width="24%" align="left" valign="top">Property ID</td> <td width="76%"> <select name="propertyid" size="1"> foreach($listarr as $key => $value) { echo $value } </select> </td> </tr> <tr> <td align="left" valign="top">&nbsp;</td> <td> <input name="userid" type="hidden" id="userid" value=$varuserid /> <input type="submit" name="submit" value="Insert transaction!" /></td> </tr> </table> </form> HTML; 

Thanks, Simeon enter code here 谢谢,Simeon enter code here

This is what I tend to do: 我倾向于这样做:

<select name="propertyid" size="1">    
<?php foreach($listarr as $key => $value) { ?>
    <option value="<?php echo $value; ?>"><?php echo $value; ?></option> 
<?php } ?>
</select>

That way you can still see the format on the page :) 这样,您仍然可以在页面上看到格式:)

You cannot use a loop inside a heredoc but you can use variables. 您不能在Heredoc内部使用循环,但可以使用变量。

As you already have your list, what you can do is: 有了清单后,您可以执行以下操作:

while($qy = mysql_fetch_array($result)){
    $listarr[] = "<option value=\"".$qy['propertyid']."\">".$qy['reference']."</option>";
}
$list = implode(" ", $listarr);

And in your heredoc: 在您的heredoc中:

<select name="propertyid" size="1">
    {$list}
</select>

Edit: Note that you need to do that for all variables you include in the heredoc, so: 编辑:请注意,您需要对heredoc中包含的所有变量执行此操作,因此:

<input name="userid" type="hidden" id="userid" value={$varuserid} />
                                                     ^          ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM