/    Sign up×
Community /Pin to ProfileBookmark

my script take long time to perform!!! :(

hi all
am a new member in this forum
i would like to participate here with my first question

i have this code :

[code=php]

<?
if($_GET[‘do’] == ‘show’)
{
include “connect.php”;
$total=$_REQUEST[‘total’];
$break_1=$_REQUEST[‘break’];
$day=$_REQUEST[‘day’] ;
$break_time=$break_1*60;
$start=$_REQUEST[‘start’];
$leave=$_REQUEST[‘leave’];
$gap=$_REQUEST[‘gap’];
$gap=$gap*60;
$tablename= $_REQUEST[‘table’];
list($hour_out1,$min_out1,$sec_out1)=split(‘:’,$leave);
list($hour_in1,$min_in1,$sec_in1)=split(‘:’,$start);
$start=mktime($hour_in1,$min_in1,$sec_in1)-mktime(0,0,0);
$leave=mktime($hour_out1,$min_out1,$sec_out1)-mktime(0,0,0);
$year= $_REQUEST[‘year’];
$month= $_REQUEST[‘month’];
$total_standard_workin_time=$leave-$start; //8:45 min in seconds =31500 seconds
$data = mysql_query(“SELECT * FROM $tablename”) or die(mysql_error().”table name”);
echo” <div style=’position: absolute; width: 100px; height: 100px; z-index: 1; left: 650px; top: 170px’ id=’layer1′>
<form method=’POST’ action=’?do=report’> “;
//echo “<input type=’submit’ class=’button’ value=’Save…’ name=’save’>
echo” <input type=’submit’ class=’button’ value=’Report…’ name=’report’>”;

echo “<p align=’center’>”;
echo “</form></div>
<div style=’position: absolute; width: 100px; height: 100px; z-index: 1; left: 450px; top: 170px’ id=’layer1′><form method=’POST’ action=’first_page.php’>
<p><input type=’submit’ class=’button’ value=’Change Calulation’ name=’B1′> “;

echo”</p> </form> </div> “;

echo” <div style=’position: absolute; width: 100px; height: 100px; z-index: 1; left: 250px; top: 170px’ ><form method=’POST’ action=’search.php’>
<p><input type=’submit’ class=’button’ value=’Query Employee…’ name=’B1′> “;

echo”</form> “;

echo”</div>”;
Print “<br><br><br><br><div align=’center’><table border cellpadding=3>”;
echo “<th>Date</th><th>Name</th><th>ID</th><th>Checked In</th><th>Checked Out</th><th>Total Worked Hrs</th><th>Over Time</th><th>Minus Time</th>”;
while($info = mysql_fetch_array( $data ))
{
list($hour_in,$min_in,$sec_in)=split(‘:’,$info[‘timein’]);
list($hour_out,$min_out,$sec_out)=split(‘:’,$info[‘timeout’]);
list($hour1,$min1,$sec1)=split(‘:’,$total);
if ($min_in<=30 && $hour_in==07 )
{ $min_in= 30;
$sec_in=0;
}
$time_in = mktime($hour_in, $min_in, $sec_in)-mktime(0, 0, 0);
$time_out= mktime($hour_out, $min_out, $sec_out)-mktime(0, 0, 0);
$total_hours_int= $time_out – $time_in ;
$total_hours_int1= $time_out – $time_in-7200 ; //ÚÏÏ ÓÇÚÇÊ ÔÛá áãæÙÝ
$total_hours_time=date(“H:i:s”,$total_hours_int1);
if ($time_in+$gap>=$time_out)
{
$total_hours_time=date(“00:00:00”);
$total_hours_int= 0;
$over_time_int=0;
$over_time= date(“00:00:00”);
$minus_time=0;
}
elseif ($time_out==’00:00:00′ )
{
$total_hours_int= 0;
$total_hours_time=date(“00:00:00”);
$total_hours_int= 0;
$over_time_int=0;
$over_time= date(“00:00:00”);
$minus_time_int=0;
$minus_time=’00:00:00′;
}

//////////////////OVER TIME//////////////////////////////
if($total_hours_int>$total_standard_workin_time){
$time_in = mktime($hour_in, $min_in, $sec_in)-mktime(0,0,0);
$time_out= mktime($hour_out, $min_out, $sec_out)-mktime(0,0,0);
$total_hours_int= $time_out – $time_in-7200;
$over_time_int=$total_hours_int-$total_standard_workin_time;
$over_time= date(“H:i:s”,$over_time_int);
$minus_time=date(“00:00:00”,$minus_time);

}
else {

////////////MINUS TIME///////////////////
if($total_hours_int<$total_standard_workin_time){
$time_in= mktime($hour_in, $min_in, $sec_in)-mktime(0,0,0);
$time_out= mktime($hour_out-2, $min_out, $sec_out)-mktime(0,0,0);
$total_hours_int= $time_out – $time_in+$break_time ;
$over_time=’00:00:00′;
$minus_time_int=$total_standard_workin_time-$total_hours_int-4500;

if($minus_time_int<0)
{$minus_time_int=abs($minus_time_int);
$over_time= date(“H:i:s”,$over_time-7200);
$minus_time_int=0;
}
$minus_time= date(“H:i:s”,($minus_time_int-7200));
}
}

Print “<tr>”;
Print ” <td>”.$info[‘date’] . “</td> “;
Print “<td>”.$info[‘name’] . ” </td> “;
Print ” <td>”.$info[‘id’] . “</td> “;
Print “<td>”.$info[‘timein’] . ” </td> “;
Print ” <td>”.$info[‘timeout’] . “</td> “;
$time_out= $time_out+7200;

if($time_out==0 || $time_in>$time_out || $time_in+$gap>=$time_out){

echo “<form method=’post’ action=’?do=edit&date=” . $info[‘date’] . “&name=” . $info[‘name’] . “&table=”.$tablename.”‘>”;
//Print “<td><a href=’?do=edit&date=” . $info[‘date’] . “&name=” . $info[‘name’] . “&table=”.$tablename.”‘><b>Edit</b> “;
echo “</form>”;

echo “<form method=’post’ action=’?do=delete&date=” . $info[‘date’] . “&name=” . $info[‘name’] . “&table=”.$tablename.”‘>”;
//Print “<td><a href=’?do=edit&date=” . $info[‘date’] . “&name=” . $info[‘name’] . “&table=”.$tablename.”‘><b>Edit</b> “;
echo”
</form>”;
$minus_time=date(“00:00:00”,$minus_time);
$over_time= date(“00:00:00″,$over_time);
}
else
Print ” <td>”.$total_hours_time. “</td> “;
Print ” <td>”.$over_time. “</td> “;
Print ” <td>”.$minus_time. “</td> “;

$names[‘0′] = $minus_time;
//print_r($names) ;

echo ” <form action=’?do=save’ method=’post’ >
<input type=’hidden’ name=’over’ value=’$over_time’>”;
echo “<input type=’hidden’ name=’minus’ value=’$minus_time’></form>”;
$sql =”update $tablename set overtime = ‘”.$over_time. “‘ where date='”.$info[‘date’].”‘ and name='”.addslashes($info[‘name’]).”‘ “;
mysql_query($sql) or die(“sql=$sql<br>sql error = “.mysql_error());
$sql2 =”update $tablename set minustime = ‘$minus_time’ where date='”.$info[‘date’].”‘ and name='”.addslashes($info[‘name’]).”‘”;
mysql_query($sql2) or die(“sql2=$sq2l<br>sql error = “.mysql_error());

echo “</tr>”;

}
Print “</table></div>”;

}
?>

[/code]

when i run this program it takes more than 5 minutes to execute,i think its because of the these lines..

[code=php]

$sql =”update $tablename set overtime = ‘”.$over_time. “‘ where date='”.$info[‘date’].”‘ and name='”.addslashes($info[‘name’]).”‘ “;
mysql_query($sql) or die(“sql=$sql<br>sql error = “.mysql_error());
$sql2 =”update $tablename set minustime = ‘$minus_time’ where date='”.$info[‘date’].”‘ and name='”.addslashes($info[‘name’]).”‘”;
mysql_query($sql2) or die(“sql2=$sq2l<br>sql error = “.mysql_error());

[/code]

it updates the database every time u open the page.
this takes along time
is there anyway to sole this issue?
or maybe i can save the values of $over_time and $minus_time in array then pass them to another page to make it faster is this will work and how??
thnx!!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderJun 18.2007 — [code=php]
$sql ="update $tablename set overtime = '".$over_time. "', minustime = '$minus_time' where date='".$info['date']."' and name='".addslashes($info['name'])."' ";
mysql_query($sql) or die("sql=$sql<br>sql error = ".mysql_error());
[/code]


Use one query for a start.
Copy linkTweet thisAlerts:
@avaJun 18.2007 — Well, I don't know how big your tables are, but you're basically doing an update query for every result row of your initial query. Meaning, if you have a main query that returns 20000 results, you're doing 20000 update queries, each of which will need to look at every row in the table. That's definitely a problem.

I don't know what the purpose is of this, but why not use a new table with fields 'date', 'name', 'overtime' and 'minustime' where you can keep track of these things. At least it will make each updatequery faster.

If you really want a good performance, you want to do only one update query on the entire page.
Copy linkTweet thisAlerts:
@elbesauthorJun 18.2007 — thnx 4 ur answer,well i am donig a time attendance system..i am reading the data from a text file,i dont know if anyone can help me do this system..

i am reading data from a text file then i insert it into the database to do the calculatoins on time.i will explain more if anyone can help..
×

Success!

Help @elbes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...