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 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突