Zend_db & Zend_paginator - Not having a fun time - php

I am having a serious problem converting my 'select' statement into something that will work with the zend paginator... could someone have a crack at it, as I am having no luck...
Here is my query:
$query = "SELECT
user_id, name, gender, city, province, country, image_id, one_liner, self_description, reputation
FROM
users
WHERE
(
(69.1 * (latitude - " . $user->latitude . ")) *
(69.1 * (latitude - " . $user->latitude . "))
) + (
(69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3)) *
(69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3))
) < " . pow($radius, 2) . "
ORDER BY
(
(69.1 * (latitude - " . $user->latitude . ")) *
(69.1 * (latitude - " . $user->latitude . "))
) + (
(69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3)) *
(69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3))
Here is what I have so far:
$select = $db->select();
$select->from(
array('users'),
array(
'user_id',
'name',
'gender',
'city',
'province',
'country',
'image_id',
'one_liner',
'self_description',
'reputation'
)
);
$select->where("(69.1 * (latitude - " . $user->latitude . ")) * (69.1 * (latitude - " . $user->latitude . "))) + ((69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3)) * (69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3))) < " . pow($radius, 2));
$select->order("(69.1 * (latitude - " . $user->latitude . ")) * (69.1 * (latitude - " . $user->latitude . "))) + ((69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3)) * (69.1 * (longitude - " . $user->longitude . ") * COS(" . $user->latitude . " / 57.3))) ASC");

Why do you have "<" in your order by clause?

What does this have to do with Zend_Paginator? Ah, do you have the query and you don't know how to make a paginator with it, or is the paginator not working with this query?
The only thing I can see is you're missing an opening parenthesis in both the where() and order() clause:
$select->where("((69.1 * [...] ");
$select->order("((69.1 * [...] ");
^
So maybe Zend_Paginator isn't working because the SQL query has errors?
And of course I have to ask: are those variables you're interpolating safe, or should you really be using $db->quote($user->latitude, Zend_Db::FLOAT_TYPE)?

Assuming you are using MVC-pattern, won't this work?
in your bootstrap:
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
in your controller:
$page = Zend_Paginator::factory($select);
$page->setCurrentPageNumber($this->_getParam('page', 1));
$page->setItemCountPerPage($this->_getParam('par', 20));
$this->view->results= $page;
in your view:
<?php foreach($this->results as $result) : ?>
<!-- print some $result stuff here -->
<?php endforeach;?>
<?= $this->results ?>
then place a pagination.phtml example that you can find on zend manual
-Lo

Related

Mysql + GMAPs - Select from DB and count distance

I have MySQL table with some libraries. I need to make SQL query in PHP, which will select library by ID, and count distance. This is invalid query. Where I'm doing mistake in this query ?
$query = "SELECT *,
( 6371 *
acos(
cos(radians(" . $center_lat . ")) *
cos(radians(lat)) *
cos(radians(lng) - radians(" . $center_lng . ")) +
sin(radians(" . $center_lat . ") ) *
sin(radians( lat ) )
)
) AS distance
FROM `libraries`
WHERE id = ". $id ."
LIMIT 0 , 1";

Subquery calculation error?

