2

应用此代码后:$lines = explode("\n", $val);where $val = $_POST['result'];and $_POST['result'];is from a textarea where these strings are being exploded:

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()); 

然后我需要添加etdblock的值,eta然后只更新具有最大值的表,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下一个的值,etd然后计算它的etaetd+ block)。然后该waiting列的值应该是 firsteta减去 next etd。像这样的东西:

在此处输入图像描述

这里至关重要和重要的是第一个eta到下一个的传递etd和计算。当我需要遍历分解的值时,如何完成它?请帮我。对于冗长的解释,我很抱歉,但我希望你们所有人都能理解我想要实现的目标。谢谢!

4

2 回答 2

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()); 
于 2012-11-08T06:56:16.703 回答
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

于 2012-11-09T06:31:41.093 回答