Solved

date question

Posted on 2006-07-20
5
239 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
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:smozgur
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:
smozgur 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:smozgur
ID: 17145051
Thanks for the A grade!

Suat
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now