Why is this query not working?
$query = ("SELECT * FROM
(SELECT *, (
(((endingLatitude - " . $h . ")(endingLatitude - " . $h . "))
/" . pow($r1, 2) . ")
+
((endingLongitude - " . $k . ")(endingLongitude - " . $k . "))
/" . pow($r2, 2) . "
) AS point2
FROM (SELECT *, ((
(
((startingLatitude - " . $h . ")(startingLatitude - " . $h . "))
/" . pow($r1, 2) . ")
+
((startingLongitude - " . $k . ")(startingLongitude - " . $k . "))
/" . pow($r2, 2) . "))
AS point1
FROM (SELECT * FROM trips WHERE distance >='" . ($distance * .25) . "') as query1)
as query2) as query3 WHERE point1 <= 1 AND point2 <= 1 LIMIT 0 , 10;");
$result = mysqli_query($con, $query);
$h and $k is the ellipses x and y coordinates respectively. I am using a formula found here to calculate whether or not the two points, (startingLat,startingLong) and (endingLat,endingLong) are within an ellipse with vertical height $r1 and horizontal height $r2. I am also limiting the rows that I search to rows that have a distance cell value of greater than $distance * .25.
I think it might have something to do with a parenthesis error or something to do with the way I am sub querying/performing my calculations.
Using
die(mysqli_error($con));
returns an error of You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(endingLatitude - 36.9564753)) /796.842964388) + ((endingLongitud' at line 3
I believe you have to use Mysql multiplication arithmetic operator, *.
https://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_times
Instead of:
(endingLatitude - " . $h . ")(endingLatitude - " . $h . ")
Do this...
(endingLatitude - " . $h . ") * (endingLatitude - " . $h . ")

PHP/Mysql: optimize query

I have the following script that retrieve numbers from 2 tables, make a sum, and the value is updated into a 3th table.
$query = "SELECT (SELECT SUM(net_amount) FROM fin_costos WHERE month='1' AND group_of_costos='general' AND year_analysis='2014' ) +
(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='1' AND group_of_costos='general' AND year_analysis='2014') AS total";
$result = mysqli_query($mysqli,$query);
while($row = mysqli_fetch_array($result)){$valor_final = $row['total']; }
$query_update="UPDATE fusion_analysis SET jan='$valor_final' WHERE year_for_analysis='2014' AND item_for_analysis='general' AND category='general'";
$result = mysqli_query($mysqli,$query_update);
I need to run the same script for each month of the year. Everything is exaclty the same except the variable 'month' that changes from 1 to 12 and the SET in UPDATE where the value is uploaded for each month ('jan','feb', 'mar'...etc)
I'm currently just copying and pasting the same script changing this few parameters but I believe there is a smarter way to do this in less lines of code I have
See date function of PHP:
$query = "SELECT (SELECT SUM(net_amount)"
. " FROM fin_costos"
. " WHERE month='".date('n')."'"
." AND group_of_costos='general' AND year_analysis='".date("Y")."' ) +"
."(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='".date('n')."'"
. " AND group_of_costos='general' AND year_analysis='".date("Y")."') AS total";
$query_update="UPDATE fusion_analysis"
. " SET `". strtolower(date('M'))."`='$valor_final'"
. " WHERE year_for_analysis='".date("Y")."'"
. " AND item_for_analysis='general'"
. " AND category='general'";
NOTE:
Y - A full numeric representation of a year, 4 digits like 2014
n - Numeric representation of a month, without leading zeros 1 - 12
M - A short textual representation of a month, three letters Jan through Dec
For month as short textual I've used the strtolower function to make it lowercase.
UPDATE
Based on OP comment:
for ($i = 1; $i <= 12; $i++) {
$query = "SELECT (SELECT SUM(net_amount)"
. " FROM fin_costos"
. " WHERE month='" . $i . "'"
. " AND group_of_costos='general' AND year_analysis='" . date("Y") . "' ) +"
. "(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='" . $i . "'"
. " AND group_of_costos='general' AND year_analysis='" . date("Y") . "') AS total";
$result = mysqli_query($mysqli, $query);
$row = mysqli_fetch_assoc($result);
$valor_final = $row['total'];
$monthName = strtolower(date('M', strtotime(date("Y") . "-" . str_pad($month,2, "0", STR_PAD_LEFT) . "-" . date("01") )));
$query_update = "UPDATE fusion_analysis"
. " SET `" . $monthName . "`=' " . $valor_final . "'"
. " WHERE year_for_analysis='" . date("Y") . "'"
. " AND item_for_analysis='general'"
. " AND category='general'";
mysqli_query($mysqli, $query_update);
}

haversine formula php / mysql

I'm trying to get a common database of geo points working with a radius search.
I've found several good tutorials on this topic, but I'm failing at the very end.
The main tutorial is here: http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates.
The basic formula, in the form of an SQL query, is
SELECT * FROM Places
WHERE (Lat => 1.2393 AND Lat <= 1.5532) AND (Lon >= -1.8184 AND Lon <= 0.4221)
AND acos(sin(1.3963) * sin(Lat) + cos(1.3963) * cos(Lat) * cos(Lon - (-0.6981)))
<= 0.1570;
I've implemented this in a simple PHP test page like this:
$R = 6371; // radius of Earth in KM
$lat = '46.98025235521883'; // lat of center point
$lon = '-110.390625'; // longitude of center point
$distance = 1000; // radius in KM of the circle drawn
$rad = $distance / $R; // angular radius for query
$query = '';
// rough cut to exclude results that aren't close
$max_lat = $lat + rad2deg($rad/$R);
$min_lat = $lat - rad2deg($rad/$R);
$max_lon = $lon + rad2deg($rad/$R/cos(deg2rad($lat)));
$min_lon = $lon - rad2deg($rad/$R/cos(deg2rad($lat)));
// this part works just fine!
$query .= '(latitude > ' . $min_lat . ' AND latitude < ' . $max_lat . ')';
$query .= ' AND (longitude > ' . $min_lon . ' AND longitude < ' . $max_lon . ')';
// refining query -- this part returns no results
$query .= ' AND acos(sin('.$lat.') * sin(latitude) + cos('.$lat.') * cos(latitude) *
cos(longitude - ('.$lon.'))) <= '.$rad;
Am I missing something here? I think I'm following the methodology exactly, but I cannot get the "fine tuning" query to return any results.
not sure but :
$R = 6371; // radius of Earth in KM
$lat = '46.98025235521883'; // lat of center point
$lon = '-110.390625'; // longitude of center point
$distance = 1000; // radius in KM of the circle drawn
$rad = $distance / $R; // angular radius for query
$query = '';
// rough cut to exclude results that aren't close
$radR = rad2deg($rad/$R);
$max_lat = $lat + radR;
$min_lat = $lat - radR;
$radR = rad2deg($rad/$R/cos(deg2rad($lat)));
$max_lon = $lon + radR;
$min_lon = $lon - radR;
// this part works just fine!
$query .= '(latitude > ' . $min_lat . ' AND latitude < ' . $max_lat . ')';
$query .= ' AND (longitude > ' . $min_lon . ' AND longitude < ' . $max_lon . ')';
// refining query -- this part returns no results
$query .= ' AND acos(sin('.deg2rad($lat).') * sin(radians(latitude)) + cos('.deg2rad($lat).') * cos(radians(latitude)) *
cos(radians(longitude) - ('.deg2rad($lon).'))) <= '.$rad;

