Cron Job Help?

Discussion in 'PHP' started by ozone1, Jun 13, 2008.

  1. #1
    Hi guys i have a video site and yesterday i made a new field "todayview" in the table videos in my database to collect the "views" of the videos of current day and clear those views when the day finishes when i asked a coder he said i hve to use cron job to do this.

    so i made a cron file todaycron.php which has the following code not sure if its right im a noob in php

    <?php
    require('/home/*****/public_html/config.php');
    $sql = mysql_query("UPDATE videos SET todayview = 0");
    ?>
    
    Code (markup):
    then on cpanel i clicked cron job and added the command to run as this
    /usr/local/bin/php /home/****/public_html/main/todaycron.php
    Code (markup):
    and set the timing to 24 hours but the problem is it keeps clearing the views from the field "todayview" every 5-10 seconds i dont knw where i made mistake i think its something to do with the code can anyone help me?

    Thanks
     
    ozone1, Jun 13, 2008 IP
  2. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #2
     $today = date('Y-m-d');
    
     $query = "SELECT view_date FROM videos";
     $result = mysql_query($query);
     $row = mysql_fetch_row($result);
     
     $date = explode('-',$row['view_date']);
      
     if($today > date('Y-m-d',mktime(0,0,0,$date[1],$date[2],$date[0]))){ 
     
       //Oh it's a new day, reset number of views
       //update view_date to the current date
     
     }else{
     
       //Ooops! today is not over
       //update number of views
    }
    PHP:
     
    php-lover, Jun 13, 2008 IP
  3. ozone1

    ozone1 Member

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Hi thanks for the code but i dont know where to apply it this is what i have on my index.tpl page where do i put the code u gave me? im not good in php :(

    
    <?php
    	
    $result[0] = mysql_query("SELECT * FROM videos ORDER BY todayview
    DESC LIMIT 8");
    	
    	for($i=0;$i<count($result);$i++) {
    		echo '<td style="width:190px;font-size:12px;text-align:left;" valign="top">';
    		while($row=mysql_fetch_array($result[$i])) {
    			$id = $row['id'];
    			$cat = $row['cat'];
    			$title = $row['title'];
    			$numviews = $row['todayview'];
    	
    $max_length = 30;
    $title = ( strlen($title) > $max_length ? 
    substr($title,0,$max_length)."..." : $title );
    			
    
    echo '<li><a href="videos.php?id=' . $id . '" class="forceRight">'. $title . '<a class="numbers">' . $numviews . ' views</a></a></li>';
    }
    
    }
    ?>
    Code (markup):
     
    ozone1, Jun 13, 2008 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    You need to add a view_date filed with data type date to your videos table.
    I build this function for you. It's easy to update your views. :)




    <?php
    
    function update($id){
    
     $today = date('Y-m-d');
    
     $query = "SELECT todayview FROM videos where id=$id";
     $result = mysql_query($query);
     $rows = mysql_fetch_row($result);
     
     $date = explode('-',$rows['todayview']);
      
     if($today > date('Y-m-d',mktime(0,0,0,$date[1],$date[2],$date[0]))){ 
      
       //Oh it's a new day, reset number of views,reset date to the current date
       $query = "UPDATE videos SET todayview=1,view_date='$today' WHERE id=$id";
       mysql_query($query);
     
     }else{
       
       //Ooops! today is not over
       //update number of views
       
       
       
       $views = $rows['todayview'] + 1;
       
       //update views   
       $update_query = "UPDATE videos SET todayview=$views WHERE id=$id";
       mysql_query($update_query);
       
    }
    
    }//function end
    
    
    
    $result[0] = mysql_query("SELECT * FROM videos ORDER BY todayview
    DESC LIMIT 8");
    	
    	for($i=0;$i<count($result);$i++) {
    		echo '<td style="width:190px;font-size:12px;text-align:left;" valign="top">';
    		while($row=mysql_fetch_array($result[$i])) {
    			$id = $row['id'];
    			$cat = $row['cat'];
    			$title = $row['title'];
    			$numviews = $row['todayview'];
    	
    $max_length = 30;
    $title = ( strlen($title) > $max_length ? 
    substr($title,0,$max_length)."..." : $title );
    
    update($row['id']);	//<-----call function update to update view details		
    
    echo '<li><a href="videos.php?id=' . $id . '" class="forceRight">'. $title . '<a class="numbers">' . $numviews . ' views</a></a></li>';
    }
    
    }
    
    
    
    ?>
    PHP:
     
    php-lover, Jun 13, 2008 IP
  5. ozone1

    ozone1 Member

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Hi thanks a lot helping me in the view_date filed watelse do i need to put i mean

    Length/Values
    Collation
    Attributes
    Null
    Default2
    Extra
    Comments

    shall i leave it blank? or do i have to put anything? when i clicked browse on videos table the view_date is 0000-00-00 for all videos did i made any mistake?

    Thanks again for helping me
     
    ozone1, Jun 13, 2008 IP
  6. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Not a problem.

    view_date was not implement to display but we use to tell today or tommorow. It was not implement to display.

    Don't display that row['view_date']
     
    php-lover, Jun 13, 2008 IP
  7. ozone1

    ozone1 Member

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #7
    ok thanks a lot for spending time helping me really appreciate it :)
     
    ozone1, Jun 13, 2008 IP
  8. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #8
    You welcome !!
     
    php-lover, Jun 13, 2008 IP
  9. ozone1

    ozone1 Member

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #9
    Hi php-lover im having a problem whenever my index page is viewed it keeps on adding extra 1 view to the field "todayview" and its not clearing on the next day. Help me please thanks
     
    ozone1, Jun 13, 2008 IP
  10. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #10
    It's seems, you build the update code in a wrong file. That's why todayview is always increase when you open your index.php.

    The right file to put in your update code is videos.php

    Can you post your videos.php page code.



    <?php
    
    function update($id){
    
     $today = date('Y-m-d'); 
     $query = "SELECT * FROM videos where id=$id"; 
     $result = mysql_query($query); 
     $rows = mysql_fetch_array($result);  
     $date = explode('-',$rows['view_date']);
        
     if($today > date('Y-m-d',mktime(0,0,0,$date[1],$date[2],$date[0]))){      
        //Oh it's a new day, reset number of views,reset date to the current date   
        $query = "UPDATE videos SET todayview=1,view_date='$today' WHERE id=$id";   
        mysql_query($query);  
     
     }else{ 
          
        //Ooops! today is not over   //update number of views            
        $views = $rows['todayview'] + 1;      //update views      
        $update_query = "UPDATE videos SET todayview=$views WHERE id=$id";   
        mysql_query($update_query);   
        
        }
     
    }//function end
     ?>
    PHP:
     
    php-lover, Jun 14, 2008 IP
  11. ozone1

    ozone1 Member

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #11
    Hi the code you made yesterday i put it on my index.tpl page coz i want to show my visitors "todays most viewed videos". This is what i have on my videos.tpl page

    <?php
     
    $result = mysql_query("SELECT * FROM vids WHERE video_id='".$videos."' ORDER BY id");
    mysql_query("UPDATE videos SET view = view+1 WHERE id = '$id'");
    mysql_query("UPDATE videos SET todayview = todayview+1 WHERE id = '$id'");
    while($row = mysql_fetch_array($result))
    
      {
      $id = $row['id'];
      $title = $row['title'];
      ?>
    Code (markup):
    My normal views are working fine only problem is "todayview" keeps on adding extra 1 view whenever the index page is viewed
     
    ozone1, Jun 14, 2008 IP
  12. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #12
    Yes we need to take off the function update from the index.php and put in the videos.php like this.

    try this and let me know.



    
    function update($id){
    
     $today = date('Y-m-d'); 
     $query = "SELECT * FROM videos where id=$id"; 
     $result = mysql_query($query); 
     $rows = mysql_fetch_array($result);  
     $date = explode('-',$rows['view_date']);
        
     if($today > date('Y-m-d',mktime(0,0,0,$date[1],$date[2],$date[0]))){      
        //Oh it's a new day, reset number of views,reset date to the current date   
        $query = "UPDATE videos SET todayview=1,view_date='$today' WHERE id=$id";   
        mysql_query($query);  
     
     }else{ 
          
        //Ooops! today is not over   //update number of views            
        $views = $rows['todayview'] + 1;      //update views      
       $update_query = "UPDATE videos SET todayview=$views WHERE id=$id";   
        mysql_query($update_query);   
        
        }
     
    }//function end
    
    
    $id = $_GET['id'];
    
    update($id);
    PHP:
     
    php-lover, Jun 14, 2008 IP
  13. ozone1

    ozone1 Member

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #13
    ya i took the function from index page but its giving me this error on videos page

    
    Fatal error: Cannot redeclare update() (previously declared in /home/*****/public_html/main/videos.tpl:171) in /home/*****/public_html/main/videos.tpl on line 171
    Code (markup):
     
    ozone1, Jun 14, 2008 IP
  14. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #14
    this error means, there is a copy of function update() in another file apart from videos.php

    make sure the only copy of function update() is in videos.php
     
    php-lover, Jun 14, 2008 IP
  15. ozone1

    ozone1 Member

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #15
    Fixed its working thanks :)
     
    ozone1, Jun 14, 2008 IP
  16. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #16
    yep well done :)
     
    php-lover, Jun 14, 2008 IP