How to display data fetch record twice in the single page?

送分小仙女□ 提交于 2019-12-11 15:39:52

问题


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>

回答1:


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>

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 \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.

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



回答2:


$data = array();
$i=0;
while($stmt->fetch()) {
   $data[$i]['id'] = $id;
   $data[$i]['country_name'] = $country_name;
   $i++;
}

then try

foreach($data as $row){

}



回答3:


try to save fetched data in variable.

$data = $stmt->fetch();

then use foreach loop wherever you want to use.

foreach($data as $row){

}


来源:https://stackoverflow.com/questions/46905248/how-to-display-data-fetch-record-twice-in-the-single-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!