• PHP Help
  • undefined index and Invalid argument supplied for foreach()

I'm working with these two files as title says. I'm not sure were I'm going wrong I've tried everything off top of my head

<?php

class UserLocations
{
	
public function __construct()
{
    $db = new Database;
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
    $this->db = $db;
}

public function search($lat, $lon, $type)
{
	$db = new Database;
	$_GET['lat'] = isset($_GET['lat']) ? $_GET['lat'] : null;
	$_GET['longitude'] = isset($_GET['longitude']) ? $_GET['longitude'] : null;
	$lat = $_SESSION['lat'];
	$lon = $_SESSION['longitude'];
	if(isset($_POST["page"])){
	$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
	if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
	}else{
	$page_number = 1;
	}
$item_per_page =4;
//get current starting point of records
$position = (($page_number-1) * $item_per_page);
		$query = "SELECT shopName, streetNumber, phone, postcode,town,streetName, lat, lon, distance, business, id
		FROM (
		SELECT z.postcode,
        z.shopName,
        z.town,
        z.phone,
        z.business,
        z.id,
        z.streetNumber,
		z.streetName,
        z.lat, z.lon,
        p.radius,
        p.distance_unit
                 * DEGREES(ACOS(COS(RADIANS(p.latpoint))
                 * COS(RADIANS(z.lat))
                 * COS(RADIANS(p.longpoint - z.lon))
                 + SIN(RADIANS(p.latpoint))
                 * SIN(RADIANS(z.lat)))) AS distance
		FROM shops AS z
		JOIN (  
SELECT :lat AS latpoint, :lon AS longpoint, 30.0 AS radius, 111.045 AS distance_unit ) AS p ON 1=1 WHERE z.lat BETWEEN p.latpoint - (p.radius / p.distance_unit) AND p.latpoint + (p.radius / p.distance_unit) AND z.lon BETWEEN p.longpoint - (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint)))) AND p.longpoint + (p.radius / (p.distance_unit * COS(RADIANS(p.latpoint)))) ) AS d WHERE distance <= radius AND business = :type ORDER BY distance LIMIT $position, $item_per_page "; $stmt = $db->prepare($query); $stmt->bindValue(':lat', $lat); $stmt->bindValue(':lon', $lon); $stmt->bindValue(':type', $type); $stmt->execute(); $results = $stmt->fetchAll(); $count = $stmt->rowCount(); if($count >= 1){ return $results; } else{ echo "No results found"; } } }
<?php 
session_start();
require ("classes/Database.php");
require ("classes/UserLocations.php");
		
	
if ($_SERVER['REQUEST_METHOD'] == 'GET')
	{	

	$_GET['type'] = isset($_GET['type']) ? $_GET['type'] : null;
	$_GET['latitude'] = isset($_GET['latitude']) ? $_GET['latitude'] : null;
	$_GET['longitude'] = isset($_GET['longitude']) ? $_GET['longitude'] : null;
	$_GET['accuracy'] = isset($_GET['accuracy']) ? $_GET['accuracy'] : null;
	$page_number = $_GET["page_number"] ?? 0;
	$get = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);	
	$lat = $lon = $accuracy = $type = $item_per_page = "";
	$userId = empty($_SESSION['userId']) ? 0 : $_SESSION['userId'];
	$lat = $get['latitude'];
	$lon = $get['longitude'];
	$type = $_GET['type'];
	
	
	$usersL = new UserLocations();
	$results = $usersL->search($lat, $lon, $type);

foreach($results as $r){
		
?>
			<div class="shop-results">
				<div class="shop-details-output">
				<a href="view-shop.php?id=<?php echo $r['id']; ?>"><h4><?php echo $r['shopName']."<br />\n"; ?></a>	
				</div>
			</div>
			<?php
		
	}
}
?>

    Assuming it's happening here, first thing I'd do is confirm that the search() method actually returned something that is iterable (e.g. not a Boolean false).

    $results = $usersL->search($lat, $lon, $type);
    
    foreach($results as $r){
    

    NogDog When I var_dump it gives me what I'd expect to see, but for whatever reason it keeps saying it's invalid in the loop?

    var_dump($results);

      Just a guess, try adding the fetch type to fetchAll():

      $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

      (Doesn't seem to me like that should be a fix for that error, but I don't think you want the default FETCH_BOTH behavior.

        PS: Probably should put in some sort of check that the call to that function returns an array (versus a string or Boolean), before trying to use it.

          It was a lazy loader I was trying to get working, think I'll look for an alternative

            Write a Reply...