带函数的PHP模板
我有一个函数,它必须在html页面上显示他的值,但它不能工作 PHP代码:带函数的PHP模板,php,templates,Php,Templates,我有一个函数,它必须在html页面上显示他的值,但它不能工作 PHP代码: $template = file_get_contents("main.tpl.php"); function updateForm($id) { $query = "SELECT * FROM platen WHERE ID =". $id; $result = mysql_query($query) or die (mysql_error()); while($row = mysql_fe
$template = file_get_contents("main.tpl.php");
function updateForm($id)
{
$query = "SELECT * FROM platen WHERE ID =". $id;
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
$editForm = "<form action='index.php?update&id=". $row['ID'] ."' method='post'>";
$editForm .= "<label for='id'><span>ID:</span></label> <input name='id' id='id' type='text' value='". $row['ID'] ."'></br>";
$editForm .= "<label for='album'><span>Album:</span></label> <input name='album' id='album' type='text' value='". $row['Album'] ."'></br>";
$editForm .= "<label for='band'>Band:</label> <input name='band' id='band' type='text' value='". $row['Band'] ."'></br>";
$editForm .= "<label for='stijl'>Stijl:</label> <input name='stijl' id='stijl' type='text' value='". $row['Stijl'] ."'></br>";
$editForm .= "<label for='medium'>Medium:</label> <input name='medium' id='medium' type='text' value='". $row['Medium'] ."'></br>";
$editForm .= "<label for='datum'>Datum:</label> <input name='datum' id='datum' type='text' value='". $row['Datum'] ."'></br>";
$editForm .= "<label for='prijs'>Prijs:</label> <input name='prijs' id='prijs' type='text' value='". $row['Prijs'] ."'></br>";
$editForm .= "<input class='submit' type='submit' value='Update'>";
$editForm .= "</form>";
}
$template = str_replace("%%editForm%%", $editForm, $template);
}
echo $template;
$template=file\u get\u contents(“main.tpl.php”);
函数updateForm($id)
{
$query=“从压板中选择*,其中ID=”.$ID;
$result=mysql\u query($query)或die(mysql\u error());
while($row=mysql\u fetch\u数组($result))
{
$editForm=“”;
$editForm.=“ID:”;
$editForm.=“相册:”;
$editForm.=“波段:”;
$editForm.=“Stijl:”;
$editForm.=“中等:”;
$editForm.=“数据:”;
$editForm.=“Prijs:”;
$editForm.=“”;
$editForm.=“”;
}
$template=str_replace(“%%editForm%%”,$editForm,$template);
}
echo$模板;
在我的HTML文件中,我得到了%%editForm%%,但当我尝试此操作时,它显示$template未定义。
希望你能帮助我或知道另一个解决方案来显示HTML文件中的数据 它说$template未定义,因为它在functionscope中不可见。 您需要将$template传递给函数或添加
global$template代码>函数内部:
function updateForm($id)
{
global $template;
$query = "SELECT * FROM platen WHERE ID =". $id;
试一试
或者只是将其传递给函数
$template = file_get_contents("main.tpl.php");
function updateForm($id, $template)
{
$query = "SELECT * FROM platen WHERE ID =". $id;
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
$editForm = "<form action='index.php?update&id=". $row['ID'] ."' method='post'>";
$editForm .= "<label for='id'><span>ID:</span></label> <input name='id' id='id' type='text' value='". $row['ID'] ."'></br>";
$editForm .= "<label for='album'><span>Album:</span></label> <input name='album' id='album' type='text' value='". $row['Album'] ."'></br>";
$editForm .= "<label for='band'>Band:</label> <input name='band' id='band' type='text' value='". $row['Band'] ."'></br>";
$editForm .= "<label for='stijl'>Stijl:</label> <input name='stijl' id='stijl' type='text' value='". $row['Stijl'] ."'></br>";
$editForm .= "<label for='medium'>Medium:</label> <input name='medium' id='medium' type='text' value='". $row['Medium'] ."'></br>";
$editForm .= "<label for='datum'>Datum:</label> <input name='datum' id='datum' type='text' value='". $row['Datum'] ."'></br>";
$editForm .= "<label for='prijs'>Prijs:</label> <input name='prijs' id='prijs' type='text' value='". $row['Prijs'] ."'></br>";
$editForm .= "<input class='submit' type='submit' value='Update'>";
$editForm .= "</form>";
}
$template = str_replace("%%editForm%%", $editForm, $template);
}
$template=file\u get\u contents(“main.tpl.php”);
函数updateForm($id,$template)
{
$query=“从压板中选择*,其中ID=”.$ID;
$result=mysql\u query($query)或die(mysql\u error());
while($row=mysql\u fetch\u数组($result))
{
$editForm=“”;
$editForm.=“ID:”;
$editForm.=“相册:”;
$editForm.=“波段:”;
$editForm.=“Stijl:”;
$editForm.=“中等:”;
$editForm.=“数据:”;
$editForm.=“Prijs:”;
$editForm.=“”;
$editForm.=“”;
}
$template=str_replace(“%%editForm%%”,$editForm,$template);
}
试试这个$template=file\u get\u contents(“main.tpl.php”);echo$模板
并查看是否有任何输出,我得到了echo$模板;在我的php文件中,我忘了键入:)我的意思是你看到了什么。当你echo$template时,你看到什么了吗
$template
是函数作用域中的局部变量。把它传进来,然后放回外面。此外,循环和str_replace也不会执行您想要的操作(它将使用仅第一行的表单字段替换%%editForm%%
的所有实例)。是的,$template中函数外部的所有内容都会显示出来。
$template = file_get_contents("main.tpl.php");
function updateForm($id, $template)
{
$query = "SELECT * FROM platen WHERE ID =". $id;
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
$editForm = "<form action='index.php?update&id=". $row['ID'] ."' method='post'>";
$editForm .= "<label for='id'><span>ID:</span></label> <input name='id' id='id' type='text' value='". $row['ID'] ."'></br>";
$editForm .= "<label for='album'><span>Album:</span></label> <input name='album' id='album' type='text' value='". $row['Album'] ."'></br>";
$editForm .= "<label for='band'>Band:</label> <input name='band' id='band' type='text' value='". $row['Band'] ."'></br>";
$editForm .= "<label for='stijl'>Stijl:</label> <input name='stijl' id='stijl' type='text' value='". $row['Stijl'] ."'></br>";
$editForm .= "<label for='medium'>Medium:</label> <input name='medium' id='medium' type='text' value='". $row['Medium'] ."'></br>";
$editForm .= "<label for='datum'>Datum:</label> <input name='datum' id='datum' type='text' value='". $row['Datum'] ."'></br>";
$editForm .= "<label for='prijs'>Prijs:</label> <input name='prijs' id='prijs' type='text' value='". $row['Prijs'] ."'></br>";
$editForm .= "<input class='submit' type='submit' value='Update'>";
$editForm .= "</form>";
}
$template = str_replace("%%editForm%%", $editForm, $template);
}