dsh125986083 2017-10-24 08:05
浏览 44
已采纳

如何在单页中显示两次数据获取记录?

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.

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.

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

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>
  • 写回答

3条回答 默认 最新

  • dongyong2063 2017-10-26 15:03
    关注

    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>
    ";
        }
    ?>
    
    
    <!--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>
    

    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.

    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.

    I added the 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.

    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>
    ";
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?