Intereting Posts

создание динамической строки mysql?

Я пытаюсь создать простую страницу поиска, но я не уверен на 100%, как написать фактическую строку поиска (используя соответствующие И и т. Д., Если переменная существует) вот код:

if ($post) { //get all search variables $type = JRequest::getVar('type'); $classifications = JRequest::getVar('classifications', array(0), 'post', 'array'); $rating = JRequest::getVar('rating'); $status = JRequest::getVar('status'); $cterms = JRequest::getVar('cterms'); $clientid = JRequest::getVar('clientid'); $company = JRequest::getVar('company'); $address = JRequest::getVar('address'); $name = JRequest::getVar('name'); $surname = JRequest::getVar('surname'); $city = JRequest::getVar('city'); $state = JRequest::getVar('state'); $pcode = JRequest::getVar('pcode'); $country = JRequest::getVar('country'); //create search string echo "SELECT * FROM #__db_clients "; <- the query is supposed to be done here.. it's in as echo because I was trying to spit it out before trying to make it run.. :) } else { echo 'There has been an error, please try again.'; }; 

Я попытался использовать (если type! = Null then searchtype = "где type = 'X'"), но тогда я не мог понять, как разместить AND до / после, если это необходимо для поиска .. если это делает смысл?

Это быстрый пример. Я не знаю, какие данные JRequest :: getVar возвращаются (всегда строка или смешанные типы?), Но это должно вас отключить. Обязательно используйте тот способ, который применяется в цикле foreach:

 if ($post) { $criteria = array(); //get all search variables $criteria['type'] = JRequest::getVar('type'); $criteria['classifications'] = JRequest::getVar('classifications', array(0), 'post', 'array'); $criteria['rating'] = JRequest::getVar('rating'); //if there are some criteria, make an array of fieldName=>Value maps if(!empty($criteria)) { $where = array(); foreach($criteria as $k => $v) { //IMPORTANT!! //$v is the value of the field, needs to be quoted correctly!! $where[] = "$k = '$v'"; } } //create search string $query = "SELECT * FROM #__db_clients"; if($where) { $query .= " where " . join(' AND ', $where); } } else { echo 'There has been an error, please try again.'; };