Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP循环语句_Php_Mysql_Loops - Fatal编程技术网

PHP循环语句

PHP循环语句,php,mysql,loops,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$行中作为$行,我将在每次->见面时再次分解它。像这样: $fields = explode('->', $line); $loc = trim($fie

应用此代码时:$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$行中作为$行,我将在每次->见面时再次分解它。像这样:

    $fields = explode('->', $line);
    $loc = trim($fields[0]);
    $bltime = trim($fields[1]);
所以$loc=A-B和$bltime=1:00。然后,我将这些值与存储在会话中的值一起保存在一个表中,即$\u 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的值,并为eta的值添加block,然后只更新具有最大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());
因此,在运行我上面发布的所有代码时,仍然基于上面的$u POST['result'];值,我将在pdf表中有以下值:

我想做的是让这成为可能:让第一个eta计算出下一个etd的值,然后计算它的eta etd+块。然后等待列的值应该是第一个eta减去下一个etd。大概是这样的:

这里至关重要的是将第一个eta传递给下一个etd和计算。当我需要循环遍历分解的值时,如何才能完成它?请帮帮我。很抱歉,我的解释太长了,但我希望你们都明白我在努力实现什么。谢谢

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

我已经知道如何将第一个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());

}

ifempty$g语句检查查询select eta as'etd'from pdf where pdf\u id in select maxpdf\u id from pdf where reservno='$\u SESSION[rno]'是否导致空集。如果查询结果为空,则它是要插入的第一个数据,因此,$temp将保留结果查询select etd from RESERVICE的值,其中reservno=“$”会话[rno]”。否则,这意味着已经有预先输入的数据,$temp变量将保存由$g保存的结果查询,所以简单地说,$temp=$g

请注意,mysql的弃用过程已经启动。请改用。您绘制的表格是reservation或PDF???@SHAKIRSHABBIR,第一个表格是table PDF。第二个是我想要的PDF表格的样子。谢谢。我怎样才能使它在循环语句中工作?请参见编辑。谢谢。在第一条惰性语句之前,请确保您检查是否至少返回了一条记录。我应该插入查询select etd from RESERVICE中的第一条eta,其中reservno=“$”会话[rno]”。如果该查询在loop语句下,如何更改第二个etd?很抱歉,我没有这么多,但我不太明白。假设表中没有记录,在第一封信中,您会得到eta的值为null。如果有$eta,您应该进行检查=null{$etd=$eta}我可以根据此查询的结果设置默认值吗:从reservation中选择etd,其中reservno='$\ U会话[rno]'?