我希望将结果显示在表格列的下拉列表中,仅显示所选位置和尚未分配的其他可用位置。
表从中选择结果:
TITLE | LINK | POSITION | ------------------------------------- Home | home.php | 1 | ------------------------------------- Contact | contact.php | 2 | ------------------------------------- about | about.php | 3 | -------------------------------------
所有职位
(1,2,3,4,5,6)
带下拉框的页面
/* extract data from table*/
if(isset($_GET['edit_id']))
{
$id = $_GET['edit_id'];
extract($update->getID('pages','TITLE',$id));
}
/* dropdown with results*/
<label class="control-label" for="basicinput">POSITION</label>
<select name="POSITION" class="span8" id="POSITION" tabindex="1">
<?php
$view->menu_drop($POSITION)
?>
</select>
功能过滤下拉框
<?php
public function menu_drop($value)
{
$stmt = $this->db->prepare("SELECT POSITION FROM pages");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
$catg=$row["POSITION "];
if( $value == $catg )
{
echo "<option selected = 'selected' value=\"$catg\">$catg</option>";
} else
{
/* i am lost here. I would like to display the other available positions excluding the positions already assigned pages in the table*/
}
}
}
?>
答案 0 :(得分:0)
简单的机制是使用三元测试,以便每次循环while循环时将变量设置为selected
或nothing
同样,类方法应该被封装并且不会影响它们自身。使方法构建一个包含html的字符串,而不是从方法中回显它,然后,理论上,如果你愿意,你可以修改它的结果。
<?php
public function menu_drop($value)
{
$htm = '';
$stmt = $this->db->prepare("SELECT POSITION FROM pages");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
$sel = $value == $row['POSITION'] ? "selected='selected'" : '';
$htm .= "<option $sel value='{$row['POSITION']}'>{$row['POSITION']}</option>";
}
return $htm;
}
?>
然后将您的通话流程更改为
/* dropdown with results*/
<label class="control-label" for="basicinput">POSITION</label>
<select name="POSITION" class="span8" id="POSITION" tabindex="1">
<?php
echo $view->menu_drop($POSITION);
?>
</select>
答案 1 :(得分:0)
试试这个
public function menu_drop ($value) {
echo "<option value=\"0\">Remove Position</option>";
for ($i= 1; $i <=6; $i++) {
$found = false;
$stmt = $this->db->prepare("SELECT POSITION FROM pages");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
$catg=$row["POSITION"];
if ($catg == $i && $i != $value) {
$found = true;
}
}
if (!$found) {
if ($value == $i) {
echo "<option selected = 'selected' value=\"$i\">$i</option>";
} else {
echo "<option value=\"$i\">$i</option>";
}
}
}
}