Solved

date question

Posted on 2006-07-20
5
274 Views
Last Modified: 2008-03-10
Hello everyone,

I'm working on a way to add dates and events online instead of having to do that locally in the file and uploading it. The things I already accomplished is. I can give the date in this way: dd-mm-yyyy, when I ask the table it's sorted. When I add a date, it checks wehter it's a valid date.
What I want now is that when I ask to view the tabel, it not only sorts the table but also gives only the date beginning from today (which is variable, because tommorrow is another day) Until now I have the next code:

add.php

<?php
mysql_connect('localhost','root','');
mysql_select_db('mydb') or die (mysql_error());

if(isset($_POST['verzenden']))
  {
              $dag = $_POST['dag'];
            $maand = $_POST['maand'];
            $jaar = $_POST['jaar'];
            
            if(checkdate($maand, $dag, $jaar))
            {
                $Datum = $_POST['jaar'].$_POST['maand'].$_POST['dag'];
          $Event      =    addslashes($_POST['event']);
          mysql_query("INSERT INTO events (datum, event) VALUES ('".$Datum."', '".$Event."')") or die (mysql_error());

    echo 'Je gegevens zijn succesvol in de database geplaatst <br />';
            }
            else
            {
                echo 'Je hebt een ongeldige datum ingevoerd <br />';
            }
 
  }
else
  {
?>
<form action="<?=$_SERVER['PHP_SELF']?> " method="post">
Dag (2 cijfers): <input type="text" name="dag" />&nbsp; Maand (2 cijfers): <input type"text" name="maand" />&nbsp;Jaar (4 cijfers): <input type="text" name="jaar" /> <br />
Event: <input type="text" name="event" /><br />
<input type="submit" name="verzenden" value="verzenden" />
</form>
<?
  }
 
?>

show.php
<?php
mysql_connect('localhost','root','');
mysql_select_db('jvanattenhoven') or die (mysql_error());

if(mysql_result(mysql_query("SELECT COUNT(id) FROM `events`"),0) == 0)
  {
    echo 'Er staan nog geen events in de database';
  }
else
  {

  ?>
<table border=1 cellpadding=2 cellspacing=0>
<tr>
<td  style="width:300px;">Datum:</td>
<td>Event:</td>
</tr>
<?php
  $qSelect_berichten  = mysql_query('SELECT * FROM `events` ORDER by datum') or die (mysql_error());
  while($aBerichten = mysql_fetch_array($qSelect_berichten))
    {
     
      $datum = $aBerichten['datum'];

      $nieuw_datum = substr( $datum, 8, 2 ) . '-' . substr( $datum, 5, 2 ) . '-' . substr( $datum, 0, 4 );

      echo '<tr>
              <td>'.$nieuw_datum.'</td>
              <td>'.$aBerichten['event'].'</td>
            </tr>';
    }
     echo '</table>';
   }
?>

<br />

In my MySQL table I have the next fields:

id (int, autoincrement)
datum (datetime)
event (varchar)

How can I accomplish this?
0
Comment
Question by:jvuz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 17144848
Hi,

To list the records starting from today, you need to put WHERE conditions in your SQL strings by using the $today value. $today should be also built in code as today's date value as below.

Modified Code:

<?php
mysql_connect('localhost','root','');
mysql_select_db('jvanattenhoven') or die (mysql_error());

$today = date("Y-m-d");

if(mysql_result(mysql_query("SELECT COUNT(id) FROM `events` WHERE `datum`>='$today'"),0) == 0)
  {
    echo 'Er staan nog geen events in de database';
  }
else
  {

  ?>
<table border=1 cellpadding=2 cellspacing=0>
<tr>
<td  style="width:300px;">Datum:</td>
<td>Event:</td>
</tr>
<?php
  $qSelect_berichten  = mysql_query('SELECT * FROM `events` WHERE `datum`>='$today' ORDER by `datum`') or die (mysql_error());
  while($aBerichten = mysql_fetch_array($qSelect_berichten))
    {
     
      $datum = $aBerichten['datum'];

      $nieuw_datum = substr( $datum, 8, 2 ) . '-' . substr( $datum, 5, 2 ) . '-' . substr( $datum, 0, 4 );

      echo '<tr>
              <td>'.$nieuw_datum.'</td>
              <td>'.$aBerichten['event'].'</td>
            </tr>';
    }
     echo '</table>';
   }
?>

<br />

I hope it helps.

Suat
0
 
LVL 21

Author Comment

by:jvuz
ID: 17145007
I'm getting parse error on line 21
0
 
LVL 17

Accepted Solution

by:
Suat Ozgur earned 500 total points
ID: 17145031
Sure, I didn't notice that in your original code.
Please change the line 21 as following (used double quotes to cover the SQL string instead single);

$qSelect_berichten  = mysql_query("SELECT * FROM `events` WHERE `datum`>='$today' ORDER by `datum`") or die (mysql_error());

Complete code:
--------------------
<?php
mysql_connect('localhost','root','');
mysql_select_db('jvanattenhoven') or die (mysql_error());

$today = date("Y-m-d");

if(mysql_result(mysql_query("SELECT COUNT(id) FROM `events` WHERE `datum`>='$today'"),0) == 0)
  {
    echo 'Er staan nog geen events in de database';
  }
else
  {

  ?>
<table border=1 cellpadding=2 cellspacing=0>
<tr>
<td  style="width:300px;">Datum:</td>
<td>Event:</td>
</tr>
<?php
  $qSelect_berichten  = mysql_query("SELECT * FROM `events` WHERE `datum`>='$today' ORDER by `datum`") or die (mysql_error());
  while($aBerichten = mysql_fetch_array($qSelect_berichten))
    {
     
      $datum = $aBerichten['datum'];

      $nieuw_datum = substr( $datum, 8, 2 ) . '-' . substr( $datum, 5, 2 ) . '-' . substr( $datum, 0, 4 );

      echo '<tr>
              <td>'.$nieuw_datum.'</td>
              <td>'.$aBerichten['event'].'</td>
            </tr>';
    }
     echo '</table>';
   }
?>

<br />


Suat
0
 
LVL 21

Author Comment

by:jvuz
ID: 17145041
Great!!!,

points are yours!
0
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 17145051
Thanks for the A grade!

Suat
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Node.js 11 81
deprecated mysql extensions 1 54
PHP $_POST vs asp request 4 57
Php variable to be sent back 3 34
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question