473,383 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to create function to avoid repetitive code?

I have 3 files I need to write to - and need to make sure I have a lock on
each, and take action if I can't get a lock. The code below works, but I
know it could be more efficient if I used a function instead of the
repetitive code. Still kind of green in php.... Any help in creating a
function to eliminate the repeated file lock tests is appreciated. Thanks
in advance!

//***** write to visdata *****
$got_it = false;
$timeout = 5;
$new_visitor =
$visdate."|".
$visip."|".
$visos."|".
$visbrow."|".
$visisp;
$fp = fopen($visdata,"a");
/////******* CODE BELOW IS REPEATED**********
while (($got_it === false) && ($timeout != 0))
{
if (flock($fp, LOCK_EX))
{
fwrite($fp, $new_visitor."\n");
flock($fp, LOCK_UN);
$got_it = true;
}
else
{
usleep(100000);
--$timeout;
}
}
//echo "<br>got_it = ".$got_it;
//if cd not get lock after 5 tries (.5 sec.) skip and record failure
if (!$got_it)
{
$fp=fopen('/home/clearpoi/public_html/viscount_files/viscount_fail', 'a');
fwrite($fp,date("M d Y h:i a",time() + $timeadjust)."\n");
exit;
}
fclose($fp);
//***** write to viscounter *****
$got_it = false;
$timeout = 5;
$fp = fopen($viscounter,"a");
//*********REPEATED CODE**********
while (($got_it === false) && ($timeout != 0))
{
if (flock($fp, LOCK_EX))
{
fwrite($fp, time()."\n");
flock($fp, LOCK_UN);
$got_it = true;
}
else
{
usleep(100000);
--$timeout;
}
}
//$got_it = 0;
//echo "<br>got_it = ".$got_it;
//if cd not get lock after 5 tries (.5 sec.) skip and record failure
if (!$got_it)
{
$fp=fopen('/home/clearpoi/public_html/viscount_files/viscount_fail', 'a');
fwrite($fp,date("M d Y h:i a",time() + $timeadjust)."\n");
exit;
}
fclose($fp);
//get count for visdisplay
$avs = file($viscounter);
$v24h = 0;
$v30d = 0;
$v365d = 0;
foreach ($avs as $val)
{
if($val>(time()-(3600*24)))
{
++$v24h;
}
if($val>(time()-(3600*24*30)))
{
++$v30d;
}
if($val>(time()-(3600*24*365)))
{
++$v365d;
}
}
echo "<br>v24h = ".$v24h;
echo "<br>v30d = ".$v30d;
echo "<br>v365d = ".$v365d;
$vtotals = $v24h."|".$v30d."|".$v365d;
//***** write to visdisplay *****
$got_it = false;
$timeout = 5;
$fp=fopen($visdisplay, 'w');
//*********REPEATED CODE********
while (($got_it === false) && ($timeout != 0))
{
if (flock($fp, LOCK_EX))
{
fwrite($fp, $vtotals);
flock($fp, LOCK_UN);
$got_it = true;
}
else
{
usleep(100000);
--$timeout;
}
}
//echo "<br>got_it = ".$got_it;
//if cd not get lock after 5 tries (.5 sec.) skip and record failure
if (!$got_it)
{
$fp=fopen('/home/clearpoi/public_html/viscount_files/viscount_fail', 'a');
fwrite($fp,date("M d Y h:i a",time() + $timeadjust)."\n");
exit;
}
fclose($fp);

Jul 17 '05 #1
1 2725
Look on the web for "php functions" e.g.
http://builder.com.com/5100-6371-1045063.html

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-create-f...ict129634.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=432527
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: nobody | last post by:
hello everybody, is there a way of creating an array with help of a function that would accept the name of this array as a parameter and then create global Array type variable of that name? so...
6
by: john sun | last post by:
Hello, I am not newbie C++ developer infact :). But till recently I would like dig up more about C++. I know those OO details. But when people talking OO they focused on the polymorphism, and...
44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
3
by: Larry Lard | last post by:
Until Generics arrives, the only way to do things like: create a strongly-typed ArrayList-like object; create a strongly-typed HashTable with given types for key and value; and so on; is to churn...
4
by: Captain Dondo | last post by:
I am building repetitive forms using PHP and templates; the PHP loops through the data and stuffs it into the templates. Javascript functions then handle each entry field as required.... I am...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
2
by: wachanayo | last post by:
Hi, i'm currently developing a program in ASP.NET which involve binding data from my database to a dropdownlist control. In this case, i have bind a column of a table to a dropdownlist control so all...
7
by: bprocopio | last post by:
Please help. I'm stumped. I need to create a dynamic variable in a procedure that will be used to update a variable of the same name in a table. i.e. the name in tblAnalysisScores are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.