Solved

date question

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 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