Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 56
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation how do i create a CRON job which deleted my entire data table and inserts new data?

    Hi all,

    I'veherd a little about CRON jobs but dont really know anything about them apart fom that i can use them to periodically update things?

    i have a table which holds data from several rss feeds and i want the data updated every hour but im not sure how i could do this

    here is the code i currently have to insert the data into the table, but it keeps inserting new data so i need to clear the table first?
    PHP Code:
    <?php
        $user_name 
    "";
        
    $password "";
        
    $database "";
        
    $server "";

           
    $db_handle mysql_connect($server$user_name$password);
        
    $db_found mysql_select_db($database$db_handle);
        
        
    $feeds = array(...);
        
        
    $itemno 1;
        
    $itemtype 0;
        
        foreach (
    $feeds as $feed){
        
    $count 2;

          
    $feedreader simplexml_load_file
        
    ("$feed");
        foreach (
    $feedreader->channel->item as $item) {
        
    $title addslashes ($item->title);
        
    $photosource get_string_between($item->description'<img src="','"');
        if(
    $count >= 0){
        
        if(
    $itemno<=45){
            
    $itemtype "Books";
        }
        if(
    $itemno<=36){
            
    $itemtype "Blu-Ray";
        }
        if(
    $itemno<=27){
            
    $itemtype "Video Games";
        }
        if(
    $itemno<=18){
            
    $itemtype "CD";
        }
        if(
    $itemno<=9){
            
    $itemtype "DVD";
        }

        
      if
        (
    $itemno >=&& $itemno <=|| $itemno >=10 && $itemno <=12 || 
        
    $itemno >=19 && $itemno <=21 || $itemno >=28 && $itemno <=30 || 
        
    $itemno >=37 && $itemno <=39){
        
    $subcat "Bestsellers";
        }
      elseif
        (
    $itemno >=&& $itemno <=|| $itemno >=13 && $itemno <=15 || 
        
    $itemno >=22 && $itemno <=24 || $itemno >=31 && $itemno <=33 || 
        
    $itemno >=40 && $itemno <=42){
        
    $subcat "New Releases";
        }
      elseif
          (
    $itemno >=&& $itemno <=|| $itemno >=16 && $itemno <=18 || 
          
    $itemno >=25 && $itemno <=27 || $itemno >=34 && $itemno <=36 || 
        
    $itemno >=43 && $itemno <=45){
        
    $subcat "Gifted";
        }
        
        
        
    $insert "INSERT INTO category_data (page_category, cat_id, subcat_id, title, image, other) VALUES ('Entertainment','$itemtype', '$subcat', '$title', '$photosource', 'other')";
        
    $sql mysql_query($insert);
        
    $count --;
        
    $itemno ++;
        }
        }
        }
        
    ?>
    but thats all i know...

    the rest is in your hands

    cheers
    Luke
    Last edited by LJackson; 05-07-2009 at 02:25 PM.

  • #2
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    I would actually like to know about this additionally...

    *lurks*



    Sorry I don't have any info, except that the code you currently have looks good.

    Consider this a bump

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    As long as the user for your mysql database has the permission to, you can use TRUNCATE
    Before this
    PHP Code:
        $insert "INSERT INTO category_data (page_category, cat_id, subcat_id, title, image, other) VALUES ('Entertainment','$itemtype', '$subcat', '$title', '$photosource', 'other')";
        
    $sql mysql_query($insert); 
    add this
    PHP Code:
    mysql_query("TRUNCATE TABLE `category_data`") or die(mysql_error());
    $insert "INSERT INTO category_data (page_category, cat_id, subcat_id, title, image, other) VALUES ('Entertainment','$itemtype', '$subcat', '$title', '$photosource', 'other')";
        
    $sql mysql_query($insert); 
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    LJackson (05-07-2009)

  • #4
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    If you have a control panel, you can easily setup cron jobs there. Just set the path to the file, and type in when to update it.

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Cron jobs can be run on apache easily as stated through CPanel or other similar control panels. If you are on a windows server you need to use the task scheduler. And finally if you can't do either of those then you can use what is called an "lazy man's" cron job which is essentially including that file into pages on your site that get the most traffic. You'll basically have the user run the file without then really knowing unless you are inserting a lot of data in which case page load might increase.
    Last edited by _Aerospace_Eng_; 05-06-2009 at 02:29 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by nobackseat88 View Post
    I would actually like to know about this additionally...

    *lurks*



    Sorry I don't have any info, except that the code you currently have looks good.

    Consider this a bump
    You Lurker You
    and thanks...

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    As long as the user for your mysql database has the permission to, you can use TRUNCATE
    Before this
    PHP Code:
        $insert "INSERT INTO category_data (page_category, cat_id, subcat_id, title, image, other) VALUES ('Entertainment','$itemtype', '$subcat', '$title', '$photosource', 'other')";
        
    $sql mysql_query($insert); 
    add this
    PHP Code:
    mysql_query("TRUNCATE TABLE `category_data`") or die(mysql_error());
    $insert "INSERT INTO category_data (page_category, cat_id, subcat_id, title, image, other) VALUES ('Entertainment','$itemtype', '$subcat', '$title', '$photosource', 'other')";
        
    $sql mysql_query($insert); 
    Hi Aerospace, thanks for this yeah i have allowed access to my database so this wont be a problem

    Can You please elaborate on the Setup of a CRON Job,

    sorry this is all new to me and im not sure i follow you
    when you are talking about the server is that my hosts server? how do i tell if its windows or apache?

    sorry for all the questions
    many thanks
    Luke

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by masterofollies View Post
    If you have a control panel, you can easily setup cron jobs there. Just set the path to the file, and type in when to update it.
    Hi masterofollies,

    i assume when talking about control panel you mean when i sign in to my host and access their control panel?

    my host is one.com i will have a look on their control pannel to see if there is an option

    thanks
    Luke

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    i have just checked out my hosts control panel and i cannot see anywhere with the option to create a cron job

    i assume you mean my own pcs control panel as i have just found task scheduler

    so do i simply save that file (i want to run) to my webspace? or on my machine? and then set up a new task which will run that script every hour?

    thanks
    Luke
    Last edited by LJackson; 05-06-2009 at 02:43 PM.

  • #10
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Cron jobs can only be run on apache servers which is how you will know. A scheduled task similar to a cron job runs on windows servers. I suggest you contact your host. As to what to do with the file you put it on your webhost. How do you expect it to even work if you run it on your own computer?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #11
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    lol ok so....

    i need to find out which sort of server my host is running to determin which route i take???

    if its a widows host then i can simple set up a new task on my computer which will run my file stored on my hosts server???

    if its an apache server im still unsure what to do sorry!!!!

    but i will contact my host and see what type of server it is first then if need be we can cross the apache bridge if needed

    thanks
    Luke

  • #12
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok just contacted my host and they said CRON is not supported
    so i assume its a windows based server.

    i also asked if i could use the task sheduler to perform the task and was told "well if it does not require cron then it is fine"

  • #13
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Okay so cron is out of the question. Put the file on your server somewhere. Since it probably doesn't matter who runs the file it is probably okay to put it in the public directory. Read this article

    http://www.hiveminds.co.uk/?p=35223
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    LJackson (05-07-2009)

  • #14
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok thanks i have copied the file to my host and can be located at www.kernow-connect.com/dbupdater.php

    i will read that link you provided to try and set up a task sheduler

    i may be back for some more assistance soon :P

    cheers for all your help so far
    Luke

  • #15
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok

    i currently have
    Code:
    Call LogEntry()
    
    Sub LogEntry()
    
    'Force the script to finish on an error.
    On Error Resume Next
    
    'Declare variables
    Dim objRequest
    Dim URL
    
    Set objRequest = CreateObject("Microsoft.XMLHTTP")
    
    'Put together the URL link appending the Variables.
    URL = "http://www.kernow-connect.com/dbupdater.php"
    
    'Open the HTTP request and pass the URL to the objRequest object
    objRequest.open "GET", URL , false
    
    'Send the HTML Request
    objRequest.Send
    
    'Set the object to nothing
    Set objRequest = Nothing
    
    End Sub
    saved as updater_script.vbs

    and my php code
    PHP Code:
    <?php
        $user_name 
    "";
        
    $password "";
        
    $database "";
        
    $server "";

           
    $db_handle mysql_connect($server$user_name$password);
        
    $db_found mysql_select_db($database$db_handle);
        
        function 
    get_string_between($string$start$end){
            
    $string " ".$string;
            
    $ini strpos($string,$start);
            if (
    $ini == 0) return "";
            
    $ini += strlen($start);   
            
    $len strpos($string,$end,$ini) - $ini;
            return 
    substr($string,$ini,$len);
        }    
        
        
    $feeds = array(
    "http://www.amazon.co.uk/rss/bestsellers/dvd/ref=pd_nr_rss_link",                                                "http://www.amazon.co.uk/rss/new-releases/dvd/ref=pd_nr_rss_link","http://www.amazon.co.uk/rss/most-gifted/dvd/ref=pd_mg_rss_link""http://www.amazon.co.uk/rss/bestsellers/music/ref=pd_ts_rss_link""http://www.amazon.co.uk/rss/new-releases/music/ref=pd_nr_rss_link""http://www.amazon.co.uk/rss/most-gifted/music/ref=pd_mg_rss_link""http://www.amazon.co.uk/rss/bestsellers/videogames/ref=pd_ts_rss_link""http://www.amazon.co.uk/rss/new-releases/videogames/ref=pd_nr_rss_link""http://www.amazon.co.uk/rss/most-gifted/videogames/ref=pd_mg_rss_link",
    "http://www.amazon.co.uk/rss/bestsellers/dvd/383380011/ref=pd_ts_rss_link",
    "http://www.amazon.co.uk/rss/new-releases/dvd/383380011/ref=pd_nr_rss_link",
    "http://www.amazon.co.uk/rss/most-gifted/dvd/383380011/ref=pd_mg_rss_link",
    "http://www.amazon.co.uk/rss/bestsellers/books/ref=pd_ts_rss_link",
    "http://www.amazon.co.uk/rss/new-releases/books/ref=pd_nr_rss_link",
    "http://www.amazon.co.uk/rss/most-gifted/books/ref=pd_mw_rss_link"
        
    );
        
        
    $itemno 1;
        
    $itemtype 0;
        
        foreach (
    $feeds as $feed){
        
    $count 2;

          
    $feedreader simplexml_load_file
        
    ("$feed");
        foreach (
    $feedreader->channel->item as $item) {
        
    $title addslashes ($item->title);
        
    $photosource get_string_between($item->description'<img src="','"');
        if(
    $count >= 0){
        
        if(
    $itemno<=45){
            
    $itemtype "Books";
        }
        if(
    $itemno<=36){
            
    $itemtype "Blu-Ray";
        }
        if(
    $itemno<=27){
            
    $itemtype "Video Games";
        }
        if(
    $itemno<=18){
            
    $itemtype "CD";
        }
        if(
    $itemno<=9){
            
    $itemtype "DVD";
        }

        
      if
        (
    $itemno >=&& $itemno <=|| $itemno >=10 && $itemno <=12 || 
        
    $itemno >=19 && $itemno <=21 || $itemno >=28 && $itemno <=30 || 
        
    $itemno >=37 && $itemno <=39){
        
    $subcat "Bestsellers";
        }
      elseif
        (
    $itemno >=&& $itemno <=|| $itemno >=13 && $itemno <=15 || 
        
    $itemno >=22 && $itemno <=24 || $itemno >=31 && $itemno <=33 || 
        
    $itemno >=40 && $itemno <=42){
        
    $subcat "New Releases";
        }
      elseif
          (
    $itemno >=&& $itemno <=|| $itemno >=16 && $itemno <=18 || 
          
    $itemno >=25 && $itemno <=27 || $itemno >=34 && $itemno <=36 || 
        
    $itemno >=43 && $itemno <=45){
        
    $subcat "Gifted";
        }
        print 
    "$itemno equals $itemtype subcat equals $subcat";
        print 
    "<br />";
        
        
        
    mysql_query("TRUNCATE TABLE `category_data`") or die(mysql_error());
    $insert "INSERT INTO category_data (page_category, cat_id, subcat_id, title, image, other) VALUES ('Entertainment','$itemtype', '$subcat', '$title', '$photosource', 'other')";
        
    $sql mysql_query($insert);
        
        
    $count --;
        
    $itemno ++;
        }
        }
        }
        
    ?>
    so is that all ok, i just need to set up the task sheduler now which is explained in the link you provided

    cheers


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •