0

I have what will, I'm sure, turn out to be a very simple calculation but I'm drawing a blank... Too many years since High School I think

What I have is a table that maintains positions and these positions are stacked, with each successive row containing twice as many positions as the previous row.

What I need to be able to determine based on record number, starting with 1 and incrementing with each new record...

If I am inserting what will be record number 3450, which row will it be on, and what relative position within that row will it hold.

The math will actually be easy to determine the relative position after determining the row number, but I'm blank on the formula to determine the row.

Any help out there this late at night??

Thanks in advance
Douglas

3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by showman13
0

Just realized my manual solution was bogus so I erased it... Guess it is time for bed.
There is always tomorrow.


Thanks again
Guess I'll hope for an answer when I wake up...

Edited by showman13: n/a

0

Well, my question was stated in the first post, but I have worked out a solution that is adequate... If there is a better or faster or one that uses less overhead, I'd love to hear the suggestions...

This code works fine and provides the results that I wanted, but if you see any flaws in the logic or execution, please point them out.

Thanks

Here is what I came up with:

$sql = "SELECT MAX(pos_id) FROM position_".$level;// gets highest record number
    $result = mysql_query($sql) or die(mysql_error()) ;
    $row = mysql_fetch_array($result) ;
    $next_pos = $row[0]+1;
$line_of = 1;     // starts at top of matrix
$avail_pos = 1;   // Starts with first available position
$position=$next_pos-100000000;// auto increment starts at 100000001
WHILE ($avail_pos<$position){//Maximum iterations will be 31 to exceed 1 billion
    $line_of*=2; // same as $line_of = $line_of*2   Number of position on line
    $avail_pos+=$line_of;  // same as $avail_pos = $avail_pos+$line of
} // falls through when $avail_pos > next_pos
    // $line_of now contains which line the new position will be placed on.
    $line_pos=$position-$line_of+1; // determine the placement on the line
0

I'm a little confused - what are you doing, something like this?

ID   POS   ITEM
1      1    1
2      2    1
3      2    2 
4      3    1
5      3    2
6      3    3
7      4    1
8      4    2

ETC

If so you need to create an nth term expression for the conversions -EDIT (is there one??)

Edited by diafol: n/a

0

The width of each row doubles (# of positions)
and the ID to be created should tell me which row and the position within that row.

more like

id    row    position
1      1       1
2      2       1
3      2       2
4      3       1
5      3       2
6      3       3
7      3       4
8      4       1
9      4       2
10     4       3
11     4       4
12     4       5
13     4       6
14     4       7
15     4       8

Edited by showman13: formatting change

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.