PHP循环语句

时间:2012-11-08 05:29:33

标签: php mysql loops

应用此代码时,$lines = explode("\n", $val);其中$val = $_POST['result'];$_POST['result'];来自发布这些字符串的文本区域:

  

A - B - > 1:00

     

B - A - > 1:30

分别是$lines[0] = "A - B -> 1:00"$lines[1] = "B - A -> 1:30"

foreach ($lines as $line)中,我会在每次->见面时再次爆炸。像这样:

    $fields = explode('->', $line);
    $loc = trim($fields[0]);
    $bltime = trim($fields[1]);

所以$loc =“A - B”和$bltime =“1:00”。然后我将这些值与我存储在会话中的值一起保存在表中,即$_SESSION[rno],它是来自另一个表的主键,并从同一个表中获取另一个值。

    $e=mysql_query("select etd from reservation 
      where reservno = '$_SESSION[rno]'") or die(mysql_error());
    $f=mysql_fetch_array($e);
    $g=$f['etd'];
    mysql_query("insert into pdf(reservno, block, location, etd) 
      values ('$_SESSION[rno]', '".mysql_escape_string($bltime)."','".
      mysql_escape_string($loc)."', '$g')") or die(mysql_error()); 

然后我需要为etd的值添加blocketa的值,然后只更新具有最大pdf_id的表,该表是主键的pdf $a=mysql_query("select pdf_id as 'maxpdf' from pdf where pdf_id in (select max(pdf_id) from pdf where reservno = '$_SESSION[rno]')") or die(mysql_error()); $b=mysql_fetch_array($a); $c=$b['maxpdf']; $h=mysql_query("select addtime(etd, block) as 'eta' from pdf where pdf_id = '$c'") or die(mysql_error()); $j=mysql_fetch_array($h); $k=$j['eta']; mysql_query("update pdf set eta = '$k' where pdf_id = '$c'") or die(mysql_error()); 表后。

pdf

所以我在$_POST['result'];表格中运行我上面发布的所有代码时仍然会有以下值仍然基于eta的上述值:

enter image description here

我想要做的是:使第一个计算etd下一个eta的值,然后计算etd block + { {1}})。然后,waiting列的值应为eta减去下一个etd。像这样:

enter image description here

这里至关重要且重要的是将第一个eta传递给下一个etd并进行计算。当我需要遍历爆炸值时,我怎样才能完成它?请帮我。对于长篇解释我很抱歉,但我希望你们所有人都能理解我想要达到的目标。谢谢!

2 个答案:

答案 0 :(得分:1)

Where you insert into PDF, Before Inserting into PDF,

1) Get the Maximum Pdf_Id record, (i.e. the last inserted record)
2) Select the eta from this record and save it in a variable.
3) While inserting, insert the etd as the recorded variable.

就是这样。

编辑代码,

而不是以下代码,

  $e=mysql_query("select etd from reservation 
      where reservno = '$_SESSION[rno]'") or die(mysql_error());
    $f=mysql_fetch_array($e);
    $g=$f['etd'];

使用,替换

   $NextETD = "Some Dfault Value, you may set it to 12:00";
   $a=mysql_query("select * from pdf where pdf_id in 
      (select max(pdf_id) from pdf where reservno = '$_SESSION[rno]')") 
      or die(mysql_error());
    $b=mysql_fetch_array($a);
    $LastETA=$b['eta'];
    if($LastETA!=null){
        $NextETD = $LastETA;
    }
   mysql_query("insert into pdf(reservno, block, location, etd) 
      values ('$_SESSION[rno]', '".mysql_escape_string($bltime)."','".
      mysql_escape_string($loc)."', '$NextETD')") or die(mysql_error()); 

答案 1 :(得分:0)

我已经想出如何将第一个eta值传递给第二个etd,依此类推。这是我用过的整个循环语句..

$lines = explode("\n", $val);

foreach ($lines as $line) 
{
    $fields = explode('->', $line);
    $loc = trim($fields[0]);
    $bltime = trim($fields[1]);
    $e=mysql_query("select eta as 'etd' from pdf where pdf_id in (select max(pdf_id) from pdf where reservno = '$_SESSION[rno]')") or die(mysql_error());
    $f=mysql_fetch_array($e);
    $g=$f['etd'];
    if(empty($g))
    {
    $m=mysql_query("select etd from reservation where reservno = '$_SESSION[rno]'") or die(mysql_error());
    $o=mysql_fetch_array($m);
    $p=$o['etd'];
    $temp=$p;
    }
    else
    {
    $temp=$g;
    }
    mysql_query("insert into pdf(reservno, block, location, etd) values ('$_SESSION[rno]', '".mysql_escape_string($bltime)."','".mysql_escape_string($loc)."', '$temp')") or die(mysql_error());
    $a=mysql_query("select pdf_id as 'maxpdf' from pdf where pdf_id in (select max(pdf_id) from pdf where reservno = '$_SESSION[rno]')") or die(mysql_error());
    $b=mysql_fetch_array($a);
    $c=$b['maxpdf'];
    $h=mysql_query("select addtime(etd, block) as 'eta' from pdf where pdf_id = '$c'") or die(mysql_error());
    $j=mysql_fetch_array($h);
    $k=$j['eta'];
    mysql_query("update pdf set eta = '$k' where pdf_id = '$c'") or die(mysql_error());

}

if(empty($g))语句检查查询select eta as 'etd' from pdf where pdf_id in (select max(pdf_id) from pdf where reservno = '$_SESSION[rno]')是否产生空集。如果查询结果为空,则它是第一个要插入的数据,因此$temp将保留结果查询select etd from reservation where reservno = '$_SESSION[rno]'的值。如果不是这样,这意味着已经预先输入了数据,因此$temp变量将保存由$g保留的结果查询,简单地说就是$temp = $g