我无法弄清楚如何正确选择代码,以便能够选择两个以上的城市来搜索和显示两个城市的结果。它一次只执行一次操作,或者一次执行数据库中的所有操作,但不执行选定的编号。
我将首先在下面添加一些Select辅助代码,然后在其中添加一些处理页面代码。第三页代码也是我认为的输出代码。如果有人需要我可以稍后发布。我不确定解决方案是否需要解决该页面,但在那儿我可能是错的。
<form id="main-search-form" method="GET" action="<?= $baseurl; ?>/_searchresults6.php" role="search">
<select id="query-input" name="city_id" style="width:28.8%" style="height:69px" required>
<option value="">Select Area</option>
<option value="3;1">Ches & VB</option>
<option value="3">Chesapeake , VA</option>
<option value="9">Hampton , VA</option>
<option value="10">Newport News , VA</option>
<option value="2">Norfolk , VA</option>
<option value="12">Poquoson , VA</option>
<option value="4">Portsmouth , VA</option>
<option value="5">Suffolk , VA</option>
<option value="1">Virginia Beach , VA</option>
<option value="11">Williamsburg , VA</option>
<option value="0">All Active US Cities</option>
</select>
</form>
//This next code is from the processing page//
<?php
require_once(__DIR__ . '/inc/config.php');
?>
<?php
$total_rows = 0;
$response = array();
$query_city_id = (!empty($_GET['city_id'])) ? $_GET['city_id'] : 0;
$query_query = (!empty($_GET['query'])) ? $_GET['query'] : '';
$page = (!empty($_GET['page'])) ? $_GET['page'] : 1;
// check vars
if(!is_numeric($query_city_id)) {
}
$query_city_id = (int)$query_city_id;
// city details
$query_city_name = '';
$query_state_abbr = '';
if (isset($_GET['city_id'])) {
if ($_GET['city_id'] != 0) {
$ids = explode(';', $_GET['city_id']) ; // split multiple ids
$k = count($ids);
$placeholders = array_fill(0, $k, '?');
$placestr = join(',', $placeholders);
$stmt = $conn->prepare("SELECT city_name, state FROM cities WHERE city_id IN ($placestr)");
$stmt->execute($ids);
}
else {
// if you want all cities then a where clause is not needed
$stmt = $conn->query("SELECT city_name, state FROM cities");
}
// process the stmt results here
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$query_city_name = $row['city_name'];
$query_state_abbr = $row['state'];
echo "$query_city_name, $query_state_abbr<br>";
}
}
// paging vars
$limit = $items_per_page;
if($page > 1) {
$offset = ($page-1) * $limit + 1;
}
else {
$offset = 1;
}
// get page
if($page == 1) {
$pag = '';
}
else {
$pag = "- $txt_page $page";
}
// count total rows
if(!empty($query_city_id) && !empty($query_query)) {
$query = "SELECT COUNT(*) AS total_rows
FROM places
WHERE city_id = :city_id AND status != 'trashed' AND paid = 1
AND MATCH(place_name, description) AGAINST(:query) ";
$stmt = $conn->prepare($query);
$stmt->bindValue(':city_id', $query_city_id);
$stmt->bindValue(':query', $query_query);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_rows = $row['total_rows'];
}
else if (empty($query_city_id) && !empty($query_query)) {
$query = "SELECT COUNT(*) AS total_rows
FROM places
WHERE status != 'trashed' AND paid = 1
AND MATCH(place_name, description) AGAINST(:query)";
$stmt = $conn->prepare($query);
$stmt->bindValue(':query', $query_query);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_rows = $row['total_rows'];
}
else if (!empty($query_city_id) && empty($query_query)) {
$query = "SELECT COUNT(*) AS total_rows
FROM places
WHERE status != 'trashed' AND paid = 1 AND city_id = :city_id";
$stmt = $conn->prepare($query);
$stmt->bindValue(':city_id', $query_city_id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_rows = $row['total_rows'];
}
else {
$total_rows = 0;
}
$pager = new DirectoryApp\PageIterator($limit, $total_rows, $page);
$start = $pager->getStartRow();
// initialize empty city and query check
$empty_city_and_query = false;
/*--------------------------------------------------
LIST ITEMS LOGIC
--------------------------------------------------*/
if(!empty($query_city_id) && !empty($query_query)) {
$query = "SELECT p.place_id, p.place_name, p.address, p.cross_street,
p.postal_code, p.phone, p.area_code, p.lat, p.lng, p.state_id, p.description,
c.city_name, c.slug, c.state, ph.filename, ph.dir,
rev_table.avg_rating
FROM places p
LEFT JOIN cities c ON p.city_id = c.city_id
LEFT JOIN photos ph ON p.place_id = ph.place_id
LEFT JOIN (
SELECT *,
AVG(rev.rating) AS avg_rating
FROM reviews rev
) rev_table ON p.place_id = rev_table.place_id
WHERE p.city_id = :city_id AND p.status != 'trashed' AND paid = 1
AND MATCH(place_name, description) AGAINST(:query)
LIMIT :start, :limit";
$stmt = $conn->prepare($query);
$stmt->bindValue(':city_id', $query_city_id);
$stmt->bindValue(':query', $query_query);
$stmt->bindValue(':start', $start);
$stmt->bindValue(':limit', $limit);
}
else if(empty($query_city_id) && !empty($query_query)) {
$query = "SELECT p.place_id, p.place_name, p.address, p.cross_street,
p.postal_code, p.phone, p.area_code, p.lat, p.lng, p.state_id, p.description,
c.city_name, c.slug, c.state, ph.filename, ph.dir,
rev_table.avg_rating
FROM places p
LEFT JOIN cities c ON p.city_id = c.city_id
LEFT JOIN photos ph ON p.place_id = ph.place_id
LEFT JOIN (
SELECT *,
AVG(rev.rating) AS avg_rating
FROM reviews rev
) rev_table ON p.place_id = rev_table.place_id
WHERE p.status != 'trashed' AND paid = 1
AND MATCH(place_name, description) AGAINST(:query)
LIMIT :start, :limit";
$stmt = $conn->prepare($query);
$stmt->bindValue(':query', $query_query);
$stmt->bindValue(':start', $start);
$stmt->bindValue(':limit', $limit);
}
else if(!empty($query_city_id) && empty($query_query)) {
$query = "SELECT p.place_id, p.place_name, p.address, p.cross_street,
p.postal_code, p.phone, p.area_code, p.lat, p.lng, p.state_id, p.description,
c.city_name, c.slug, c.state, ph.filename, ph.dir,
rev_table.avg_rating
FROM places p
LEFT JOIN cities c ON p.city_id = c.city_id
LEFT JOIN photos ph ON p.place_id = ph.place_id
LEFT JOIN (
SELECT *,
AVG(rev.rating) AS avg_rating
FROM reviews rev
) rev_table ON p.place_id = rev_table.place_id
WHERE p.city_id = :city_id AND p.status != 'trashed' AND paid = 1
LIMIT :start, :limit";
$stmt = $conn->prepare($query);
$stmt->bindValue(':city_id', $query_city_id);
$stmt->bindValue(':start', $start);
$stmt->bindValue(':limit', $limit);
}
else{ // both $query_loc and $query_query empty
$empty_city_and_query = true;
}
// now execute query
$stmt->execute();
我已经在其他论坛上提出了解决方案,但没人能解决这个问题。可能是我遗漏了一些东西,但这是最新的代码更改...急需帮助。