JamesXL1967 Posted May 20, 2009 Share Posted May 20, 2009 Hi Guys I am new to php/mysql and need some help. I want to amend the following code summary which is used in a search page so that it can calculate the items distance from the user (and then allow for sorting according to distance). User_id and user_location are stored in a database called users and I have a function to calculate the distance. The existing code is as follows : Firstly, the code checks to see if there are any items which meet the users query. $plf_items = $db->get_sql_number("SELECT a.unit_id FROM " . DB_PREFIX . "units a " . $where_query . " GROUP BY a.unit_id"); The code then puts the relevant items into an array. if ($plf_items) { $sql_select_items = $db->query("SELECT a.item_id, a.name, a.price, a.currency, a.item_location FROM " . DB_PREFIX . "items a " . $force_index . " " . $where_query . " GROUP BY a.item_id ORDER BY " . $order_field . " " . $order_type . " LIMIT " . $start . ", " . $limit); } The code then uses this loop to format the array ready for display on screen. while ($item_details = $db->fetch_array($sql_select_items)) { <snip> } The function I want to incorporate requires the variables user_location (which I can collect via a mysql query) and item_location (collected in sql_select_items above) to be passed to it but I am not sure how to amend the above code to incorporate the distance function and thereby get the function to calculate the distance of each item in the array from the user, ready for me to sort via distance (which I know how to code). Any help re. the code would be appreciated. Many thanks J Link to comment https://forums.phpfreaks.com/topic/158928-php-mysql-problem/ Share on other sites More sharing options...
radi8 Posted May 20, 2009 Share Posted May 20, 2009 Seeing more of your code would help... but it seems as though the data you want is coming from the sq_select_items query, right? If so, then to retrieve those items, use <?php $loc=$item_details[4]; // push to the function $dist=distFunction($user_id,$loc,$var3...); ?> Link to comment https://forums.phpfreaks.com/topic/158928-php-mysql-problem/#findComment-838212 Share on other sites More sharing options...
JamesXL1967 Posted May 20, 2009 Author Share Posted May 20, 2009 Many thanks Radi8. Could you please explain the code a little more? Such as where it needs to go and what the [4] relates to because I thought item location was in the 5th column in the array. My biggest concern was that there may be 100 items (and therefore rows) in the array and the code needs to calculate distance for each item and then allow me to sort by distance, so in effect its creating a new column in the array. Would I be better using a while loop and adding some kind of counter variable to allow for the calculating distance for each row? Cheers Link to comment https://forums.phpfreaks.com/topic/158928-php-mysql-problem/#findComment-838220 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.