Jump to content

get the right number in loop


canabatz

Recommended Posts

Hi all,

 

i have this records that coming from the database:

 

0

0

0

1

1

1

1

1

0

0

1

1

1

1

1

1

 

 

i want to build a loop that will do like that:

 

if ($res==0) {$res11==number}

 

if ($res was allready 0 ignore it)

 

if ($res==1 ignore it and all other $res=1 and wait till it's $res=0 again

 

if ($res==0 again) { $res11==newnumber }

 

please guide me to doing that, im stuck!

 

thanks

Link to comment
Share on other sites

I'm a little confused by your 'logic', but I think what you are trying to say is that you want to set a value each time the result zero is encountered IF the previous result was not 0.

 

$lastRes = '';
while($row = mysql_fetch_assoc($result))
{
    if($row['res'] == 0 && $lastRes != 0)
    {
        //This line is only executed if 'res'=0 and the last 'res' was not 0
        $res11 = $row['number'];
    }
    $lastRes = $row['res'];
}

Link to comment
Share on other sites

The code I provided was only mock code based upon the requirements as I understood it. You did not provide enough information to provide a full-fledged solution or to even validate it was the correct solution for you. having said that . . .

 

but if there is res=1

 

how do i skip it?

 

With the code I provided, you don't need to skip. The if() condition in that loop would only update $res11 if the 'res' value is 0 AND the last value was not 0. But, you don't want to 'skip' the 1's because you need to update $lastRes with the 1.

 

your code is good, but only for one number.

 

the code dose not get the new number result!

 

any why to do it differently ?

I'm really not understanding you here. I'm guessing English is not your native language so please try to be specific. Can you provide an example of the data you are using and "how" the output is not what you expect/want?

 

EDIT: I did find one bug which would prevent the very first record from being processed correctly - if the first record's res value was 0. But, all other scenarios will work as I intended. The problem was that I set the initial value of $lastRes to an empty string and when I did the comparison

$lastRes != 0

It was resulting in true, because using a non type specific comparison and empty string is logically equal to 0 (or false). This can be fixed by making the comparison type specific or setting th einitial value to something like a negative 1.

 

Here is the slightly modified code. I replaced the while() loop with a foreach() loop so I could test the process on an array using values similar to what you had in your first post. I have also included the output of the test.

 

//Array of value to test against
$testArray = array(
    array('res' => 0, 'number' => 1), //Starting 0 value
    array('res' => 0, 'number' => 2),
    array('res' => 0, 'number' => 3),
    array('res' => 1, 'number' => 4),
    array('res' => 1, 'number' => 5),
    array('res' => 1, 'number' => 6),
    array('res' => 1, 'number' => 7),
    array('res' => 1, 'number' => ,
    array('res' => 0, 'number' => 9), //Starting 0 value
    array('res' => 0, 'number' => 10),
    array('res' => 1, 'number' => 11),
    array('res' => 1, 'number' => 12),
    array('res' => 1, 'number' => 13),
    array('res' => 1, 'number' => 14),
    array('res' => 1, 'number' => 15),
    array('res' => 0, 'number' => 16) //Starting 0 value
);

$lastRes = 1;
//while($row = mysql_fetch_assoc($result))
foreach($testArray as $row)
{
    if($row['res'] == 0 && $lastRes !== 0)
    {
        //This line is only executed if 'res'=0 and the last 'res' was not 0
        $res11 = $row['number'];
        echo "A New '0' was encountered. res11 now equals {$res11}<br>\n";
    }
    $lastRes = $row['res'];
}

 

Output

A New '0' was encountered. res11 now equals 1
A New '0' was encountered. res11 now equals 9
A New '0' was encountered. res11 now equals 16

Link to comment
Share on other sites

Hi mjdamato , i dont know how to thank you for taking the time to write and test this code.

 

your code is perffect! but i got small problem.

 

im trying to get the results from the database while doing what i need .

 

$sql=mysql_query("SELECT *   FROM `my_table` ")or die(mysql_error());
while ($row=mysql_fetch_assoc($sql))
{



$testArray=     

array(

array ('res' =>$row['something'], 'number' =>$row['something else']),


);

}

$lastRes = 1;




foreach($testArray as $row)
{
    if($row['res'] == 0 && $lastRes !== 0)
    {
        //This line is only executed if 'res'=0 and the last 'res' was not 0
        $res11 = $row['number'];
        echo "A New '0' was encountered. res11 now equals {$res11}<br>\n";
    }
    $lastRes = $row['res'];
}

 

im not geting anny result back , if i put your test code it's working!!

 

can it be a problem to add returned results from the database in the while loop!!

 

thanks a Billion!

Link to comment
Share on other sites

You don't need to dump the DB results into an array, simple run the same code in the while() loop when extracting the results. But, the problem with your code above is that you are redefining the array on each iteration of the while() loop instead of appending the results!

 

The code should look something like this (replace 'res' and 'number' with the appropriate DB field names)

$sql = mysql_query("SELECT * FROM `my_table` ")or die(mysql_error());
$lastRes = 1;
while ($row=mysql_fetch_assoc($sql))
{
    if($row['res'] == 0 && $lastRes !== 0)
    {
        //This line is only executed if 'res'=0 and the last 'res' was not 0
        $res11 = $row['number'];
        echo "A New '0' was encountered. res11 now equals {$res11}<br>\n";
    }
    $lastRes = $row['res'];
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.