使用 fopen 和 str_replace 自动填充选择选项
我有这个文件 'gardens.php',它从名为 'generalinfo' 的表中提取数据,我使用 fopen 将该数据发送到名为 ' 的文件索引.html'。这是问题,只填了一个选项。我在这里运行一个演示 Garden Demo <-- 这是新更新的页面和位置,错误比我本地的错误多如果有人可以帮助我修复它们 用户名:stack1 密码:stack1
有更好的方法来实现我想要的吗?
GARDENS.PHP
<?php
include("connect.php");
$results = mysql_query("SELECT * FROM generalinfo");
while($row = mysql_fetch_array($results)){
$country = $row['country'];
$province = $row['province'];
$city = $row['city'];
$address = $row['address'];
//echo $country;
//echo $province;
//echo $city;
//echo $address;
}
$fd = fopen("index.html","r") or die ("Can not fopen the file");
while ($buf =fgets($fd, 1024)){
$template .= $buf;
}
$template = str_replace("<%country%>",$country,$template);
echo $template;
?>
INDEX.PHP 片段
<form name="filter" method="get" action="filter.php">
<select class="country" name="country">
<option><%country%></option>
</select>
</form>
CONNECT.PHP
<?php
mysql_connect("mysql.andcreate.com", "*******", "********")or die("Cannot Connect to DB");
mysql_select_db("gardencollective")or die("cannot select the DB table");
mysql_close();
?>
I have this file 'gardens.php', which pulls data from a table called 'generalinfo' and I use fopen to send that data to a file called 'index.html'. Here is the issue, only one option is filled. I have a demo running here Garden Demo <-- this is a new updated page and location, there are more errors than I have locally If anyone could help me fix them
Username:stack1 Password:stack1
Is there a better way to achieve what I want to?
GARDENS.PHP
<?php
include("connect.php");
$results = mysql_query("SELECT * FROM generalinfo");
while($row = mysql_fetch_array($results)){
$country = $row['country'];
$province = $row['province'];
$city = $row['city'];
$address = $row['address'];
//echo $country;
//echo $province;
//echo $city;
//echo $address;
}
$fd = fopen("index.html","r") or die ("Can not fopen the file");
while ($buf =fgets($fd, 1024)){
$template .= $buf;
}
$template = str_replace("<%country%>",$country,$template);
echo $template;
?>
INDEX.PHP SNIPPET
<form name="filter" method="get" action="filter.php">
<select class="country" name="country">
<option><%country%></option>
</select>
</form>
CONNECT.PHP
<?php
mysql_connect("mysql.andcreate.com", "*******", "********")or die("Cannot Connect to DB");
mysql_select_db("gardencollective")or die("cannot select the DB table");
mysql_close();
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否也在其他 PHP 文件中使用此代码片段?如果没有,您可以集成到您的 Gardens.php 文件中:
Do you use this snippet in some other PHP file as well? If not you can just integrate into your gardens.php file: