Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Styl jako zmienna w adresie.
Forum PHP.pl > Forum > Przedszkole
Tarr
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.
mike
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. ?>
Tarr
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
mike
Kod działa na pewno, ale wklejając go do swojego skryptu zrobiłeś na pewno błąd.
Pokaż jak to zrobiłeś.
Tarr
  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
tiraeth
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>
Tarr
A style są Red, Green i Blue
mike
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!
Tarr
Ups, zapomniałem :roll2:
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.