Page 1 of 1

PHP Mysql Array manipulation problem

Posted: Fri May 22, 2009 7:58 am
by EJ1969
Hi Guys

I am new to php/mysql and need some help.

I want to amend some existing code which is used in a search page so that it can calculate each items distance from the user.

I have summarised the existing code below :

Firstly, the code checks to see if there are any items which meet the users query.

Code: Select all

$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.

Code: Select all

 
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 a while loop to format the array ready for display on screen.

Code: Select all

 
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 so that it does the distance calculation on each line of the existing array and stores each value in a new column in the array so I can use it for sorting. Any help re. the code would be appreciated.

My initial thoughts were to call the function within a while loop with come kind of $t++ counter variable, but I am not sure of the precise code.

Many thanks

EJ