pass the previous records value for comparison to current records value

Here goes...





I have  a recordset.... that yields these values... as it loops through

$fld[3]
 
1
2
2
0
0
0
0
1
2
2
2
2
0

What I want to do is echo a comma after teh last 2... like so...

1
2
2
,
0
0
0
0
1
2
2
2
2
,
0

If a 0 is preceeded by a 2.... I am trying to compare the previous record to the current record...
if current record = 0 and previous record = 2 then echo ","



while (!$rs->EOF)
      {
            $cur=$fld[3];    // current record
            $prev=$fld[3];  // current record....

            IF ($cur==0 || $prev ==2  ){
                        echo "," ;
            }
            
   $rs->MoveNext();
}


So as you loop thru you should get this

      $cur=1;   $prev=;       
      $cur=2;   $prev=1;      
      $cur=2;   $prev=2;      
      $cur=0;   $prev=2;      
      $cur=0;   $prev=0;      
      $cur=0;   $prev=0;      
      $cur=0;   $prev=0;            
      $cur=1;   $prev=0;      
      $cur=2;   $prev=1;      
      $cur=2;   $prev=2;      
      $cur=2;   $prev=2;      
      $cur=2;   $prev=2;            
     $cur=0;   $prev=2;            
      $cur=0;   $prev=0;      

Hope this is clear...


concep86Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zappafan2k2Commented:
How about something like this:
$prev = '';        // initialize $prev
while (!$rs->EOF)
      {
        if ($fld[3] != 2 && $prev == 2)
                {
                echo ",";
                }
        echo $fld[3];
        $prev = $fld[3];
        $rs->MoveNext();
        }

Open in new window

Dave BaldwinFixer of ProblemsCommented:
Try this.
$cur=0;
$prev=0;
while (!$rs->EOF)
      {
            $cur=$fld[3];    // current record
            $prev=$fld[3];  // current record....

            if ($cur==0 && $prev ==2  ){
                        echo "," ;
            }
            echo $cur;
            $prev = $cur;
   $rs->MoveNext();
}

Open in new window

zappafan2k2Commented:
@DaveBaldwin,
If you look at your code, you will never echo a comma, because $cur will always equal $prev.
There is no real need for $cur.  $fld[3] is the current record.


