How to create function to avoid repetitive code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    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($viscount er,"a");
    //*********REPEAT ED 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($viscounte r);
    $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($visd isplay, 'w');
    //*********REPEAT ED 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);



  • steve

    #2
    Re: How to create function to avoid repetitive code?

    Look on the web for "php functions" e.g.


    --
    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

    Comment

    Working...