如何在单个页面中两次显示数据获取记录? [英] How to display data fetch record twice in the single page?

查看:67
本文介绍了如何在单个页面中两次显示数据获取记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在单个页面中两次提取国家/地区名称.我有四个下拉菜单,分别是country_1,state_1,country_2,state_2.

I have to fetch country name twice in the single page. I have four dropdowns which is country_1,state_1, country_2, state_2.

在国家/地区中,用户选择国家/地区名称,然后根据国家/地区名称显示州名称. 如果我仅使用country_1和state_1,则可以显示它,但是我需要在同一页面上同时显示两个国家/地区.

In the country, User select the country name and according to the country name, state name will display. If I use only country_1, state_1 then I am able to display it but I need both countries dropdown on the same page.

我尝试了$stmt->data_seek($stmt,0); $stmt->data_seek(0);,但仍然无法显示.

I tried $stmt->data_seek($stmt,0); $stmt->data_seek(0); but still not able to display it.

我只想知道应该在哪里使用data_seek

I just want to know where should I use the data_seek

<!--country name-->
$country_list="SELECT id,name FROM countries";
/* create a prepared statement */
if ($stmt = $conn->prepare($country_list)) {
    /* execute statement */
    $stmt->execute();
    /* bind result variables */
    $stmt->bind_result($id, $country_name);
    }
   <select  name="country_data" class="myselect form-control country_data">
                        <option  value="" disabled selected>Select your country</option>
                            <?php  while ($stmt->fetch()) {?>
                            <option value="<?php echo $id;?>"><?php echo $country_name;?></option>
                            <?php }?>
                     </select>

    <!--state name-->
                <select  name="state"  class="myselect form-control state_get" >
                <option value=''>Select state</option>
                </select>

    <!--country name-->
     <select  name="country_data" class="myselect form-control country_data">
                        <option  value="" disabled selected>Select your country</option>
                            <?php  
                             $stmt->data_seek($stmt,0);
                             while ($stmt->fetch()) {?>
                            <option value="<?php echo $id;?>"><?php echo $country_name;?></option>
                            <?php }?>
                     </select>

    <!--state name-->
                <select  name="state"  class="myselect form-control state_get" >
                <option value=''>Select state</option>
                </select>

推荐答案

由于两种情况下的输出都相同,因此仅循环遍历一次数据会更加有效.以下代码仅使用一个循环来创建html选项.它将其存储在变量中.然后,只需回显变量即可在两个地方使用它.

Since the output is the same in both cases, it's much more efficient to loop through the data only once. The following code only uses one loop to create the html options. It stores it in a variable. You can then use it in both places just by echoing out the variable.

<?php
    $options = "";
    while ($stmt->fetch()){
        $options .= "<option value='$id'>$country_name</option>\n";
    }
?>


<!--country name 1-->
<select  name="country_data1" class="myselect form-control country_data">
    <option  value="" disabled selected>Select your country</option>
    <?php  echo $options ?>
</select>

<!--state name 1-->
<select  name="state1"  class="myselect form-control state_get" >
    <option value=''>Select state</option>
</select>

<!--country name 2-->
<select  name="country_data2" class="myselect form-control country_data">
    <option  value="" disabled selected>Select your country</option>
    <?php echo $options ?>
</select>

<!--state name 2-->
<select  name="state2"  class="myselect form-control state_get" >
    <option value=''>Select state</option>
</select>

我更改了name属性,因为您可能不希望它们具有相同的名称.目前,我无法对此进行测试,因此,如果您遇到任何错误,请告诉我.

I have changed the name attributes, because you probably do not want them to have the same name. I am not able to test this at the moment, so let me know if you encounter any errors.

此外,您尚未实现状态"(State)下拉列表的代码,到这一点时,您可能要考虑使用AJAX.

Also, you have not yet implemented the code for the "State" dropdowns, when you get to that point, you may want to consider using AJAX.

我在每个选项行的末尾添加了\ n,以使您的源代码在查看源代码时更易于在浏览器中阅读.但是,这不是必需的,您可以根据需要将其删除.

I added the \n at the end of each option line to make your source code a bit easier to read to read in the browser if you are viewing the source. However, it is not necessary and you can remove it if you prefer.

编码第一节的另一种方法(如果您不使用bind_result,如下所示:

An alternate way to code the first section (if you do not use bind_result is as follows:

<?php
    $options = "";
    while ($result = $stmt->fetch()){
        $options .= "<option value='{$result['id']}'>{$result['name']}</option>\n";
    }
?>

这篇关于如何在单个页面中两次显示数据获取记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