使用 fopen 和 str_replace 自动填充选择选项

发布于 2024-08-29 02:35:42 字数 1585 浏览 2 评论 0原文

我有这个文件 '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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-09-05 02:35:42

您是否也在其他 PHP 文件中使用此代码片段?如果没有,您可以集成到您的 Gardens.php 文件中:

<?php
    include("connect.php");
    $results = mysql_query("SELECT * FROM generalinfo");
    $infos = array();
    while($row = mysql_fetch_array($results)){
        $infos[] = $row;
    }
?>

<form name="filter" method="get" action="filter.php">
    <select class="country" name="country">
        <?php foreach($infos as $info): ?>
            <option><?php echo $info['country'] ?></option>
        <?php endforeach; ?>
    </select>
</form>

Do you use this snippet in some other PHP file as well? If not you can just integrate into your gardens.php file:

<?php
    include("connect.php");
    $results = mysql_query("SELECT * FROM generalinfo");
    $infos = array();
    while($row = mysql_fetch_array($results)){
        $infos[] = $row;
    }
?>

<form name="filter" method="get" action="filter.php">
    <select class="country" name="country">
        <?php foreach($infos as $info): ?>
            <option><?php echo $info['country'] ?></option>
        <?php endforeach; ?>
    </select>
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文