witam,
dostosowuje do moich potrzeb skrypt losujacy produkty z bazy danych osCommerce i dodanie wylosowanych do specjalnej tabelki z promocjami..
Gdy tabelka jest pusta skrypt wypelnia ja 31 rekordami.
W przypadku ponownego wywolania skryptu (zeby dopisac jeden rekord) skrypt nic nie robi. Gdy trzeci raz wyoluje skrypt wszystko jest ok. Zawsze "ucieka" mi rekord z id=32 pomimo tego, ze mam komunikat ze dodalo.
<?php
/*
Filename: specials_random.php
Created/Last Edit: 29/12/2003 8:15PM
Created by Advis Network Systems
<a href=\"http://www.advisns.com/\" target=\"_blank\">http://www.advisns.com/</a>
Using osCommerce, Open Source E-Commerce Solutions
<a href=\"http://www.oscommerce.com/\" target=\"_blank\">http://www.oscommerce.com/</a>
Copyright (c) 2003 osCommerce & Advis Network Systems
*/
$non_count = 0;
$count = 0;
require('includes/application_top.php');
// require(DIR_WS_CLASSES . 'currencies.php');
// $currencies = new currencies();
// $expires_date = strftime(\"%Y-%m-%d\",mktime(0,0,0,date(\"m\"),date(\"d\")+NUMBER_SPECIAL_DAYS,date(\"Y\")));
// $date_time = strftime(\"%Y-%m-%d\",mktime(0,0,0,date(\"m\"),date(\"d\"),date(\"Y\")));
// echo $end_date;
// Funkcja zwraca liczbę dni między dwoma przekazanymi datami.
function count_days($b) {
$a_new = mktime(0
,0
,0
,date(\"m\"),date(\"d\"),date(\"Y\")); $b_new = mktime(0
,0
,0
,$b_dt[&apos
;1
&apos
;],$b_dt[&apos
;2
&apos
;],$b_dt[&apos
;0
&apos
;]);
return round(abs($a_new - $b_new) / 86400
); }
/////////////////////robimy porzadki///////////////////
$porzadki_query = tep_db_query(\"SELECT * FROM \" . TABLE_HAPPY_HOURS_SPECIALS_BASIC);
while ($porzadki=tep_db_fetch_array($porzadki_query)){
if (count_days($porzadki['happy_hours_beginning_date']) == 2)
{
tep_db_query(\"DELETE FROM `\".TABLE_HAPPY_HOURS_SPECIALS_BASIC.\"` WHERE `\".TABLE_HAPPY_HOURS_SPECIALS_BASIC.\"`.`specials_id` = \".$porzadki['specials_id'].\" LIMIT 1\");
}
}
////////////////////////////////////////////////////////////////////////
if (tep_db_num_rows($porzadki_query) == 0 ){
$random_products_query = tep_db_query(\"SELECT products_id, products_model, products_price from \" . TABLE_PRODUCTS . \" WHERE products_status ='1' and products_price > '\". SPECIAL_PRODUCT_MIN_PRICE .\"' order by rand() DESC limit \" . NUMBER_SPECIALS);
while ($random_products=tep_db_fetch_array($random_products_query)){
$expires_date = strftime(\"%Y-%m-%d\",mktime(0,0,0,date(\"m\"),date(\"d\")+$count+1,date(\"Y\"))); $date_time = strftime(\"%Y-%m-%d\",mktime(0,0,0,date(\"m\"),date(\"d\")+$count,date(\"Y\"))); ///////////
$special_product_query = tep_db_query(\"select products_id from \" . TABLE_HAPPY_HOURS_SPECIALS_BASIC . \" where products_id = \" . $random_products['products_id']);
if (tep_db_num_rows($special_product_query ) == 0 ){
$specials_price = ($random_products[&apos
;products_price
&apos
;] - round((SPECIAL_PERCENTAGE
/ 100
) * $random_products[&apos
;products_price
&apos
;])); tep_db_query(\"insert into \" . TABLE_HAPPY_HOURS_SPECIALS_BASIC . \" (products_id, specials_new_products_price, specials_date_added, expires_date
, status, status_happy_hours, happy_hours_beginning_date, happy_hours_end_date, s
pecials_price_happy_hours) values ('\" . (int)$random_products['products_id'] . \"', '\" . tep_db_input($specials_price) . \"', '\" . tep_db_input($date_time) . \"', '\" . tep_db_input($expires_date) . \"', '1', '1', '\" . tep_db_input($date_time) . \"', '\" . tep_db_input($expires_date) . \"', '\" . tep_db_input($specials_price) . \"')\");
$count++;
} else {
$non_count++;
}
}
}
else
{
//
$random_products_query = tep_db_query(\"SELECT products_id, products_model, products_price from \" . TABLE_PRODUCTS . \" WHERE products_status ='1' and products_price > '\". SPECIAL_PRODUCT_MIN_PRICE .\"' order by rand() DESC limit 2\");
while ($random_products=tep_db_fetch_array($random_products_query)){
//$expires_date = strftime(\"%Y-%m-%d\",mktime(0,0,0,date(\"m\"),date(\"d\")+$count+1,date(\"Y\")));
// $date_time = strftime(\"%Y-%m-%d\",mktime(0,0,0,date(\"m\"),date(\"d\")+$count,date(\"Y\")));
///////////
$special_product_query = tep_db_query(\"select products_id from \" . TABLE_HAPPY_HOURS_SPECIALS_BASIC . \" where products_id = \" . $random_products['products_id']);
//////
$ostatni_query = tep_db_query(\"SELECT happy_hours_beginning_date, happy_hours_end_date FROM \" . TABLE_HAPPY_HOURS_SPECIALS_BASIC . \" order by happy_hours_beginning_date DESC limit 1\");
$ostatni=tep_db_fetch_array($ostatni_query);
$ostatni_rozbity = explode(\"-\", $ostatni['happy_hours_end_date']); $ostatniGaw = mktime(0
,0
,0
,$ostatni_rozbity[&apos
;1
&apos
;],$ostatni_rozbity[&apos
;2
&apos
;],$ostatni_rozbity[&apos
;0
&apos
;]); $ostatniGaw2 = mktime(0
,0
,0
,$ostatni_rozbity[&apos
;1
&apos
;],$ostatni_rozbity[&apos
;2
&apos
;]+1
,$ostatni_rozbity[&apos
;0
&apos
;]);
//$ostatniGaw = $ostatniGaw+86400; //poczatek nowej
//$ostatniGaw2 = $ostatniGaw+86400; //koniec nowej
$ostatniGaw = strftime(\"%Y-%m-%d\",$ostatniGaw); $ostatniGaw2 = strftime(\"%Y-%m-%d\",$ostatniGaw2); echo $ostatniGaw.&apos
;-&apos
;; echo $ostatniGaw2.&apos
;<br
>&apos
;;
//
$specials_price = ($random_products[&apos
;products_price
&apos
;] - round((SPECIAL_PERCENTAGE
/ 100
) * $random_products[&apos
;products_price
&apos
;])); tep_db_query(\"insert into \" . TABLE_HAPPY_HOURS_SPECIALS_BASIC . \" (products_id, specials_new_products_price, specials_date_added, expires_date
, status, status_happy_hours, happy_hours_beginning_date, happy_hours_end_date, s
pecials_price_happy_hours) values ('\" . (int)$random_products['products_id'] . \"', '\" . tep_db_input($specials_price) . \"', '\" . tep_db_input($ostatniGaw) . \"', '\" . tep_db_input($ostatniGaw2) . \"', '1', '1', '\" . tep_db_input($ostatniGaw) . \"', '\" . tep_db_input($ostatniGaw2) . \"', '\" . tep_db_input($specials_price) . \"')\");
echo \"insert into \" . TABLE_HAPPY_HOURS_SPECIALS_BASIC . \" (products_id, specials_new_products_price, specials_date_added, expires_date
, status, status_happy_hours, happy_hours_beginning_date, happy_hours_end_date, s
pecials_price_happy_hours) values ('\" . (int)$random_products['products_id'] . \"', '\" . tep_db_input($specials_price) . \"', '\" . tep_db_input($ostatniGaw) . \"', '\" . tep_db_input($ostatniGaw2) . \"', '1', '1', '\" . tep_db_input($ostatniGaw) . \"', '\" . tep_db_input($ostatniGaw2) . \"', '\" . tep_db_input($specials_price) . \"')\"; $count++;
}
}
echo \"Special Products added: \" . $count . \"<br>\"; echo \"Special Products not added due to duplicates: \" . $non_count . \"<br>\"; ?>
na razie chce zeby to dzialalo dlatego prosze nie zwracac uwagi na niektore rzeczy (np. rozbijanie daty explode'em :-) )
Po pierwszym wywolaniu(i pustej tabeli) mam na ekranie
Special Products added: 31
Special Products not added due to duplicates: 0
czyli wszystko dobrze
Po drugim wywolaniu mam:
2007-12-26-2007-12-27
insert into happy_hours_specials_basic (products_id, specials_new_products_price, specials_date_added, expires_date, status, status_happy_hours, happy_hours_beginning_date, happy_hours_end_date, specials_price_happy_hours) values ('52', '87', '2007-12-26', '2007-12-27', '1', '1', '2007-12-26', '2007-12-27', '87')2007-12-27-2007-12-28
insert into happy_hours_specials_basic (products_id, specials_new_products_price, specials_date_added, expires_date, status, status_happy_hours, happy_hours_beginning_date, happy_hours_end_date, specials_price_happy_hours) values ('119', '103', '2007-12-27', '2007-12-28', '1', '1', '2007-12-27', '2007-12-28', '103')Special Products added: 2
Special Products not added due to duplicates: 0
czyli wstawia:
INSERT INTO happy_hours_specials_basic (products_id, specials_new_products_price, specials_date_added, expires_date, STATUS, status_happy_hours, happy_hours_beginning_date, happy_hours_end_date, specials_price_happy_hours) VALUES ('52', '87', '2007-12-26', '2007-12-27', '1', '1', '2007-12-26', '2007-12-27', '87')
ale problem w tym, ze ten rekord nie jest nigdy dodawany. absolutnie nigdy. no chyba ze dodam go w phpMyAdmin. zawsze gdzies ginie rekord o id=32
drugie zapytanie wstawiania wykonuje sie bez problemu czyli wstawia sie rekord o id=33 i dobrych datach.
Co najlepsze - przy trzecim wywolaniu skryptu dziala wszystko dobrze
prosze o pomoc, bo sam nie moge znalesc bledu
p.s. skrypt forum dodal znaki "\" nie wiem co to - to nie nalezy do mojego skryptu
Ten post edytował Gaw 25.11.2007, 11:18:45