我的问题是,由于我使用cat_id将SWITCH函数添加到用于查询的代码中,因此代码无法再将查询条件传递给URL以进行分页。 cat_id是一个categoryID,它在子表中保存为外键。
如果你能提供帮助,这是我的代码:
$criteria = array('ctitle', 'csubject', 'creference', 'cat_id', 'cmaterial', 'ctechnic', 'cartist', 'csource', 'cposture', 'stolen');
$likes = "";
$url_criteria = '';
foreach ( $criteria AS $criterion ) {
if ( ! empty($_POST[$criterion]) ) {
$value = ($_POST[$criterion]);
$likes .= " AND `$criterion` LIKE '%$value%'";
switch ($criterion) {
case 'cat_id':
$likes .= " AND `$criterion`='$value'";
break;
default:
$likes .= " AND `$criterion` LIKE '%$value%'";
break;
$url_criteria .= '&'.$criterion.'='.htmlentities($_POST[$criterion]);
}
} elseif ( ! empty($_GET[$criterion]) ) {
$value = mysql_real_escape_string($_GET[$criterion]);
$likes .= " AND `$criterion` LIKE '%$value%'";
switch ($criterion) {
case 'cat_id':
$likes .= " AND `$criterion`='$value'";
break;
default:
$likes .= " AND `$criterion` LIKE '%$value%'";
break;
$url_criteria .= '&'.$criterion.'='.htmlentities($_GET[$criterion]);
} //var_dump($likes);
}
}
$sql = "SELECT * FROM collections WHERE c_id>0" . $likes . " ORDER BY c_id ASC";
答案 0 :(得分:2)
您不需要将$url_criteria
放入交换机。它没有被处理,也永远不会被PHP执行
<?php
switch ($criterion) {
case 'cat_id':
$likes .= " AND `$criterion`='$value'";
break;
default:
$likes .= " AND `$criterion` LIKE '%$value%'";
break;
//removed from here
}
// put it here
$url_criteria .= '&'.$criterion.'='.htmlentities($_POST[$criterion]);
答案 1 :(得分:2)
$url_criteria .= '&'.$criterion.'='.htmlentities($_POST[$criterion]);
这是在
之后default:
...
break;
所以这永远不会被执行。