Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL select any row that data in column zipcode matches data within an array?
    primarykey
    data
    text
    <p>OK so question is I have am doing a custom geolocation on the site 702web.com/tutors</p> <p>Use a inner page for searching as the home page search is not done.</p> <p>I have it setup to where is user doesnt enter a zipcode and subject is any it will display all tutors, also have it to where is no zipcode is entered and they select a subject then all tutors that teach that subject will be displayed, I also have it to where is a zipcode is enter and user selects any then any tutor in that zipcode gets displayed, the forth and final is if user enters zipcode and subject then all tutors that teach that subject and in that zip code get displayed. </p> <p>Doing it that way is easy, but to make it slightly more complicated for the third and forth I have it selecting all zipcodes that are in a 40 mile radius of the user entered zip code (using a table with all zip codes and latitude and longitudes). The finding all the zip codes within that radius is working.</p> <p><strong>My question is this</strong> How to a query that database using an array? since all the zip codes in that radius are put in an array how can I tell use the SELECT statement to "SELECT * FROM wp_testimonials WHERE postcode = '".$THIS IS MY ARRAY??"' AND find_in_set( '".$_REQUEST['subject']."', subject )" </p> <p><strong>CODE OF THE RESULTS PAGE</strong></p> <pre><code>&lt;?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 "&lt;p&gt;&lt;strong&gt;There was a database error attempting to retrieve your ZIP Code.&lt;/strong&gt; Please try again.&lt;/p&gt;\n"; } else { if(mysql_num_rows($rs) == 0) { echo "&lt;p&gt;&lt;strong&gt;No database match for provided ZIP Code.&lt;/strong&gt; Please enter a new ZIP Code.&lt;/p&gt;\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 &lt;= $latN AND latitude &gt;= $latS AND longitude &lt;= $lonE AND longitude &gt;= $lonW) AND city != '' ORDER BY state, city, latitude, longitude"; if(!$rs = mysql_query($query)) { echo "&lt;p&gt;&lt;strong&gt;There was an error selecting nearby ZIP Codes from the database.&lt;/strong&gt;&lt;/p&gt;\n"; } elseif(mysql_num_rows($rs) == 0) { echo "&lt;p&gt;&lt;strong&gt;No nearby ZIP Codes located within the distance specified.&lt;/strong&gt; Please try a different distance.&lt;/p&gt;\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']!='' &amp;&amp; $_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-&gt;get_results($sql); $count = 0; print('&lt;br&gt;'); 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-&gt;testid."'"; $res3=mysql_query($sql3); $data3=mysql_fetch_assoc($res3); $blogurl = get_bloginfo('wpurl'); $imgsrc = '/wp-content/uploads/'; $tutorimg = $data3['sfimgurl']; print('&lt;table width="100%" style="border-radius: 10px;" bgcolor="'.$bgcolor.'"&gt;'); print('&lt;tr height="145px"&gt;&lt;td width="110px" style="padding: 10px 5px 10px 10px; vertical-align: top; "&gt;&lt;div style="height:145px; overflow:hidden; float: left; padding-right: 10px; border: dotted; border-width: 0 1px 0 0"&gt;&lt;img src="'.$blogurl.'' .$imgsrc.'' .$tutorimg.'" width="100" height="145" /&gt;'); print('&lt;/div&gt;&lt;/td&gt;'); print('&lt;td valign="top" width="150px" style="float:left; padding-left: 5px; padding-top:5px;"&gt;&lt;div style="float: left; font-size:12px;"&gt;&lt;strong&gt;TUTOR:&lt;br&gt;&lt;a href="?page_id=175&amp;tid=' .$tutor-&gt;testid. '"&gt;'.$tutor-&gt;clientname.'&lt;/a&gt;&lt;br&gt;&lt;/strong&gt;&lt;br&gt;'); $subj=$tutor-&gt;subject; $arrr_subject=explode(',', $subj); for($i=0;$i&lt;3;$i++) { //$i&lt;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']) &gt; 20) ? substr($data2['subject'],0,17).'...' : $data2['subject']; echo $subject2.'&lt;br&gt;'; } //print('&lt;br&gt; '.$tutor-&gt;text_full.'&lt;br/&gt;&lt;br/&gt;'); print('&lt;br&gt;&lt;strong&gt;Zip Code: '.$tutor-&gt;postcode.'&lt;/strong&gt;&lt;/div&gt;&lt;/td&gt;'); print('&lt;td width="350px" style="padding-left:10px; padding-top: 5px; vertical-align: top; border: dotted; border-width: 0 0 0 1px;"&gt;&lt;div style="float: left; font-size: 12px; position: relative; padding-right: 5px; "&gt;'); $idnum=$tutor-&gt;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']) &gt; 103) ? substr($data3['text_full'],0,100).'...' : $data3['text_full']; echo '&lt;strong&gt;BIO:&lt;/strong&gt; &lt;br&gt;'.$bio.'&lt;br&gt;&lt;/div&gt;'; print('&lt;div align="center"&gt;&lt;p style="text-align: center;"&gt;&lt;span class="bk-button-wrapper"&gt;&lt;a href="?page_id=175&amp;tid=' .$tutor-&gt;testid. '" target="_self" class="bk-button red center rounded small" style="margin-top: 15px;"&gt;View '.$tutor-&gt;clientname.''. $ses . ' Profile&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;'); print('&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;'); $count++; } } } } } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload