Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Wyświetlanie wydarzeń każdego dnia - problem
Forum PHP.pl > Forum > Przedszkole
kadela
witam

Mam problem z utworzeniem ( wydaje mi się ) prostego skryptu w php.
Mianowicie mam 365 różnych dat - wydarzeń. I skrypt ma po prostu pierwszego
stycznia wyświetlić tylko datę którą do niego przypiszemy. Drugi, trzeci stycznia
itd wyświetla to co podamy w tym skrypcie.. I tyle, jak to ogarnąć ?

Oczywiście te 365 dat będzie najpierw trzeba wprowadzić ręcznie, ale mój
poziom php jest raczkujący sad.gif

Z góry
dziękuje za pomoc!
woxala123
Witam!
Nie ma sensu wprowadzać 365 dat. Wystarczy jak zrobisz sobie pole tekstowe z formułą data type DATE I WTEDY WPROWADZASZ DATE JAKA CHCESZ I PRZYPISUJESZ ZDARZENIE.
  1. CCREATE TABLE `wydarzenia` (
  2. `id` INT(11) NOT NULL AUTO_INCREMENT,
  3. `data` DATE NOT NULL,
  4. `zdarzenie` VARCHAR(255) NULL DEFAULT NULL,
  5. PRIMARY KEY (`id`)
  6. )
  7. COLLATE='utf8_general_ci'
  8. ENGINE=InnoDB
  9. AUTO_INCREMENT=3
  10. ;
  11.  
  12. ;
  13.  
  14.  

  1. <?php require_once('../Connections/local.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6. if (PHP_VERSION < 6) {
  7. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8. }
  9.  
  10. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12. switch ($theType) {
  13. case "text":
  14. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15. break;
  16. case "long":
  17. case "int":
  18. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19. break;
  20. case "double":
  21. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22. break;
  23. case "date":
  24. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25. break;
  26. case "defined":
  27. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28. break;
  29. }
  30. return $theValue;
  31. }
  32. }
  33.  
  34. $editFormAction = $_SERVER['PHP_SELF'];
  35. if (isset($_SERVER['QUERY_STRING'])) {
  36. $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  37. }
  38.  
  39. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  40. $insertSQL = sprintf("INSERT INTO wydarzenia (`data`, zdarzenie) VALUES (%s, %s)",
  41. GetSQLValueString($_POST['dataform'], "date"),
  42. GetSQLValueString($_POST['wydform'], "text"));
  43.  
  44. mysql_select_db($database_local, $local);
  45. $Result1 = mysql_query($insertSQL, $local) or die(mysql_error());
  46.  
  47. $insertGoTo = "zdarzenie.php";
  48. if (isset($_SERVER['QUERY_STRING'])) {
  49. $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
  50. $insertGoTo .= $_SERVER['QUERY_STRING'];
  51. }
  52. header(sprintf("Location: %s", $insertGoTo));
  53. }
  54. ?>
  55. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  56. <html xmlns="http://www.w3.org/1999/xhtml">
  57. <head>
  58. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  59. <title>Dokument bez tytułu</title>
  60. </head>
  61.  
  62. <body>
  63. <form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  64. <p>
  65. <label for="dataform"></label>
  66. <input type="text" name="dataform" id="dataform" />
  67. </p>
  68. <p>
  69. <label for="wydform"></label>
  70. <textarea name="wydform" id="wydform" cols="45" rows="5"></textarea>
  71. <input type="submit" name="wydbutton" id="wydbutton" value="Wyślij" />
  72. </p>
  73. <input type="hidden" name="MM_insert" value="form1" />
  74. </form>
  75. </body>
  76. </html>

a potem zrobić sobie tylko wyszukiwarkę z poziomu select data i bedziesz miał problem z głowy.
i masz już tu wyszukiwarkę po dacie
search.php
  1. <?php require_once('../Connections/local.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6. if (PHP_VERSION < 6) {
  7. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8. }
  9.  
  10. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12. switch ($theType) {
  13. case "text":
  14. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15. break;
  16. case "long":
  17. case "int":
  18. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19. break;
  20. case "double":
  21. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22. break;
  23. case "date":
  24. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25. break;
  26. case "defined":
  27. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28. break;
  29. }
  30. return $theValue;
  31. }
  32. }
  33.  
  34. mysql_select_db($database_local, $local);
  35. $query_Recordset1 = "SELECT * FROM wydarzenia";
  36. $Recordset1 = mysql_query($query_Recordset1, $local) or die(mysql_error());
  37. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  38. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  39. ?>
  40. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  41. <html xmlns="http://www.w3.org/1999/xhtml">
  42. <head>
  43. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  44. <title>Dokument bez tytułu</title>
  45. </head>
  46.  
  47. <body>
  48. <form id="form1" name="form1" method="post" action="">
  49. <table border="1">
  50. <tr>
  51. <td>data</td>
  52. </tr>
  53. <tr>
  54. <td>Wydarzenie
  55. <label for="select"></label>
  56. <select name="select" id="select">
  57. <?php
  58. do {
  59. ?>
  60. <option value="<?php echo $row_Recordset1['zdarzenie']?>"><?php echo $row_Recordset1['data'];echo'- '; echo $row_Recordset1['zdarzenie']?></option>
  61. <?php
  62. } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
  63. $rows = mysql_num_rows($Recordset1);
  64. if($rows > 0) {
  65. mysql_data_seek($Recordset1, 0);
  66. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  67. }
  68. ?>
  69. </select></td>
  70. </tr>
  71. </table>
  72. </form>
  73. </body>
  74. </html>
  75. <?php
  76. mysql_free_result($Recordset1);
  77. ?>
  78.  
viking
Po co mu wklejasz ten swój nieszczęsny kod funkcji GetSQLValueString pracujący w dodatku na wywalonym z PHP rozszerzeniu mysql? Collate utf8 powinien być, default 0 też nie ma sensu.

Co potrzebujesz to sterownika mysqli / pdo i proste selecty inserty na bazie danych. W dokumentacji przykłady które są powinny wystarczyć do ogarnięcia tematu zapytań.
Pyton_000
Takiego skopanego kodu to ja dawno nie widziałem... ;(
kadela
kto byłby zainteresowany żeby napisać to za kasę - proszę o priv i cena, tylko prościej niż to co kolega wyżej dał tongue.gif
woxala123
Cieszy mnie Pyton_000 że odezwałeś się w dzień świąteczny. Nie jest to kod na gotowca,tylko propozycja i zarys jak może to zrobić. Oczywiście kod w 60 sekund napisany headsetsmiley.png
Niech chłopak ruszy z kopyta i zacznie już coś tworzyć. Może już ma jakis kod i podzieli się z nim. Można zrobić jeszcze wodotryski z użyciem ajaxa.
Pozdrawiam
PS.
A gotowca to szukaj za kasę w innym dziale kadela
A jeszcze dodam kod z wodotryskiem
  1. <?php require_once('../Connections/local.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6. if (PHP_VERSION < 6) {
  7. $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8. }
  9.  
  10. $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12. switch ($theType) {
  13. case "text":
  14. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15. break;
  16. case "long":
  17. case "int":
  18. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19. break;
  20. case "double":
  21. $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22. break;
  23. case "date":
  24. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25. break;
  26. case "defined":
  27. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28. break;
  29. }
  30. return $theValue;
  31. }
  32. }
  33.  
  34. $editFormAction = $_SERVER['PHP_SELF'];
  35. if (isset($_SERVER['QUERY_STRING'])) {
  36. $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  37. }
  38.  
  39. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formkur")) {
  40. $insertSQL = sprintf("INSERT INTO wydarzenia (`data`, zdarzenie) VALUES (%s, %s)",
  41.  
  42. GetSQLValueString($_POST['dataa'], "date"),
  43. GetSQLValueString($_POST['formwyd'], "text"));
  44.  
  45. mysql_select_db($database_local, $local);
  46. $Result1 = mysql_query($insertSQL, $local) or die(mysql_error());
  47.  
  48. $insertGoTo = "poli.php";
  49. if (isset($_SERVER['QUERY_STRING'])) {
  50. $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
  51. $insertGoTo .= $_SERVER['QUERY_STRING'];
  52. }
  53. header(sprintf("Location: %s", $insertGoTo));
  54. }
  55. ?>
  56. <!doctype html>
  57. <html lang="en">
  58. <head>
  59. <meta charset="utf-8">
  60. <meta name="viewport" content="width=device-width, initial-scale=1">
  61. <title>jQuery UI Datepicker - Format date</title>
  62. <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  63. <link rel="stylesheet" href="/resources/demos/style.css">
  64. <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  65. <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  66. <script>
  67. $( function() {
  68. $( "#datepicker" ).datepicker();
  69. $( "#format" ).on( "change", function() {
  70. $( "#datepicker" ).datepicker( "option", "dateFormat", $( this ).val() );
  71. });
  72. } );
  73. </script>
  74. </head>
  75. <body>
  76.  
  77. <p><br>
  78. </p>
  79. <form action="<?php echo $editFormAction; ?>" method="POST" name="formkur" id="formkur">
  80. <p>
  81. <input name="dataa" type="text" id="datepicker" size="30">
  82. Date: </p>
  83. <p>
  84. <select name="format" id="format">
  85. <option value="mm/dd/yy">Default - mm/dd/yy</option>
  86. <option value="yy-mm-dd">ISO 8601 - yy-mm-dd</option>
  87. <option value="d M, y">Short - d M, y</option>
  88. <option value="d MM, y">Medium - d MM, y</option>
  89. <option value="DD, d MM, yy">Full - DD, d MM, yy</option>
  90. <option value="&apos;day&apos; d &apos;of&apos; MM &apos;in the year&apos; yy">With text - 'day' d 'of' MM 'in the year' yy</option>
  91. </select>
  92. Format options:</p>
  93. <p>
  94. <label for="formwyd"></label>
  95. <textarea name="formwyd" id="formwyd"></textarea>
  96. Wpisz wydarzenie</p>
  97. <input type="hidden" name="MM_insert" value="formkur">
  98. <input type="submit" name="but" id="but" value="Wyślij">
  99. </form>
  100. <p>&nbsp;</p>
  101.  
  102.  
  103. </body>
  104. </html>

Wybierasz sobie formę wpisania daty do bazy ale przeważnie wybieraj forme YY-mm-dd aa góry rozwinie Ci się kalendarz i wpiszesz jakiś tam komentarztu jeszcze kod do połączenia z bazą
local.php
  1. <?php
  2. # FileName="Connection_php_mysql.htm"
  3. # Type="MYSQL"
  4. # HTTP="true"
  5. $hostname_local = "localhost";
  6. $database_local = "twoja baza";
  7. $username_local = "tu twój nick do bazy";
  8. $password_local = "twoje hasło do bazy";
  9. $local = mysql_pconnect($hostname_local, $username_local, $password_local) or trigger_error(mysql_error(),E_USER_ERROR);
  10. ?>
  11.  
Pyton_000
Proszę Cię wywal te kody bo aż płakać się chce jak się na to patrzy. Nie dość że strasznie niechlujne to jeszcze pokazuje jak NIE pisać. A o mysql_* nie wspomnę...
woxala123
Chłopaki nie płaczą. To python popraw to po swojemu.
Pyton_000
Proszę bardzo... na szybko:
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>Document</title>
  9. </head>
  10. <body>
  11. <form action="POST">
  12. <label for="date">Data:</label>
  13. <input type="date" name="date" id="date"/>
  14.  
  15. <label for="message">Wiadaomość:</label>
  16. <textarea name="message" id="message" cols="30" rows="10"></textarea>
  17.  
  18. <?php
  19. if(!empty($_POST)) {
  20. /* Connect to a MySQL database using driver invocation */
  21. $dsn = 'mysql:host=127.0.0.1';
  22. $user = 'root';
  23. $password = 'root';
  24.  
  25. try {
  26. $dbh = new PDO($dsn, $user, $password);
  27. } catch (PDOException $e) {
  28. echo 'Connection failed: ' . $e->getMessage();die;
  29. }
  30. $sth = $dbh->prepare('INSERT INTO wydarzenia (`data`, zdarzenie) VALUES (?, ?)');
  31. $sth->bindParam(1, $_POST['date'], PDO::PARAM_STR);
  32. $sth->bindParam(2, $_POST['message'], PDO::PARAM_STR);
  33. $sth->execute();
  34. }
  35. ?>
  36. </form>
  37. </body>
  38. </html>


Oczywiście trzeba dodać sprawdzanie czy zmienne istnieją ale to szczegóły
com
Cytat
PHP_VERSION < 6


