From a normal distribution with all the files and folders where they are supposed to be, the below little perl script will create a daily random product for free in the "Specials" block.
Use it to modify your own program.
====================================
#!/usr/bin/perl
#### Load modules.
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use LWP::UserAgent;
my $query = new CGI;
my $cgi = $query->url();
$now=time();
@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June','July','August','September','October','November','December');
($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime($now);
$mon=$mon+1;
$mon = "0$mon" if ($mon < 10);
$day = "0$day" if ($day < 10);
$year = 1900 + $year;
$date = "$year-$mon-$day";
##################################################
# changing the daily random product and make its price $0.00 #########
##################################################
$host="localhost"; ## The server name of the MySQL database
$user="********"; ## The username for the Database
$pass="********"; ## The Password to the Database
$database="******"; ## The name of the Database
############## checking if the daily special needs to be changed ###########################
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth=$dbh->prepare('SELECT * FROM `va_special`;');
$sth->execute();while(my $ref = $sth->fetchrow_hashref()){
$special_date=$ref->{'special_date'};
$item_price=$ref->{'item_price'};
}$sth->finish;$dbh->disconnect;
if("$date" eq "$special_date"){
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth=$dbh->prepare('SELECT * FROM `va_items` WHERE `is_special_offer`="1";');
$sth->execute();while(my $ref = $sth->fetchrow_hashref()){
$item_name=$ref->{'item_name'};
}$sth->finish;$dbh->disconnect;
print "Location: /images/check.gif\n\n";
exit();}
############## checking if the daily special needs to be changed ###########################
###### no need to change? then program has stopped on the exit(); command above. ###########
###### there is a change needed so program will continue with the instructions below. #####
############## setting everything back to its default settings ###########################
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth=$dbh->prepare('UPDATE `va_items` SET `price`="'."$item_price".'" WHERE `is_offer_special`="1";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="0" WHERE `is_special_offer`="1";');
$sth->execute();$sth->finish;
############## below products are always special and free ##################################
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="1" WHERE `item_id`="1";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="1" WHERE `item_id`="2";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="1" WHERE `item_id`="10385";');
$sth->execute();$sth->finish;
############## above products are always special and free ##################################
############## now we are getting the total amount of items in the database ##############
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth = $dbh->prepare('SELECT COUNT(*) FROM `va_items`;');
$sth->execute();( $total_items ) = $sth->fetchrow_array();
$sth->finish;$dbh->disconnect;
############## now we generate a random number equal to an item number in the database ###
$new_item=int(rand($total_items));
############## get the original price of the new daily special ###########################
$sth=$dbh->prepare('SELECT * FROM `va_items` WHERE `item_id="'."$new_item".'";');
$sth->execute();while(my $ref = $sth->fetchrow_hashref()){
$price=$ref->{'price'};
}$sth->execute();$sth->finish;
############## now we update the data in the database for the daily special ##############
$sth=$dbh->prepare('UPDATE `va_special` SET `special_date`="'."$date".'";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_special` SET `item_price`="'."$price".'";');
$sth->execute();$sth->finish;
############## now we are setting this new item on special and for $0.00 #################
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="1" WHERE `item_id`="'."$new_item".'";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_items` SET `price`="0.00" WHERE `item_id`="'."$new_item".'";');
$sth->execute();$sth->finish;
The first thing I did was to make a new database table in the Shop Database and I called this table "va_special".
This table has 2 field records, one for the date and one for the original product price.
I used phpMyAdmin for this.
-------------------------------------------------------
#!/usr/bin/perl
#### Load modules.
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use LWP::UserAgent;
my $query = new CGI;
my $cgi = $query->url();
$now=time();
@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June','July','August','September','October','November','December');
($sec,$min, $hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime($now);
$mon=$mon+1;
$mon = "0$mon" if ($mon < 10);
$day = "0$day" if ($day < 10);
$year = 1900 + $year;
$date = "$year-$mon-$day";
-------------------------------------------------------------------
The first 8 lines are basically perl coding to set the sceen for the program.
Line 9 gets the system date string from the computer clock of the server.
The following 8 lines are used to give me the date variable as used by Viart. e.g. "2009-03-27"
-------------------------------------------------------------------
$host="localhost"; ## The server name of the MySQL database
$user="********"; ## The username for the Database
$pass="********"; ## The Password to the Database
$database="******"; ## The name of the Database
-------------------------------------------------------------------
The above lines will allow the perl interpreter to access the MySQL database.
-----------------------------------------------------------------
############## checking if the daily special needs to be changed ###########################
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth=$dbh->prepare('SELECT * FROM `va_special`;');
$sth->execute();while(my $ref = $sth->fetchrow_hashref()){
$special_date=$ref->{'special_date'};
$item_price=$ref->{'item_price'};
}$sth->finish;$dbh->disconnect;
if("$date" eq "$special_date"){
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth=$dbh->prepare('SELECT * FROM `va_items` WHERE `is_special_offer`="1";');
$sth->execute();while(my $ref = $sth->fetchrow_hashref()){
$item_name=$ref->{'item_name'};
}$sth->finish;$dbh->disconnect;
print "Location: /images/check.gif\n\n";
exit();}
############## checking if the daily special needs to be changed ###########################
-------------------------------------------------------------------
The above block of code does 2 things.
First it will retrieve the date variable set in the "va_special" database table.
The second part checks this date variable agains the current system clock date.
If they are the same, then do nothing. Print an image on the screen (/images/check.gif) and finish the program.
If they are not the same than we just had a date change (past midnight).
In the later case the program continues.
-----------------------------------------------------------------------
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth=$dbh->prepare('UPDATE `va_items` SET `price`="'."$item_price".'" WHERE `is_offer_special`="1";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="0" WHERE `is_special_offer`="1";');
$sth->execute();$sth->finish;
############## below products are always special and free ##################################
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="1" WHERE `item_id`="1";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="1" WHERE `item_id`="2";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="1" WHERE `item_id`="10385";');
$sth->execute();$sth->finish;
############## above products are always special and free ##################################
--------------------------------------------------------------------
First we make sure that all the products on special are set back to their normal status. Then, products that are always on special are made special again. In my example case products with item numbers 1, 2 and 10385 are always on special.
-----------------------------------------------------------------------
############## now we are getting the total amount of items in the database ##############
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth = $dbh->prepare('SELECT COUNT(*) FROM `va_items`;');
$sth->execute();( $total_items ) = $sth->fetchrow_array();
$sth->finish;$dbh->disconnect;
############## now we generate a random number equal to an item number in the database ###
$new_item=int(rand($total_items));
############## get the original price of the new daily special ###########################
$sth=$dbh->prepare('SELECT * FROM `va_items` WHERE `item_id="'."$new_item".'";');
$sth->execute();while(my $ref = $sth->fetchrow_hashref()){
$price=$ref->{'price'};
}$sth->execute();$sth->finish;
---------------------------------------------------------------------
The next step is to get the total number of items in the database. When we have that amount we generate a random value between 1 and that total amount.
When we have the random number, we collect the price of the product with its item_id number the same as the random number.
---------------------------------------------------------------------
############## now we update the data in the database for the daily special ##############
$sth=$dbh->prepare('UPDATE `va_special` SET `special_date`="'."$date".'";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_special` SET `item_price`="'."$price".'";');
$sth->execute();$sth->finish;
############## now we are setting this new item on special and for $0.00 #################
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="1" WHERE `item_id`="'."$new_item".'";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_items` SET `price`="0.00" WHERE `item_id`="'."$new_item".'";');
$sth->execute();$sth->finish;
The next step is that we update the date variable in the database to the today's date and we add the item original price also into the "va_special" database table.
next step is we make the price of this new special o.oo (for free.)
As you can see, the image itself is a " .pl" (perl) file located in the cgi-bin folder of the server.
I gave the file the name "product_on_special.pl".
As soon as a visitor visits your site, the image tag line in the footer will trigger the execution of the perl file.
First the perl program will execute its coding in the perl environment and then forward the image "check.gif" to the requesting footer.html
What you will see is just a little "tick" image.
However it executes the coding as explained above.
This now means that I do not have to do the change every day.
Its the visitors to the website who do the change for me.
The first visitor after midnight, triggers the change of daily special, while the rest of the visitors don't do a thing.
In both cases however the "check.gif" image is displayed on the page.
Any questions???
I tried to do it as clear as possible with all the explanation lines in the script as well.
If you need help, please come to http://www.vt.net.nz
You can contact me there on the LifeSupport helpdesk.
Cheers
Herman ( KiwiMate )
kiwimate (Guest)
29 Mar 2009 7:59 AM
Ooops I made a typing mistake. !!!
`is_offer_special` needs to be `is_special_offer`
in the below block of code.
###### there is a change needed so program will continue with the instructions below. #####
############## setting everything back to its default settings ###########################
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass);
$sth=$dbh->prepare('UPDATE `va_items` SET `price`="'."$item_price".'" WHERE `is_offer_special`="1";');
$sth->execute();$sth->finish;
$sth=$dbh->prepare('UPDATE `va_items` SET `is_special_offer`="0" WHERE `is_special_offer`="1";');
$sth->execute();$sth->finish;
############## below products are always special and free ##################################
Sorry
Webbia
15 Jul 2011 1:35 PM
Well, now in version 4.0.7 none of the suggestions works. Even if I set the system to allow php in custom blocks and then push the source button before pasting the code, the code shows in the webshop as text.
And if I create a php file with the code and set the block to get the content from the file, the result is blank.
And the last suggestion is not interesting for me since I just want to show random products on the startpage, not selling them instantly nor collect email addresses.
Has anyone asked ViArt how much it would cost to make this as a customization? Or do you know if they plan to include it in the next update? It is a lot strange that there is no function like this in the system still today!
Webbia
18 Jul 2011 1:33 PM
I have now asked ViArt to make a new block with random product showing and some settings (i.e. exclusion of some products, how many products to be shown and in how many columns). They want to charge $100 for this, does anyone else want this function and would like to share the cost with me?