concep86
asked on
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...
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...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();
}
ASKER
I have tried both solutions...
zappafan2k2...
your solution... with the addition of line 8 below... to compare the current value with the past value....
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...
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...
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();
}
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();
}
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...
When is $fld[3] being assigned? I figured that it was in the $rs->MoveNext() command.
ASKER
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();
}
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>
ASKER
DaveBaldwin,
To use your solution, I would need to create an array from the recordset..... ?
I am stuck... :-(
To use your solution, I would need to create an array from the recordset..... ?
I am stuck... :-(
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();
}
ASKER
Here is what I have....
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....
$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();
}
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....
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();
}
ASKER
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
So I am accpeting both answers as accurate... they should have produced the desired effect.
Thanks
Thanks, I hope you get it working.
Open in new window