Mysql Result Return Problem

I'm creating a project in FuelPHP, with php + mysql.
I have this database query (I'm not using their built in query builder because its just a pain in the ass for complex queries).
$sql = DB::query('
SELECT game_id, lati, longi,
acos(sin (' . $lat . ')
* sin (radians( lati ))
+ cos (' . $lat . ')
* cos (radians(lati))
* cos (radians(longi) - ' . $lon . '))
*' . $R . ' AS D
FROM (
SELECT game_id, lati, longi
FROM games
WHERE lati > ' . $minLat .
' AND lati <' . $maxLat .
' AND longi > ' . $minLon .
' AND longi < ' . $maxLon .
') AS firstcut
WHERE
acos(sin (' . $lat . ')
* sin (radians( lati ))
+ cos (' . $lat . ')
* cos (radians(lati))
* cos (radians(longi) - ' . $lon . '))
*' . $R . '<' . $rad .
' ORDER BY D');
If I execute this and print_r(result) the number of rows is displayed i.e 2.
However I cant treat, or convert this result into an array, so I cant
foreach($result as $row) { echo $row['id'] . ' + ' . $row['D']}
Or something similar.
If I paste this code and replace the php variables with real values into phpmyadmin, I get the green 'The query was executed successfully' message but no rows are returned (two rows should be returned, there is not even a 'This query returned zero rows' message).
I dont have much experience with this kind of nested/complex sql queries, so any help would be appreciated.
Thanks.
You need to pass DB::SELECT as the second param for DB::query().
You might want to use the as_array method from the database result. Or take a look here: http://fuelphp.com/docs/classes/database/usage.html#results

Categories