Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Ostatnio oglądane produkty
northwest
post 19.06.2015, 00:22:54
Post #1





Grupa: Zarejestrowani
Postów: 788
Pomógł: 1
Dołączył: 17.09.2004

Ostrzeżenie: (10%)
X----


Witam serdecznie,
Potrzebuje skryptu który bedzie mi zapisywał w COOKIE 10 ostatnio oglądanych przez użytkownika produktów.
Miałoby to działać w następujący sposób:
- użytkownik wchodzi na kartę produktu,
- dodajemy nowy ostatnio oglądany produkt do COOKIE,
- sprawdzamy czy user oglądał już ten produkt (czy znajduje sie w cookie - i jeśli tak, to usuwa poprzedni),
- usuwamy produkty z pamięci powyżej 10

Czy mógłbym prosić o napisanie takiego skryptu?

Bardzo proszę o pomoc,
Northwest
Go to the top of the page
+Quote Post
Pyton_000
post 19.06.2015, 06:56:51
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


  1. array_unshift($_COOKIE['last_seen'], $product);
  2. $_COOKIE['last_seen'] = array_slice($_COOKIE['last_seen'], 0, 10);
Go to the top of the page
+Quote Post
northwest
post 19.06.2015, 16:36:23
Post #3





Grupa: Zarejestrowani
Postów: 788
Pomógł: 1
Dołączył: 17.09.2004

Ostrzeżenie: (10%)
X----


A jak takiemu Cookiesowi ustawić czas życia na np. 7 dni?

Wyświetlenie zawartości cookie np. w takiej formie:
- produkt id 1: ...
- produkt id 2: ...
- produkt id 3: ...
- produkt id 4: ...
- produkt id 5: ...
- produkt id 6: ...
- produkt id 7: ...
- produkt id 8: ...
- produkt id 9: ...
- produkt id 10: ...

robi się jakoś foreachem?

Dziękuję bardzo za pomoc smile.gif

ten kod nie zapisuje mi cookiesa sad.gif

Zwraca błąd:
Warning: array_unshift() expects parameter 1 to be array, null given in index.php on line 7

Warning: array_slice() expects parameter 1 to be array, null given in index.php on line 8

Ten post edytował northwest 19.06.2015, 16:43:03
Go to the top of the page
+Quote Post
sazian
post 19.06.2015, 16:57:35
Post #4





Grupa: Zarejestrowani
Postów: 1 045
Pomógł: 141
Dołączył: 19.09.2006
Skąd: B-tów

Ostrzeżenie: (0%)
-----


dodaj przed tym kodem to
  1. if(!isset($_COOKIE['last_seen'] || !is_array($_COOKIE['last_seen']))
  2. {
  3. setcookie('last_seen', array(), time()+604800);
  4.  
  5. }


pisane z palca, nie sprawdzałem
Go to the top of the page
+Quote Post
northwest
post 19.06.2015, 17:35:37
Post #5





Grupa: Zarejestrowani
Postów: 788
Pomógł: 1
Dołączył: 17.09.2004

Ostrzeżenie: (10%)
X----


mam taki kod:
  1. $product = rand(1000, 1000000);
  2. if(!isset($_COOKIE['last_seenxa']) || !is_array($_COOKIE['last_seenxa']))
  3. {
  4. setcookie('last_seenxa', array(), time()+604800);
  5. }
  6. array_unshift($_COOKIE['last_seenxa'], $product);
  7. $_COOKIE['last_seenxa'] = array_slice($_COOKIE['last_seenxa'], 0, 10);
  8.  
  9. echo $_COOKIE['last_seenxa'];


i nie zapisuje sad.gif

Zwraca:

Warning: setcookie() expects parameter 2 to be string, array given in index.php on line 40

Warning: array_unshift() expects parameter 1 to be array, null given in index.php on line 42

Warning: array_slice() expects parameter 1 to be array, null given in /index.php on line 43

Ten post edytował northwest 19.06.2015, 17:35:59
Go to the top of the page
+Quote Post
sazian
post 19.06.2015, 18:05:48
Post #6





Grupa: Zarejestrowani
Postów: 1 045
Pomógł: 141
Dołączył: 19.09.2006
Skąd: B-tów

Ostrzeżenie: (0%)
-----


  1.  
  2. if(!isset($_COOKIE['last_seenxa']) || !is_array($_COOKIE['last_seenxa']))
  3. {
  4. setcookie('last_seenxa', array(), time()+604800);
  5. }
  6. else
  7. {
  8. $_COOKIE['last_seenxa']=explode(',',$_COOKIE['last_seenxa']);
  9. }
  10. array_unshift($_COOKIE['last_seenxa'], $product);
  11. $_COOKIE['last_seenxa'] = array_slice($_COOKIE['last_seenxa'], 0, 10);
  12.  
  13.  
  14. var_dump($_COOKIE['last_seenxa'];);
  15.  


i gdzieś na końcu strony
  1.  
  2. $_COOKIE['last_seenxa']=imlode(',',$_COOKIE['last_seenxa']);


Go to the top of the page
+Quote Post
northwest
post 19.06.2015, 18:38:50
Post #7





Grupa: Zarejestrowani
Postów: 788
Pomógł: 1
Dołączył: 17.09.2004

Ostrzeżenie: (10%)
X----


też nie działa sad.gif NIe zapisuje nic

ten var_dump zwraca "null"
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 19:26