Array manipulation Q?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
gomac
Forum Newbie
Posts: 11
Joined: Mon Jan 12, 2004 2:55 pm
Location: Toronto

Array manipulation Q?

Post 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
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post 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;
User avatar
gomac
Forum Newbie
Posts: 11
Joined: Mon Jan 12, 2004 2:55 pm
Location: Toronto

Post 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;
User avatar
gomac
Forum Newbie
Posts: 11
Joined: Mon Jan 12, 2004 2:55 pm
Location: Toronto

Post 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.
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

you are incrementing $i++ before the code,
move it after the prev/next statements and the original version should work...
Post Reply