Page 1 of 1

Array manipulation Q?

Posted: Mon Jan 12, 2004 2:55 pm
by gomac
Hi,

I have a numerical array returned with a SELECT:

(9,7,6,5,4,3,1)

These numbers represent dates- with 9 being the most current, 1 the oldest. I'm unclear how to manipulate and access the individual elements in that array. I'm trying to set a couple of variables based on this array, and I've been futzing for days on it with no success.

Here's some code that shows what I'm trying to do:

Code: Select all

<?php
$snap=5;
$nav_sql = 'SELECT snapdate.date_id'
       . ' FROM snapdate, snapshot'
       . ' WHERE snapshot.owner =1 AND security_id =8 AND snapdate.date_id = snapshot.date_id'
       . ' ORDER BY date_id DESC ';
$nav_result = MySQL_query($nav_sql);
$num = mysql_numrows($nav_result);
$i=0;
while ($i < $num) &#123;
$date_id = mysql_result($nav_result,$i,"date_id");
++$i;

if ($date_id == $snap) &#123; 
$next = next($date_id);
$prev = prev($date_id);
&#125;

&#125;
?>
$snap is the "date" I'm looking at, in this case 5.

The SELECT is fine and I can echo my array, but I can't set $next or $prev.

I want to set $next to be 4 and $prev to be 6 (for when $snap is 5 as in this example) . The array won't always have consecutive numbers, so just adding to $snap is not an option.

Any ideas how?

Thanks for any one who takes a whack at this - I hope it makes sense,
gord

Posted: Mon Jan 12, 2004 3:57 pm
by xisle
somethin like this maybe...

Code: Select all

$date_id= mysql_result($nav_result,$i,"date_id");

if ($date_id == $snap) &#123;  
  if($i < ($num-1))&#123;
    $nextrow=$i+1;
    $next = mysql_result($nav_result,$nextrow,"date_id");
  &#125;
  if($i > 0)&#123;
    $prevrow=$i-1;
    $prev = mysql_result($nav_result,$prevrow,"date_id");
  &#125;
&#125;

Posted: Mon Jan 12, 2004 5:00 pm
by gomac
Well this is definetly progress! Thanks - but this returns 5,3 not 6,4. I'm trying to think my way through what this conditional is doing but my head starts to hurt...

if I fudge with the + around $nextrow (e.g. $nextrow=$i+2) it returns 5,2, which totally confuses me... any more insight? Anyone?
xisle wrote:somethin like this maybe...

Code: Select all

$date_id= mysql_result($nav_result,$i,"date_id");

if ($date_id == $snap) &#123;  
  if($i < ($num-1))&#123;
    $nextrow=$i+1;
    $next = mysql_result($nav_result,$nextrow,"date_id");
  &#125;
  if($i > 0)&#123;
    $prevrow=$i-1;
    $prev = mysql_result($nav_result,$prevrow,"date_id");
  &#125;
&#125;

Posted: Mon Jan 12, 2004 10:12 pm
by gomac
If anyone's interested I solved this thanks to xisle's help. The code that makes this work is:

Code: Select all

if ($date_id == $snap) &#123;
    $nextrow=$i-2;
    $next = mysql_result($nav_result,$nextrow,"date_id"); 
  &#125; 
if ($date_id == $snap) &#123;  
    $prevrow=$i; 
    $prev = mysql_result($nav_result,$prevrow,"date_id"); 
  &#125;
I don't understand why - but it works. If someone can explain that would be grand. Probably something to do with the cursor in the array, but it doesn't make sense to me.

Posted: Tue Jan 13, 2004 2:21 pm
by xisle
you are incrementing $i++ before the code,
move it after the prev/next statements and the original version should work...