homepage Welcome to WebmasterWorld Guest from 54.196.197.153
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Array Newbie
sorting this array.
phlogit




msg:3362772
 9:55 am on Jun 9, 2007 (gmt 0)

ARRAY NEWBIE AND PHP NEWBIE FOR THAT MATTER.

Hi i am trying to do the following as advised by eelixduppy.

eelixduppy - If you want to sort them using your php function(distance) you are going to have to push all of the distances into an array and sort them using sort(), however, you are going to have to keep track of which ad belongs to which distance. For this, I would set the index for each element of the distance array to something unique for each ad.

$query = "select * from ads";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result);
$key=$row[id];

$nearest_distance = array();

$lati[1]=$row[latitude];
$long[1]=$row[longitude];

$distance = round(distance($lati[0], $long[0], $lati[1], $long[1], "M"));

$nearest_distance[$key] = $distance;

sort($nearest_distance);

echo $nearest_distance[0];

I have no idea on how to use arrays and have gathered the above code from previous posts any help will be greatly appreciated. Thanx guys.

 

jatar_k




msg:3362858
 3:00 pm on Jun 9, 2007 (gmt 0)

Well a couple things

what specifically isn't working?

If you want to analyze your sort then you should print_r it before and after like so

echo '<p>before sort: <pre>',print_r($nearest_distance),'</pre>';
sort($nearest_distance);
echo '<p>after sort: <pre>',print_r($nearest_distance),'</pre>';

that might help you better visualize what is happening

phlogit




msg:3363014
 6:48 pm on Jun 9, 2007 (gmt 0)

Well jatar thanx for replying basicaly i am trying to sort my ads according to the distance echo'd. the distance is not in the database and is using a php function(called distance). so my ads are displayed as follows.

//start while loop for each ad
while (my condition) {

//here goes my looks and other code for the ad which will be looped for every add

$query = "select * from ads";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result);
$key=$row[id];

$nearest_distance = array();

$lati[1]=$row[latitude];
$long[1]=$row[longitude];

$distance = round(distance($lati[0], $long[0], $lati[1], $long[1], "M"));

$nearest_distance[$key] = $distance;

sort($nearest_distance);

echo $nearest_distance[0];

} //end while for each ad.

eelixduppy previously guided me to the fact that i needed to push all my distances for each ad in to an array and the use sort on it, which is right i think cos it does make sense to me. The only problem is i dont know how to do it. The above code is what i have formulated so far but it does nothing but just display the distance for each ad it doesnt sort it. i think i need to have the array outside of the while so its populated before it is sorted and i think that it can only be sorted once all the ads have been looped through. As follows.

$nearest_distance = array(); // array outside loop
//start while loop for each ad
while (my condition) {

//here goes my looks and other code for the ad which will be looped for every add

$query = "select * from ads";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result);
$key=$row[id];

$lati[1]=$row[latitude];
$long[1]=$row[longitude];

$distance = round(distance($lati[0], $long[0], $lati[1], $long[1], "M"));

$nearest_distance[$key] = $distance;

} //end while for each ad.

sort($nearest_distance); //sort after the array has been populated

echo $nearest_distance[0]; // but then echo has to be outside loop it needs to be in every ad and move each ad with it. Thats why i am trying to index it with the id of each ad so stays linked to something unique of each ad like eelixduppy said, but im not sure if i am doing that right.

so thats what doesnt work lol seems like everything. Your help will be appreciated and you will be prayed for if you can help me.

CDNQuilter




msg:3363160
 1:01 am on Jun 10, 2007 (gmt 0)

Sorting outside the loop is what you want but
sort($nearest_distance);
assigns new keys so you will lose the id value of the keys.

You need to use asort() instead as it will keep the values of the keys and just change the order.

But, first of all, in your while loop, put the ads into a second array inside your while loop using the id as the key for that array also, e.g.
$ads[$key]= $row[advertisement]; where advertisement is whatever you call the ad in the database.

You will end up with two arrays with the id as the keys - your array of distances and your array of ads.

Then take your array containing the distances and sort it:

asort()$nearest_distance;

Now to see the ads in distance order:

foreach($nearest_distance as $key =>$value){
echo $ads[$key];
}
You should get the ads output in distance order.

Use print_r($nearest_distance) and print_r($ads);scattered throughout your code to see what the arrays are doing. This will help you learn how to use arrays. As you are discovering, you cannot do much programming without arrays.

good luck

edited to add
ps I could use some prayers, that's for sure - so thanks in advance!

[edited by: CDNQuilter at 1:02 am (utc) on June 10, 2007]

phlogit




msg:3363619
 5:07 pm on Jun 10, 2007 (gmt 0)

Hi CDNQuilter,

Thanx for replying mate. i see what your saying by using two arrays i can probably solve the problem but i cant seem to get my ads into the $ads array like you mentioned.

$ads[$key]= $row[advertisement]; where advertisement is whatever you call the ad in the database.

so firstly i have to start the array for the ads in the while loop as such

$ads = array();

after that within the loop i populate the array with my column names of the addwhiuch is in the database or just one unique column name because in place of where u say advertisment i have header, price, etc different fields that will be shown in the adds and all these have to be looped throughout all ads so how do i populate the $ads array with all this information aswell as keeping the looks of the ads in tact and linking the $distance.

CDNQuilter




msg:3363628
 5:25 pm on Jun 10, 2007 (gmt 0)

I would suggest solving the problem one step at a time.

First of all, get the sort working so that your $nearest_distance array contains the id's of the ad records in the order you need.

Because the information for your ads is in a variety of columns it will be much easier to just make a second pass through the database, using your $nearest_distance array than to push all the ad information into arrays.

If your database is not huge, the efficiency difference between one and two passes may not be very great.

Depending on how big the ads are, it may even be more efficient to make two passes of the database than to create arrays to keep all the ad information. (But I have no idea - I am a noob as far as mysql is concerned!)

So:
Step 1
Get it working with two passes of the database and see if the response is OK.
Step 2
To make only one pass of the database, what you will have to do is to create an array for each heading in the database that you need to extract, and, using the unique id as the key, put each of these into their own array. Then to print out the ads, you will refer to the arrays rather than printing from the database.

For a newbie it is more important to get ONE correct solution at a time than to go for the most efficient solution first time around, IMHO. (I have lots of systems experience, but I have only learned php in the past 6 mo so I am a a noob myself!)

good luck and thanks in advance for the prayers

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved