Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Styl jako zmienna w adresie., Jak to zrobić?
-Tarr-
post 3.02.2006, 17:55:05
Post #1





Goście







Mam problem: jak zrobić , żeby ktoś na stronie mógł wybrać styl CSS, i żeby ten wybór zapisywało najlepiej jako ciasteczko. Adres wyglądałby wtedy jakoś tak: index.php?podstrona=x&styl=y . Podstronyw linkach wpisuję ręcznie, ale styl byłby stały dla każdej. Mam nadzieję, że napisałem to jasno.

Z góry dzięki za pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
mike
post 3.02.2006, 18:15:28
Post #2





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

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


Od czasu do czasu nawet Moderatorzy mówią ludzkim głosem, więc masz gotowca.
Nie jest to idealnie to co chcesz, ale lepsze tongue.gif
  1. <?php
  2.  
  3. if( isset( $_SESSION[ 'styleId' ] ) )
  4. {
  5. $intStyleId = (int)$_SESSION[ 'styleId' ];
  6. }
  7. else
  8. {
  9. if( isset( $_POST[ 'styleId' ] ) && ! empty( $_POST[ 'styleId' ] ) )
  10. {
  11. $intStyleId = (int)$_POST[ 'styleId' ];
  12. setcookie( 'styleId', $intStyleId, time() + 60 * 60 * 24 );
  13. }
  14. elseif( isset( $_COOKIE[ 'styleId' ] ) )
  15. {
  16. $intStyleId = (int)$_COOKIE[ 'styleId' ];
  17. }
  18. else
  19. {
  20. $intStyleId = 1;
  21. }
  22.  
  23. }
  24.  
  25. ?>
  26.  
  27. <link rel="stylesheet" type="text/css" href="style_<?php echo $intStyleId; ?>.css" />
  28.  
  29. <form id="styeSelect" action="#" method="post">
  30. <select name="styleId">
  31. <option value="1">Red</option>
  32. <option value="2">Green</option>
  33. <option value="3">Blue</option>
  34. </select>
  35. <input type="submit" name="submit" value="Change style" />
  36. </form>
  37.  
  38. <?php
  39.  
  40. echo 'Aktualny styl: ' . $intStyleId;
  41.  
  42. ?>
Go to the top of the page
+Quote Post
-Tarr-
post 3.02.2006, 18:33:26
Post #3





Goście







Wywala mi błąd:
Cytat
Warning: Cannot modify header information - headers already sent by (output started at /srv/www/htdocs/user/html/index.php:2) in /srv/www/htdocs/web125/html/p4/index.php on line 12
, czyli przy
  1. <?php
  2. setcookie( 'styleId', $intStyleId, time() + 60 * 60 * 24 );
  3. ?>
sad.gif
Przeniosłem
  1. <link rel="stylesheet" type="text/css" href="style_<?php echo $intStyleId; ?>.css" />
do head i nic nie dało sad.gif
Go to the top of the page
+Quote Post
mike
post 3.02.2006, 18:36:10
Post #4





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

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


Kod działa na pewno, ale wklejając go do swojego skryptu zrobiłeś na pewno błąd.
Pokaż jak to zrobiłeś.
Go to the top of the page
+Quote Post
-Tarr-
post 3.02.2006, 18:39:44
Post #5





