problem of foreach()

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
valen53
Forum Contributor
Posts: 137
Joined: Tue Aug 27, 2002 9:29 am

problem of foreach()

Post by valen53 »

i got a page contain few of edit function, such as..
i select the 100 record. Then i can change the "status" - one of the select option. At the same time, i can change the time also. below is the code i used.

Code: Select all

<?php

foreach ($_POST['status'] as $status) {
   foreach ($status as $status1) {
      foreach ($status1 as $status2) {
 ......my_sql update statement .. 
}}}

foreach ($_POST['TIME_START'] as $TIME)  {
     foreach ($TIME as $TIME1) {
        foreach ($TIME1 as $TIME2) {
 ......my_sql update statement .. 
}}}

foreach ($_POST['TIME_END'] as $END)  {
      foreach ($END as $END1) {
	        foreach ($END1 as $END2) {        
 ......my_sql update statement .. 
}}}
?>
But the problem is when update the records, it was quite slow. i think that it was running the foreach() quite many time then cause it slow.
So any method can make it faster or compile the foreach() ?
thank to any comment.
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

what's the query you'r running?
valen53
Forum Contributor
Posts: 137
Joined: Tue Aug 27, 2002 9:29 am

Post by valen53 »

Thank reply.

Code: Select all

<?php
if (isset($_POST['status'])) {
 foreach ($_POST['status'] as $status) {
   foreach ($status as $status1) {
      foreach ($status1 as $status2) {
$poin = $poin + 1;
 $getTemp = mysql_query("Select * from TEMP_EMP2 where pointer = '$poin' and super_id = '$ecmHR'") ;
 $row = mysql_fetch_array($getTemp) ;
 $emp = $row[emp_id] ;  
 $apply = $row[apply_date];
 $RECORD = $row[REC];
 
 $update = mysql_query("update temp_ot set status='$status2', HOD = '$ecmHR' where EMP_ID='$emp' and APPLY_DATE = '$apply' and REC = '$RECORD'  ");

}}}

foreach ($_POST['TIME_START'] as $TIME)  {
      foreach ($TIME as $TIME1)                     {
	        foreach ($TIME1 as $TIME2)   {
        
 $pointer = $pointer + 1;
 $getTemp = mysql_query("Select * from TEMP_EMP2 where pointer = '$pointer' and super_id = '$ecmHR' ") ;
 $row = mysql_fetch_array($getTemp) ;
 $emp = $row[emp_id] ;  
 $apply = $row[apply_date];
  $RECORD = $row[REC];
 
 $update = mysql_query("update Temp_OT set TIME_START ='$TIME2'
  where EMP_ID='$emp' and APPLY_DATE = '$apply' and REC = '$RECORD'  ");

   }
  }
 }
foreach ($_POST['TIME_END'] as $END)  {
      foreach ($END as $END1)                    {
	        foreach ($END1 as $END2)  {
        
 $point = $point + 1;
 $getTemp = mysql_query("Select * from TEMP_EMP2 where pointer = '$point' and super_id = '$ecmHR' ") ;
 $row = mysql_fetch_array($getTemp) ;
 $emp = $row[emp_id] ;  
 $apply = $row[apply_date];
 $RECORD = $row[REC];

 $update = mysql_query("update Temp_OT set TIME_END ='$END2'
  where EMP_ID='$emp' and APPLY_DATE = '$apply' and REC = '$RECORD'  ");

  }
 }
} 

?>
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

status, time_start and start_end are three dimensional arrays?

Perhaps you could add the data you get from the select queries to your POST dataset (as hidden fields or something).. then you don't need the select queries.. should save you a lot of time...

Or do the select queries up front, and save them in an array.. instead of requering the db, you could look in the array for the values corresponding with $poin and $ecmHR?
valen53
Forum Contributor
Posts: 137
Joined: Tue Aug 27, 2002 9:29 am

Post by valen53 »

thank for reply. Below is my coding in edit page.
i use ['.$empid.']['.$row[APPLY_DATE].']['.$REC.'] as primary key. That why i need foreach() to get the data.

Code: Select all

<?php
$pointer = $pointer + 1; 
$insertTemp = mysql_query("insert into TEMP_EMP2(super_id,emp_id,pointer,apply_date,REC) values('$ecmHR','$row[EMP_ID]', '$pointer', '$row[APPLY_DATE]','$REC' )"); 
...
<td><div align="center"><font size="2"> 
<input name="TIME_START['.$empid.']['.$row[APPLY_DATE].']['.$REC.']" type="text" value="'.$row[TIME_START].'" size="4" maxlength="4"></font></div></td>
<td><div align="center"><font size="2"> 
<input name="TIME_END['.$empid.']['.$row[APPLY_DATE].']['.$REC.']" type="text" value="'.$row[TIME_END].'" size="4" maxlength="4"> </font></div></td>
<td><div align="center"> 
<select name="status['.$empid.']['.$row[APPLY_DATE].']['.$REC.']" class="cssLine1L4">
<option value="1" selected>Not Yet</option>
<option value="0">Approve</option>
... 
?>
actually i not very understand
Or do the select queries up front, and save them in an array.. instead of requering the db, you could look in the array for the values corresponding with $poin and $ecmHR?
can u show a example to me ?
Post Reply