开发者

php mysql check previous row

I have开发者_Go百科 output from a select query as below

id          price  valid    
1000368     69.95   1       
1000369     69.94   0       
1000370     69.95   0 

now in php I am trying to pass the id 1000369 in function. the funciton can execute only if the valid =1 for id 1000368. if it's not 1 then it will throw error. so if the id passed is 1000370, it will check if valid =1 for 1000369.

how can i check this? I think it is logically possible to do but I am not able to code it i tried using foreach but at the end it always checks the last record 1000370 and so it throws error.

regards


Use a boolean variable:

<?php
$lastValid=false;
while($row = mysql_fetch_array($result))
{
    if ($lastValid) {
        myFunction();
    }
    $lastValid = $row['valid'];
}
?>

(Excuse possible errors, have no access to a console at the moment.)


If I understand correctly you want to check the if the previous id is valid.

$prev['valid'] = 0;

foreach($input as $i){

    if($prev['valid']){

        // Execute function

    }

    $prev = $i;

}


<?php
$sql = "SELECT * FROM tablename";
$qry = mysql_query($sql);

while($row = mysql_fetch_array($qry))
{
    if ($row['valid'] == 1)
    {
        // do some actions
    }
}
?>


I really really recommend walking through some tutorials. This is basic stuff man.

Here is how to request a specific record:

//This is to inspect a specific record
$id = '1000369'; //**some specified value**
$sql = "SELECT * FROM data_tbl WHERE id = $id";
$data = mysql_fetch_assoc(mysql_query($sql));

$valid = $data['valid'];

if ($valid == 1)
  //Do this
else
  //Do that

And here is how to loop through all the records and check each.

//This is to loop through all of it.
$sql = "SELECT * FROM data_tbl";
$res = mysql_query($sql);
$previous_row = null;

while ($row = mysql_fetch_assoc($res))
{
  some_action($row, $previous_row);
  $previous_row = $row; //At the end of the call the current row becomes the previous one. This way you can refer to it in the next iteration through the loop
}

function some_action($data, $previous_data)
{
  if (!empty($previous_data) && $condition_is_met)
  {
    //Use previous data
    $valid = $previous_data['valid'];
  }
  else
  {
    //Use data
    $valid = $data['valid'];
  }

  if ($valid == 1)
  {
    //Do the valid thing
  }
  else
  {
    //Do the not valid thing
  }

  //Do whatever
}

Here are some links to some good tutorials: http://www.phpfreaks.com/tutorials
http://php.net/manual/en/tutorial.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