@concep86,
Looking again at your post, I realize that you only want to echo a comma if a zero is preceded by a two.  My code should then be changed to
$prev = '';        // initialize $prev
while (!$rs->EOF)
      {
        if ($fld[3] == 0 && $prev == 2)
                {
                echo ",";
                }
        echo $fld[3];
        $prev = $fld[3];
        $rs->MoveNext();
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Dave BaldwinFixer of ProblemsCommented:
You're right.  I copied the question code and forgot to delete a line.
$cur=0;
$prev=0;
while (!$rs->EOF)
      {
            $cur=$fld[3];    // current record
            if ($cur==0 && $prev ==2  ){
                        echo "," ;
            }
            echo $cur;
            $prev = $cur;
   $rs->MoveNext();
}

Open in new window

concep86Author Commented:
I have tried both solutions...

zappafan2k2...

your solution...  with the addition of line 8 below... to compare the current value with the past value....
$prev='';
while (!$rs->EOF) 
	{
	if ($fld[3] == 0 && $prev == 2)
                {
                echo ",";
                }
    echo $fld[3]."-".$prev."<BR>";
        $prev = $fld[3];	
		
   $rs->MoveNext(); 
}

Open in new window


This is what I get...Still no comma.

1-
2-2
0-0
0-0
0-0
0-0
0-0
1-1
2-2
2-2
2-2
0-0
0-0
0-0
0-0



DaveBaldwin... this is the result from yours...


$cur=0;
$prev=0;
while (!$rs->EOF)
      {
            $cur=$fld[3];    // current record
            if ($cur==0 && $prev ==2  ){
                        echo "," ;
            }
            echo $cur."-".$prev."<BR>";
            $prev = $cur;
   $rs->MoveNext();
}

Open in new window



1-0
2-2
0-0
0-0
0-0
0-0
0-0
1-1
2-2
2-2
2-2
0-0
0-0
0-0
0-0

Something wrong with previous value...
zappafan2k2Commented:
When is $fld[3] being assigned?  I figured that it was in the $rs->MoveNext() command.
concep86Author Commented:
This is the full code


$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;   $conn->open($connStr); //Open the connection to the database
$query = "SELECT item, Description, Qty, [Level], CAST(co_line AS int) AS co_line, co_num  FROM bom WHERE (co_num = '632215' ) ORDER BY SEQ";
$rs = $conn->execute($query);
$num_columns = $rs->Fields->Count();

for ($i=0; $i < $num_columns; $i++) {
    $fld[$i] = $rs->Fields($i);
}


$prev='';
while (!$rs->EOF) 
	{
	if ($fld[3] == 0 && $prev == 2)
                {
                echo ",";
                }
        echo $fld[3]."-".$prev."<BR>";
        $prev = $fld[3];	
		
   $rs->MoveNext(); 
}

Open in new window

Dave BaldwinFixer of ProblemsCommented:
Here, this works.  I used an array and 'foreach' since I don't have your data in a database.  If you while loop is getting the correct data, you can take the inner part of the foreach and put it in your while loop.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php 
$rs = array(1,2,2,0,0,0,0,1,2,2,2,2,0);
$cur=0;
$prev=0;
foreach($rs as $val) {
	$cur=$val;    // current record
	echo $cur."-".$prev;
	if ($cur==0 && $prev ==2  ){
		echo "," ;
		}
	echo "<br>";
	$prev = $cur;
	}
 ?>
</body>
</html>

Open in new window

concep86Author Commented:
DaveBaldwin,

To use your solution, I would need to create an array from the recordset.....  ?

I am stuck... :-(
Dave BaldwinFixer of ProblemsCommented:
No, just put the correct code in the middle like below.
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;   $conn->open($connStr); //Open the connection to the database
$query = "SELECT item, Description, Qty, [Level], CAST(co_line AS int) AS co_line, co_num  FROM bom WHERE (co_num = '632215' ) ORDER BY SEQ";
$rs = $conn->execute($query);
$num_columns = $rs->Fields->Count();

for ($i=0; $i < $num_columns; $i++) {
    $fld[$i] = $rs->Fields($i);
}

$cur=0;
$prev=0;
while (!$rs->EOF) 
        {
        $cur=$fld[3];    // current record
        echo $cur."-".$prev;
        if ($cur==0 && $prev ==2  ){
                echo "," ;
                }
        echo "<br>";
        $prev = $cur;
                 
   $rs->MoveNext(); 
}

Open in new window

concep86Author Commented:
Here is what I have....



$rs = $conn->execute($query);
$num_columns = $rs->Fields->Count();

for ($i=0; $i < $num_columns; $i++) {
    $fld[$i] = $rs->Fields($i);
}

$cur=0;
$prev=0;
while (!$rs->EOF) 
        {
        $cur=$fld[3];    // current record
        echo $cur."-".$prev;
        if ($cur==0 && $prev ==2  ){
                echo "," ;
                }
        echo "<br>";
        $prev = $cur;
   $rs->MoveNext();
}

Open in new window



And the result...
1-0
2-2
0-0
0-0
0-0
0-0
0-0
1-1
2-2
2-2
2-2
0-0
0-0
0-0



I think the problem is that the $fld[3] assignment....



Dave BaldwinFixer of ProblemsCommented:
You may be right.  I'm not sure which method you're using.  Try this, it gets the next value from the database record inside the loop.
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB;   $conn->open($connStr); //Open the connection to the database
$query = "SELECT item, Description, Qty, [Level], CAST(co_line AS int) AS co_line, co_num  FROM bom WHERE (co_num = '632215' ) ORDER BY SEQ";
$rs = $conn->execute($query);
$num_columns = $rs->Fields->Count();

for ($i=0; $i < $num_columns; $i++) {
    $fld[$i] = $rs->Fields($i);
}

$cur=0;
$prev=0;
while (!$rs->EOF) 
        {
        $cur=$rs->Fields(3);    // current record
        echo $cur."-".$prev;
        if ($cur==0 && $prev ==2  ){
                echo "," ;
                }
        echo "<br>";
        $prev = $cur;
                 
   $rs->MoveNext(); 
}

Open in new window

concep86Author Commented:
The problem is in the $prev...

See the output... of

echo $cur."-".$prev;


1-0
2-2
0-0
0-0
0-0
0-0
0-0
1-1
2-2
2-2
2-2
0-0
0-0
0-0
0-0

This is what it should be... where is the initial 1... thats bold. It is being overwritten with the current value...

1-0
2-1
0-2
0-0
0-0
0-0
0-0
1-0
2-1
2-2
2-2
0-2
0-0
0-0
0-0
Dave BaldwinFixer of ProblemsCommented:
If I change my code to use a 'while' loop, it still works perfectly.  I don't understand the database method you are using.  It looks like a cross between PHP and ASP and I can't find anything in PHP that works quite that way.  This is what I get with the code below:

1-0
2-1
2-2
0-2,
0-0
0-0
0-0
1-0
2-1
2-2
2-2
2-2
0-2,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php 
$rs = array(1,2,2,0,0,0,0,1,2,2,2,2,0);
$cur=0;
$prev=0;
$i = 0;
while(isset($rs[$i])) {
	$cur=$rs[$i];    // current record
	echo $cur."-".$prev;
	if ($cur==0 && $prev ==2  ){
		echo "," ;
		}
	echo "<br>";
	$prev = $cur;
	$i++;
	}
 ?>
</body>
</html>

Open in new window

concep86Author Commented:
OK. Both solutions actually should have worked... but neither did... Not because there was something wrong with the code or solutions provided... but more so because of of other issues...

So I am accpeting both answers as accurate... they should have produced the desired effect.

Thanks
Dave BaldwinFixer of ProblemsCommented:
Thanks, I hope you get it working.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.