这个页面基本上显示了用户可以选择的DB选项列表的表,一旦选择了用户,就可以删除所选的选项,如果他们犯了错误。我已经删除了所有不必要的东西,使它更容易遵循。我已经尝试了每一种组合来获得一个会话数组元素来取消设置,并且几乎放弃了。我知道张贴的值工作正常,因为我已经让他们在提交表单后返回屏幕。array_search函数似乎不喜欢变量名,如果我用物理值替换$ss_id,我可以让它取消设置ok。
如有任何建议,不胜感激。
<?php require_once('Connections/fs1.php');
if (!isset($_SESSION)) {
session_start();
}
if (!isset($_SESSION['con29']))
{
$_SESSION['con29'] = array();
}
//db call to get con29 options
$query1 = mysql_query("SELECT * FROM sub_search_content") or die(mysql_error());
$_SESSION['main_search_selected'] = 's7';
$main_search_selected = $_SESSION['main_search_selected'];
//setup the session variables depending on what was posted------------------
if (isset($_POST['sub_search_content_id']))//add an option
{
$row_id_1 = $_POST['sub_search_content_id'];
array_push($_SESSION['con29'],$row_id_1);
}
if (isset($_POST['sub_search_content_id_remove']))
{
$ss_id = $_POST['sub_search_content_id_remove'];
$key=array_search($ss_id,$_SESSION['con29']);
if($key!==false) unset($_SESSION['con29'][$key]);
//echo $ss_id;
//echo $key;
}
//print_r ($_SESSION['con29']);
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<?php
while ($row = mysql_fetch_assoc($query1))
{
$sub_search_id = $_SESSION['con29'];
if($row[$main_search_selected] == 1)
{
$bgcolour = '#ffffff';
$select_row ='';
$select_row_2='';
}
elseif($row[$main_search_selected] == 2)
{
$id = $row['sub_search_content_id'];
$key = array_search($id, $sub_search_id);
if($key != '')
{
$bgcolour = '#AFC137';
$select_row = '<input name="submit" class="push_buttons" type="submit" value="Remove" />';
$select_row_2 = '<input name="sub_search_content_id_remove" type="hidden" id="sub_search_content_id_remove" value="' . $row['sub_search_content_id'] .' " />';
}
else
{
$bgcolour = '#ffffff';
$select_row = '<input name="submit" class="push_buttons" type="submit" value="select" />';
$select_row_2 = '<input name="sub_search_content_id" type="hidden" id="sub_search_content_id" value="' . $row['sub_search_content_id'].'" />';
}
}
elseif($row[$main_search_selected] == 3)
{
$bgcolour = '#FF9900';
$select_row = "Included";
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if($row[$main_search_selected] != 1)
{ ?>
<tr bgcolor="<?php echo $bgcolour;?>" >
<form action="test1.php" method="POST" name="form1" id="form1" >
<?php echo $select_row_2; ?>
<td width="60" height="25" align="center" valign="middle" class="top-edge-border-menu"><?php echo $row['sub_search_content_id']; ?></td>
<td align="center" valign="middle" class="top-and-right-edge-border-left"><?php echo $row['sub_title']; ?></td>
<td height="25" align="center" valign="middle" class="top-and-right-edge-border">price</td>
<td height="25" align="center" valign="middle" class="top-edge-border"><?php echo $select_row; ?></td>
</form>
</tr>
<?php }
} ?>
</table></td>
</tr>
</tbody>
</table>
<?php mysql_free_result($query1); ?>
value="' . $row['sub_search_content_id'] .' "
注意到后面的空格了吗?当你将ID发送回脚本以从会话中删除它时,你会在ID后面添加一个空格-这可以防止array_search
在会话中找到它。
把这一行改成:
$select_row_2 = '<input name="sub_search_content_id_remove" type="hidden" id="sub_search_content_id_remove" value="' . $row['sub_search_content_id'] . '" />';
注意-如果你需要详细检查一个变量,var_dump()
远优于echo()
-它会显示你的东西,如空格和变量类型