Solved

date question

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

Independent Software Vendors: 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

623 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