Solved

date question

Posted on 2006-07-20
5
271 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: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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Echo Multiple values from multiple records 19 55
issue with DB import 1 36
Use Select Query to Return Results as a Form 9 41
How to use session variables in php? 22 46
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.  …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

756 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