Hi Everyone,

I'm having some issues in the construction of an algorithm for a project. This is just a fraction of the algorithm and my problem is this:

I'm trying to build a PHP function that gets the values of the first and second rows of a table and subtract them. Then the function gets the second and third values and subtract them, then the third and forth and so on until the end of the table. (The table will be sorted ASC and the value of the subtraction will be use for posterior use) Is this possible?

Thank you very much. All the help provided will be helpfull

Recommended Answers

All 5 Replies

Can you provide an example of the expected operation? Does the table contain numerical values? You need to provide more details in order for someone to be able to help...

Member Avatar for diafol

(The table will be sorted ASS and the value of the subtraction will be use for posterior use)

Oooh, sounds painful - do you mean like a suppository?

Anyway, post the code that you have so far.

I'm sorry you are right.

I have a table named position which has the columns: P_ID (int) Latitude (decimal) and Longitude (decimal).

Having for example this values applied:

Figure 1 : My table
P_ID Latitude Longitude
1 23,4 56,87
2 53,12 56,5
3 12,4 89,4
4 12,4 86,4
5 3,45 6,67

Figure 2: Ascending Latitude
P_ID Latitude Longitude
5 3,45 6,67
3 12,4 89,4
4 12,4 86,4
1 23,4 56,87
2 53,12 56,5

My goal is to order the Latitude column ascending, pick the first value of the table and the second value of the table (in my example 3,45 and 12,4). If second value minus first value is lower than let's say for example the number 5 the function searches on the Longitude table the correspondent longitude ( in my example 6,67 and 89,4 ) and does exactly the same subtraction.

Is it clear ? I guess it's a little bit confusing and I guess I'll have a place from where to start if I at least learn how to make a PHP Loop that goes through ALL rows of a column and compares the first with second, the second with third, etc...

Sorry for the mess

Is there any way to select just a row from a table ? Like selecting a position from an array list... where position[2] would be the second line of the table.. is that possible in php+sql ?

Member Avatar for diafol

Is there any way to select just a row from a table ? Like selecting a position from an array list... where position[2] would be the second line of the table.. is that possible in php+sql ?

Yes. See the LIMIT clause. Check out the MySQL online manual:

http://dev.mysql.com/doc/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.