xjshiya
xjshiya

Reputation: 925

PHP looping statement

Upon applying this code: $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

So $lines[0] = "A - B -> 1:00" and $lines[1] = "B - A -> 1:30" respectively.

In foreach ($lines as $line) , I'll explode it again for every -> meet. Like this:

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

So $loc= "A - B" and $bltime = "1:00". Then I'll save these values in a table along with a value I stored in a session namely $_SESSION[rno] which is a primary key from another table and fetch another value from the same table.

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

Then I need to add the values of etd and block for the value of eta then just update the table having the maximum pdf_id which is the primary key of pdf table after.

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

So I'll have the following values in the pdf table upon running the whole codes I posted above still based on the above value of $_POST['result'];:

enter image description here

What I want to do is to make this possible: have the first computed eta the value of the next etd then compute for it's eta (etd + block). And then the waiting column should have the value of first eta minus the next etd. Something like this:

enter image description here

What is crucial and important here is the passing of the first eta to the next etd and the computation. How can I get it done when I'm required to loop through the exploded values? Please help me. I'm sorry for the long explanation but I want all of you to understand what I am trying to achieve. Thanks!

Upvotes: 2

Views: 138

Answers (2)

xjshiya
xjshiya

Reputation: 925

I've figured out how to pass the 1st eta value to the 2nd etd and so on. Here's the whole looping statement I used..

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

}

The if(empty($g)) statement checks if the query select eta as 'etd' from pdf where pdf_id in (select max(pdf_id) from pdf where reservno = '$_SESSION[rno]') results an empty set or not. If the query results empty, then it is the first data to be inserted, therefore, $temp will hold the value of the resulted query select etd from reservation where reservno = '$_SESSION[rno]'. If otherwise, it means that there's already data that was inputted beforehand so the $temp variable will hold the resulted query which is held by $g, so simply, $temp = $g.

Upvotes: 0

SHAKIR SHABBIR
SHAKIR SHABBIR

Reputation: 1295

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.

That's it.

Edit your code like,

Instead of the following code,

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

Use, this replacement

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

Upvotes: 1

Related Questions