hehe biggrin.gif
Pyton_000
W sumie cały jego kod powinien zawierać się w tym ifie biggrin.gif
woxala123
Masz Pyton tego swego ifa oneeyedsmiley02.png
  1. CREATE TABLE `test` (
  2. `id` INT(11) NOT NULL AUTO_INCREMENT,
  3. `data` DATE NOT NULL,
  4. `message` VARCHAR(255) NOT NULL,
  5. PRIMARY KEY (`id`)
  6. )
  7. COLLATE='utf8_general_ci'
  8. ENGINE=InnoDB
  9. AUTO_INCREMENT=21
  10. ;
  11.  
  12.  


  1. <!doctype html>
  2. <html lang="pl">
  3. <meta charset="UTF-8">
  4. <meta name="viewport"
  5. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <form action="kalendarz.php" method="post">
  10.  
  11. <label for="date">Data:</label>
  12. <input type="date" name="date" id="date"/>
  13.  
  14. <label for="message">Wiadaomość:</label>
  15. <textarea name="message" id="message" cols="30" rows="10"></textarea>
  16.  
  17. <input type="submit" name="insert" value="Insert Data">
  18.  
  19. </form>
  20.  
  21. </body>
  22.  
  23. </html>
  24.  
  25.  
  26.  


  1. if(isset($_POST['insert']))
  2. {
  3. try {
  4.  
  5. // connect to mysql
  6.  
  7. $pdoConnect = new PDO('mysql:host=localhost; dbname=xxx','root','xxx');
  8. $pdoConnect ->query('SET CHARACTER SET utf8');
  9. $pdoConnect ->query('SET NAMES utf8');
  10.  
  11. } catch (PDOException $exc) {
  12. echo $exc->getMessage();
  13. exit();
  14. }
  15.  
  16. // get values form input text and number
  17. $date = $_POST['date'];
  18. $message = $_POST['message'];
  19.  
  20.  
  21. // mysql query to insert data
  22.  
  23. $pdoQuery = "INSERT INTO `test`(`data`, `message`) VALUES (:date,:message)";
  24. $pdoResult = $pdoConnect->prepare($pdoQuery);
  25. $pdoExec = $pdoResult->execute(array(":date"=>$date,":message"=>$message));
  26.  
  27. // check if mysql insert query successful
  28. if($pdoExec)
  29. {
  30. echo 'Dodano wydarzenie';
  31. }else{
  32. echo 'Nie dodano wydarzenia';
  33. }
  34. }
  35.  
  36. ?>
  37.  
Pyton_000
O widzisz, jak chcesz to potrafisz, teraz to ma przynajmniej ręce i nogi. Dobra robota.
woxala123
Teraz to przejdę na PDO. Szybciej pisze się kody. Tylko jeszcze pętle muszę poznać w PDO i będzie po mału git.
viking
Tylko zamiast tego if($pdoExec) lepiej try/catch
woxala123

Witam.
Gdybyś chciał skorzystać Kadela z powyższego kalendarza to dorzucę Ci jeszcze mój skrypt na wyszukiwanie zdarzeń po dacie którą sobie zaznaczysz w kalendarzu.
  1. <?php
  2. //Połączenie z bazą
  3. try {
  4.  
  5. $pdo = new PDO('mysql:host=localhost; dbname=xxx','root','xxx');
  6.  
  7. $pdo ->query('SET CHARACTER SET utf8');
  8. $pdo ->query('SET NAMES utf8');
  9. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.  
  11. }
  12. catch(PDOException $e)
  13. {
  14. echo "Brak połączenia: " . $e->getMessage();
  15. }
  16.  
  17.  
  18. // Wyszukiwanie z bazy danych
  19. $search=$_POST['date'];
  20. $query = $pdo->prepare("select * from test where data LIKE '%$search%' OR message LIKE '%$search%' LIMIT 1 , 15");
  21. $query->bindValue(1, "%$search%", PDO::PARAM_STR);
  22. $query->execute();
  23. // Display search result
  24. if (!$query->rowCount() == 0) {
  25. echo "Znaleziono :<br/>";
  26. echo "<table style=\"font-family:arial;color:#333333;\">";
  27. echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Data</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\">Wiadomosć</td><td </tr>";
  28. while ($results = $query->fetch()) {
  29. echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
  30. echo $results['data'];
  31. echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;\">";
  32. echo $results['message'];
  33.  
  34. echo "</td></tr>";
  35. }
  36. echo "</table>";
  37. } else {
  38. echo 'Nie znaleziono';
  39. }
  40. ?>
  41. <html>
  42. <head>
  43. <title> Wyszukiwarka </title>
  44. </head>
  45. <body>
  46. <form action="p1.php" method="post">
  47. Wyszukaj:
  48. <input type="date" name="date" placeholder=" Wyszukaj tutaj ..."/>
  49. <input type="submit" value="Submit" />
  50. </form>
  51.  
  52. </body>
  53. </html>
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.