doushizhou4477 2012-04-13 14:33
浏览 63
已采纳

使用先前选择的选项预加载表单

I have written a script that allows me to reorder items on a menu.

The code selects (to display), the title of the page and the order in which the page appears on the menu.

For example, if there are 5 pages (About Us, Mission & Vision, Awards, Environmental Policy, Locations), the order will be 1-5.

Screenshot of current form

As you can see from the screenshot, the code generates 5 dropdowns all with the 5 page options. On the other dropdown, you get 1-5.

What I'm trying to do is get the menu to load the names and page order that the menu is already in straight into that form.

So each dropdown will have a page title and the corresponding order in the other dropdown. The user would then be able to change this if needed.

How could I change my code to achieve this?

<?php
$apageid = array();
$apagename = array();
$apageorder = array();

$q = "SELECT g.id, g.title, n.order FROM tbl_general g INNER JOIN tbl_navigation n ON n.pageid = g.id WHERE n.type = '$_SESSION[parent]' ORDER BY n.order";
$return = $database->query($q);
while($row=mysql_fetch_assoc($return)){
    $apageid[] = $row['id'];
    $apagename[] = $row['title'];
    $apageorder[] = $row['order'];
}

$count = count($apageid);
$bpageid = array();
$bpageorder = array();
?>
<div class="form-holder">
<?php

//run through each page one at a time and update the order of the menu
for($i=0; $i<$count; $i++){
    ?>
    <div class="form-row">
        <div class="form-label">
            Page Name
        </div>
        <div class="form-field">
            <select class="select-small" name="<?php echo $bpageid[$i] ?>">
                <?php
                for($j=0; $j<$count; $j++){
                    ?>
                    <option value="<?php echo $apageid[$j]; ?>"><?php echo $apagename[$j]; ?></option>
                    <?php
                }
                ?>
            </select>
            <select class="select-small" name="<?php echo $bpageorder[$i] ?>">
                <?php
                for($k=0; $k<$count; $k++){
                    ?>
                    <option value="<?php echo $apageorder[$k]; ?>"><?php echo $apageorder[$k]; ?></option>
                    <?php
                }
                ?>
            </select>
        </div>
    </div>
    <?php
}
?>
  • 写回答

1条回答 默认 最新

  • donglei2022 2012-04-13 16:29
    关注

    Alright I know what you need.

    change this:

    for($i=0; $i<$count; $i++){
    

    to this:

    mysql_data_seek( $return ); //reset the pointer to do the same query
    while($row=mysql_fetch_assoc($return)){
    
    $id = $row['id'];
    $title = $row['title'];
    $order = $row['order'];
    

    this will loop each id field straight from the db instead of from your arrays. Also we have variables now for each field during the loop. Now change the array var for id to the id var we added earlier:

    <select class="select-small" name="<?php echo $id; ?>">
    

    Now for the page name select fields lets add this to the second for loop:

    for($j=0; $j<$count; $j++){
          if($apageid[$j] == $id) {
          $selected = true;
          } else {
          $selected = false;
          }
    

    Now add a variable to make the current option value in the array 'selected' if it is equal to the value of the current row

    <option value="<?php echo $apageid[$j]; ?>" <? echo ($selected == true) ? 'selected="selected"' : ''; ?>><?php echo $apagename[$j]; ?></option>
    

    then do the same for the select on the bottom:

    <select class="select-small" name="<?php echo $order; ?>">
                <?php
                for($k=0; $k<$count; $k++){
                    if($apageorder[$k] == $order) {
                        $selected = true;
                    } else {
                        $selected = false;
                    }
                    ?>
                    <option value="<?php echo $apageorder[$k]; ?>" <? echo ($selected == true) ? 'selected="selected"' : ''; ?>><?php echo $apageorder[$k]; ?></option>
                    <?php
                }
                ?>
            </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line