myladeybugg
myladeybugg

Reputation: 329

Auto update prices in database, mysql

I am currently getting products from one site, storing them in a database, and then having their prices display on another site. I am trying to get the prices from the one site to update daily in my database so the new updated prices can be displayed onto my other site.

Right now I am getting the products using an item number but have to manually go in and update any prices that have changed.

I am guessing I am going to have to use some kind of cronjob but not sure how to do this. I have no experience with cronjobs and am a noob with php.

Any ideas? Thanks!


I have done some reading on the foreach loop and have written some code. But my foreach loop is only running once for the first item number. The foreach loop runs then goes to the "api.php" page but then stops. It doesn't continually loop for each item number. How do I tell it to go through all of item numbers in my database?

Also if you see anything else wrong in my code please let me know. Thanks

 ....


$itemnumber = array("".$result['item_number']."");

foreach ($itemnumber as $item_number) {

echo "<form method=\"post\" action=\"api.php\" name=\"ChangeSubmit\" id=\"ChangeSubmit\">";
echo "<input type=\"text\" name=\"item_number\" value=\"{$item_number}\" />";

echo "<script type=\"text/javascript\">
function myfunc () {
var frm = document.getElementById(\"ChangeSubmit\");
frm.submit();
}
window.onload = myfunc;
</script></form>";



}


}

Upvotes: 1

Views: 741

Answers (2)

Kaivosukeltaja
Kaivosukeltaja

Reputation: 15735

If you already retrieve the product data from an external site and store it in a local database, updating the prices from the same source should be no problem to you. Just retrieve the data, iterate through it in a foreach loop or similar and update the prices to the database based on the item number.

Once you have created the update script and run it manually, adding it as a cronjob will be as simple as running the command `crontab -e´ and adding this row to execute your script every midnight:

0 0 * * * /usr/local/bin/php /path/to/your/script.php

Don't forget to use the correct path for PHP for your system, running which php in the shell will tell you the path.

Upvotes: 1

DanRedux
DanRedux

Reputation: 9359

If you have cronjob's on your server, it'll be very apparent- You make a PHP script that updates it, and throw it in a daily cronjob.

However, I do it this way:

Method 1: At the beginning of every page request, check the last "update" time (you choose how to store it). If it's been more than a day, do the update and set the "update" time to the current time.

This way, every time someone loads a page and it's been a day since the last update, it updates for them. However, this means it's slower for random users, once a day. If this isn't acceptable, there's a little change:

Method 2: If you need to update (via the above method of checking), start an asyncronous request for the data, handle the rest of the page, flush it to the user, then in a while loop wait until the request finishes and update it.

The downside to method 2 is that the user won't see the updated values, but, the benefit is that it won't be any more of a wait for them.

Upvotes: 0

Related Questions