我有一个重新排列导航项目的系统。
它将新订单存储在数组中,然后我使用 for 循环遍历数组并更新数据库。
<?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
mysql_data_seek( $return, 0 ); //reset the pointer to do the same query
$i = 0; //the count for saving the new configuration
while($row=mysql_fetch_assoc($return)){
$id = $row['id'];
$title = $row['title'];
$order = $row['order'];
?>
<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++){
if($apageid[$j] == $id) {
$selected = true;
} else {
$selected = false;
}
?>
<option value="<?php echo $apageid[$j]; ?>" <? echo ($selected == true) ? 'selected="selected"' : ''; ?>><?php echo $apagename[$j]; ?></option>
<?php
}
?>
</select>
<select class="select-small" name="<?php echo $bpageorder[$i]; ?>">
<?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>
</div>
</div>
<?php
$i++;
}
?>
第一个代码块是可以在其中对项重新排序的菜单。
最初,它会加载当前选择并允许您更改顺序。
function reorderChildren($pageid, $pageorder){
global $database;
$count = count($pageid);
//run through each page one at a time and update the order of the menu
for($i=0; $i<$count; $i++){
//set a few variables
$pid = $pageid[$i];
$porder = $pageorder[$i];
echo "pid = $pid porder = $porder";
$q = "UPDATE tbl_navigation SET order = '$porder' WHERE pageid = '$pid' AND type = '$_SESSION[parent]'";
$database->query($q);
}
return 0;
}
然后信息最终被传递到这里,问题似乎是 for 循环从未执行过。
谁能明白为什么会这样?
这不是使用的命名,我已经检查过了。
我是否正确地将信息存储在数组中?
我能否明确指出,$bpageid
和$bpageorder
是传递信息的阵列。
谢谢!
是一个简单的修复!问题是选择名称只需要 name="bpageid[]" 而不是我上面列出的名称。憨!
您刚刚在第一个 while 循环后在顶部启动了$bpageid = array();
。未分配任何值
之后你使用
<select class="select-small" name="<?php echo $bpageid[$i]; ?>">
但$bpageid[$i]
中没有值,因此此选择框的名称为空。
这可能是问题。请检查这一点并按要求执行。
您可以尝试从选择元素的选项中构造一个 JSON 将其存储到隐藏字段或将 ajax 请求发送到 PHP 脚本进行处理,可以从"权重"值 1,2,3 等进行排序。用 PHP 和循环对其进行排序以显示