Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3213358
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:56:08+00:00 2026-05-17T14:56:08+00:00

First off, I created a screen cast, in order to explain what I have

  • 0

First off, I created a screen cast, in order to explain what I have and what I’m attempting to create. Much easier to understand.

Please view the screen cast here: http://www.youtube.com/v/lZf3S3EGHDw?fs=1&hl=en_US&rel=0&hd=1

Tables:

CREATE TABLE `locations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(45) DEFAULT NULL,
  `latitude` decimal(10,6) DEFAULT NULL,
  `longitude` decimal(10,6) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

INSERT INTO `locations` (`id`,`title`,`latitude`,`longitude`)
VALUES
    (1,'Randall Automotive Car Repair',42.729642,-84.515524),
    (2,'Belle Tire',42.662458,-84.538177),
    (3,'Better Buy Muffler & Breaks',42.740845,-84.589541),
    (4,'Kwik Car Wash',42.721221,-84.545926);


CREATE TABLE `listings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `token` varchar(4) DEFAULT NULL,
  `location` varchar(45) DEFAULT NULL,
  `info` varchar(70) DEFAULT NULL,
  `status` varchar(45) DEFAULT NULL,
  `auto_inactive` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


INSERT INTO `listings` (`id`,`token`,`location`,`info`,`status`,`auto_inactive`)
VALUES
    (35,'4uaJ','1','All employees are NSA certified.','active','0'),
    (36,'RdcX','1','Family Owned and Operated','active','0'),
    (37,'WuaZ','1','Also repair Small engines','active','0'),
    (38,'2jxD','2','Open on the weekends.','active','0'),
    (39,'Xsu4','2','Two locations in this town.','active','0'),
    (40,'p9cB','2','Advertise on Tiger\'s Baseball','active','0'),
    (41,'mYa3','2','Started right here in Michigan','active','0'),
    (42,'Q8g5','3','Building built in 1997','active','0'),
    (43,'l734','3','Great ratings from BBB','active','0'),
    (44,'7cLY','4','Open in the Winter','active','0'),
    (45,'gtlU','4','Largest car wash in town','active','0'),
    (46,'fEjK','4','Owned and Operated by John Smith','active','1285614174'),
    (47,'dRcu','4','Opened in 1987','inactive','0');



<?php

include_once('include.php'); // Calls the Mysql Database`
ini_set('date.timezone', 'America/Detroit');


$user_latitude = 42.7160084;
$user_longitude = -84.5615018;


$sql =  mysqli_query($mysqli, "SELECT 
                                        loc.id, 
                                        loc.title, 
                                        ( 3959 * acos( cos( radians('".$user_latitude."') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('".$user_longitude."') ) + sin( radians('".$user_latitude."') ) * sin( radians( latitude ) ) ) ) AS distance

                                FROM 
                                        locations loc 

                                WHERE EXISTS(SELECT NULL FROM listings li
                                        WHERE li.location = loc.id 
                                        AND li.status = 'active' 
                                        AND (li.auto_inactive > '".time()."' OR li.auto_inactive = '0')) 

                                ORDER BY distance");


while($locations = mysqli_fetch_array($sql)) {

        $listings = mysqli_fetch_array(mysqli_query($mysqli, "SELECT listings.token, listings.info FROM listings WHERE (listings.location = '".$locations['id']."') AND listings.status = 'active' AND (listings.auto_inactive > '".time()."' OR listings.auto_inactive = '0') ORDER BY RAND()"));


        echo '<a href="listing.php?id='.$listings['token'].'"><h2>'.$locations['title'].'</h2></a>';

        echo '<h5>Distance: '.sprintf ('%.2f', $locations['distance']).' mi</h5>';
        echo '<p>'.$listings['info'].'</p>';
        echo '<hr/>';
}

?>

Please let me know if you need anything clarified. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:56:08+00:00Added an answer on May 17, 2026 at 2:56 pm

    This one works:

    SELECT t1.title, t2.token, t2.info
        FROM
            (SELECT loc.id AS id, loc.title AS title,( 3959 * acos( cos( radians('".$user_latitude."') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('".$user_longitude."') ) + sin( radians('".$user_latitude."') ) * sin( radians( latitude ) ) ) ) AS distance
                FROM locations loc
                WHERE EXISTS(SELECT NULL FROM listings li
                        WHERE li.location = loc.id
                            AND li.status = 'active'
                            AND (li.auto_inactive > UNIX_TIMESTAMP() OR li.auto_inactive = '0'))
            ) t1
        JOIN
            (SELECT DISTINCT(listings.location) AS location, listings.token AS token, listings.info AS info
                FROM listings
                WHERE listings.status = 'active'
                    AND (listings.auto_inactive > UNIX_TIMESTAMP() OR listings.auto_inactive = '0')
                ORDER BY RAND()
            ) t2
        ON t1.id=t2.location
            GROUP BY t2.location
            ORDER BY t2.location ASC;
    

    I also would suggest altering the listings table to make the location, status and auto_inactive columns type int – there’s no point in using varchar for them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I am using Windows XP. I have multiple hard drives and it
First off, I understand the reasons why an interface or abstract class (in the
First off if you're unaware, samba or smb == Windows file sharing, \\computer\share etc.
First off, there's a bit of background to this issue available on my blog:
First off: I'm using a rather obscure implementation of javascript embedded as a scripting
First off, I'm working on an app that's written such that some of your
First off, let me start off that I am not a .net developer. The
First off, this question is ripped out from this question. I did it because
First off, I know next to nothing about language theory, and I barely know
First off, I apologize if this doesn't make sense. I'm new to XHTML, CSS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.