Goście







  1. <?php
  2.  echo '<?xml version="1.0" encoding="iso-8859-2">';
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="content-type" content="text/html; charset=iso-8859-2" />
  8. <meta http-equiv="reply-to" content="" />
  9. <meta name="generator" content="WebSite PRO 4.3" />
  10. <meta name="author" content="" />
  11. <meta name="description" content="" />
  12. <title>Bez Tytułu</title>
  13. </head>
  14. <body>
  15. <?php
  16.  
  17. if( isset( $_SESSION[ 'styleId' ] ) )
  18. {
  19. $intStyleId = (int)$_SESSION[ 'styleId' ];
  20. }
  21. else
  22. {
  23. if( isset( $_POST[ 'styleId' ] ) && ! empty( $_POST[ 'styleId' ] ) )
  24. {
  25. $intStyleId = (int)$_POST[ 'styleId' ];
  26. setcookie( 'styleId', $intStyleId, time() + 60 * 60 * 24 );
  27. }
  28. elseif( isset( $_COOKIE[ 'styleId' ] ) )
  29. {
  30. $intStyleId = (int)$_COOKIE[ 'styleId' ];
  31. }
  32. else
  33. {
  34. $intStyleId = 1;
  35. }
  36.  
  37. }
  38.  
  39. echo 'Aktualny styl: ' . $intStyleId;
  40.  
  41. ?>
  42. <link rel="stylesheet" type="text/css" href="style_<?php echo $intStyleId; ?>.css" />
  43.  
  44.  
  45. <form id="styeSelect" action="#" method="post">
  46. <select name="styleId">
  47. <option value="1">Red</option>
  48. <option value="2">Green</option>
  49. <option value="3">Blue</option>
  50. </select>
  51. <input type="submit" name="submit" value="Change style" />
  52. </form>
  53. </body>
  54. </html>


~mike_mech
Go to the top of the page
+Quote Post
tiraeth
post 3.02.2006, 18:41:57
Post #6





Grupa: Przyjaciele php.pl
Postów: 1 789
Pomógł: 41
Dołączył: 30.10.2003
Skąd: Wrocław

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


Ten kod odpowiadający za zmianę stylu (pierwszy kod <?php...?>) umieść przed <html> tzn. pierwszym kodem jaki ma być w pliku html jest ten zawarty pomiędzy <?php..?> np.
  1. <?php
  2.  
  3. if( isset( $_SESSION[ 'styleId' ] ) )
  4. {
  5. $intStyleId = (int)$_SESSION[ 'styleId' ];
  6. }
  7. else
  8. {
  9. if( isset( $_POST[ 'styleId' ] ) && ! empty( $_POST[ 'styleId' ] ) )
  10. {
  11. $intStyleId = (int)$_POST[ 'styleId' ];
  12. setcookie( 'styleId', $intStyleId, time() + 60 * 60 * 24 );
  13. }
  14. elseif( isset( $_COOKIE[ 'styleId' ] ) )
  15. {
  16. $intStyleId = (int)$_COOKIE[ 'styleId' ];
  17. }
  18. else
  19. {
  20. $intStyleId = 1;
  21. }
  22.  
  23. }
  24.  
  25. ?>
  26.  
  27. <html>
  28. <head>
  29. <link rel="stylesheet" type="text/css" href="style_<?php echo $intStyleId; ?>.css" />
  30. </head>
  31. <body>
  32.  
  33. <form id="styeSelect" action="#" method="post">
  34. <select name="styleId">
  35. <option value="1">Red</option>
  36. <option value="2">Green</option>
  37. <option value="3">Blue</option>
  38. </select>
  39. <input type="submit" name="submit" value="Change style" />
  40. </form>
  41.  
  42. <?php
  43.  
  44. echo 'Aktualny styl: ' . $intStyleId;
  45.  
  46. ?>
  47. </body>
  48. </html>
Go to the top of the page
+Quote Post
-Tarr-
post 3.02.2006, 18:41:58
Post #7





Goście







A style są Red, Green i Blue
Go to the top of the page
+Quote Post
mike
post 3.02.2006, 18:44:00
Post #8





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

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


setcookie()
Cytat
setcookie() określa ciasteczko (ang. cookie) do wysłania z nagłówkami HTTP. Ciasteczko musi być wysłane zanim  jakiekolwiek inne nagłówki zostaną wysłane (to jest ograniczenie ciasteczek, nie php). To wymaga od ciebie umieszczenia wywołań tej funkcji przed znacznikami <html> czy <head>.

exclamation.gif!
Go to the top of the page
+Quote Post
-Tarr-
post 3.02.2006, 18:46:02
Post #9





Goście







Ups, zapomniałem :roll2:
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 - 07:58