好的,所以问题是我正在网站 702web.com/tutors 上进行自定义地理位置
使用内页进行搜索,因为主页搜索未完成。
我将其设置为用户不输入邮政编码的位置和主题是任何它将显示所有导师,也将其设置为没有输入邮政编码并且他们选择一个主题然后将显示所有教授该主题的导师,我也将它放到输入邮政编码的位置,并且用户选择任何然后显示该邮政编码中的任何导师,第四个也是最后一个是如果用户输入邮政编码和主题,那么所有教授该主题和该邮政编码的导师都会显示。
这样做很容易,但是为了使它第三次和第四次稍微复杂一些,我让它选择用户输入的邮政编码半径 40 英里内的所有邮政编码(使用包含所有邮政编码和纬度和经度的表格)。正在查找该半径内的所有邮政编码。
我的问题是 如何使用数组查询该数据库?由于该半径内的所有邮政编码都放在一个数组中,我怎么知道使用 SELECT 语句来“SELECT * FROM wp_testimonials WHERE postcode = '”.$THIS IS MY ARRAY??"' AND find_in_set('".$_REQUEST ['主题']."', 主题)"
结果页面代码
<?php
if(isset($_REQUEST['send']) OR !empty($_REQUEST['zipcode'])) {
if(!preg_match('/^[0-9]{5}$/', $_REQUEST['zipcode'])) {
include('search_page.php');
}
else {
//connect to db server; select database
$link = mysql_connect('t0tors102938.db.7131821.hostedresource.com', 't0tors102938', 'aL8#Jfo89!') or die('Cannot connect to database server');
mysql_select_db('t0tors102938') or die('Cannot select database');
//query for coordinates of provided ZIP Code
if(!$rs = mysql_query("SELECT * FROM wp_us_zipcodes WHERE zip_code = '$_REQUEST[zipcode]'")) {
echo "<p><strong>There was a database error attempting to retrieve your ZIP Code.</strong> Please try again.</p>\n";
}
else {
if(mysql_num_rows($rs) == 0) {
echo "<p><strong>No database match for provided ZIP Code.</strong> Please enter a new ZIP Code.</p>\n";
}
else {
//if found, set variables
$row = mysql_fetch_array($rs);
$lat1 = $row['latitude'];
$lon1 = $row['longitude'];
$d = 40;
$r = 3959;
//compute max and min latitudes / longitudes for search square
$latN = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(0))));
$latS = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(180))));
$lonE = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$lonW = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
//find all coordinates within the search square's area
//exclude the starting point and any empty city values
$query = "SELECT * FROM wp_us_zipcodes WHERE (latitude <= $latN AND latitude >= $latS AND longitude <= $lonE AND longitude >= $lonW) AND city != '' ORDER BY state, city, latitude, longitude";
if(!$rs = mysql_query($query)) {
echo "<p><strong>There was an error selecting nearby ZIP Codes from the database.</strong></p>\n";
}
elseif(mysql_num_rows($rs) == 0) {
echo "<p><strong>No nearby ZIP Codes located within the distance specified.</strong> Please try a different distance.</p>\n";
}
else {
//output all matches to screen
while($row = mysql_fetch_array($rs)) {
foreach($row['zip_code'] as $rows) {
$queryzipcodes[] = $rows;
}
}
$sqlzip = implode(',',$queryzipcodes);
echo "$queryzipcodes";
//echo 'ok';
if($_REQUEST['subject']!='' && $_REQUEST['zipcode']!='')
{
$sql="SELECT * FROM wp_testimonials WHERE postcode IN ($sqlzip) AND find_in_set( '".$_REQUEST['subject']."', subject )";
}elseif($_REQUEST['subject']!='') {
$sql="SELECT * FROM wp_testimonials WHERE find_in_set( '".$_REQUEST['subject']."', subject )";
}
elseif($_REQUEST['zipcode']!='') {
$sql="SELECT * FROM wp_testimonials WHERE postcode = '".$_REQUEST['zipcode']."'";
}
else
{
$sql="SELECT * FROM wp_testimonials";
}
//echo $sql;
$tutors = $wpdb->get_results($sql);
$count = 0;
print('<br>');
foreach ($tutors as $tutor)
{
$odd = $count%2;
if ($odd == 1)
{
$bgcolor='#fafafa';
}elseif ($odd == 0)
{
$bgcolor='#f0f0f0';
}
$sql3="SELECT sfimgurl FROM wp_testimonials WHERE testid='".$tutor->testid."'";
$res3=mysql_query($sql3);
$data3=mysql_fetch_assoc($res3);
$blogurl = get_bloginfo('wpurl');
$imgsrc = '/wp-content/uploads/';
$tutorimg = $data3['sfimgurl'];
print('<table width="100%" style="border-radius: 10px;" bgcolor="'.$bgcolor.'">');
print('<tr height="145px"><td width="110px" style="padding: 10px 5px 10px 10px; vertical-align: top; "><div style="height:145px; overflow:hidden; float: left; padding-right: 10px; border: dotted; border-width: 0 1px 0 0"><img src="'.$blogurl.'' .$imgsrc.'' .$tutorimg.'" width="100" height="145" />');
print('</div></td>');
print('<td valign="top" width="150px" style="float:left; padding-left: 5px; padding-top:5px;"><div style="float: left; font-size:12px;"><strong>TUTOR:<br><a href="?page_id=175&tid=' .$tutor->testid. '">'.$tutor->clientname.'</a><br></strong><br>');
$subj=$tutor->subject;
$arrr_subject=explode(',', $subj);
for($i=0;$i<3;$i++) {
//$i<count($arrr_subject)
$sql2="select * from wp_tutorcat WHERE subid='".$arrr_subject[$i]."'";
$res2=mysql_query($sql2);
$data2=mysql_fetch_assoc($res2);
$subject2 = (strlen($data2['subject']) > 20) ? substr($data2['subject'],0,17).'...' : $data2['subject'];
echo $subject2.'<br>';
}
//print('<br> '.$tutor->text_full.'<br/><br/>');
print('<br><strong>Zip Code: '.$tutor->postcode.'</strong></div></td>');
print('<td width="350px" style="padding-left:10px; padding-top: 5px; vertical-align: top; border: dotted; border-width: 0 0 0 1px;"><div style="float: left; font-size: 12px; position: relative; padding-right: 5px; ">');
$idnum=$tutor->testid;
//$bio=$data['text_full'];
$sql3="SELECT text_full FROM wp_testimonials WHERE testid='".$idnum."'";
$res3=mysql_query($sql3);
$data3=mysql_fetch_assoc($res3);
$ses = "'s";
$bio = (strlen($data3['text_full']) > 103) ? substr($data3['text_full'],0,100).'...' : $data3['text_full'];
echo '<strong>BIO:</strong>
<br>'.$bio.'<br></div>';
print('<div align="center"><p style="text-align: center;"><span class="bk-button-wrapper"><a href="?page_id=175&tid=' .$tutor->testid. '" target="_self" class="bk-button red center rounded small" style="margin-top: 15px;">View '.$tutor->clientname.''. $ses . ' Profile</a></span></p></div>');
print('</td></tr></table>');
$count++;
}
}
}
}
}
}
?>