Jump to content


Photo

perform script based on form input.


  • Please log in to reply
4 replies to this topic

#1 sivarts

sivarts
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 24 August 2006 - 09:30 PM

Hi fellow freaks-

I have a drop down menu with the following choices- Today, This Week, Next week, This Month.
What I want to do is get records from the DB where the results would be from one of the date ranges above. Does anyone know a good way to do this?

I tried writing an if/else clause that would perform a query to the DB based on the POST value of the form (where today=1,This Week=2,...) but that won't work since I cannot figuer out how to get the date 7(14,30)days from now. How does one add a number of days to todays date? I searched for about 4 hours on line but could not find anything that a newbie like me could understand to use. I need to some how get a variable $endDate to specify the date range in my SQL query. (which I assume I can write liek this: Select * From myTable WHERE event_date < $endDate --{there are no past dates in the DB at any time}). All my dates are stored in YYYY-MM-DD format (which I cannot change - although I wish I could have used timestamps!).

Also, could any one clue me in on how to write something like this:
if($_POST['getDays'] = 1) {
// go to this page (how do I write this logic to forward to a new page??)
} else {
if($_POST['getDays'] = 2) {
// go to this page
} etc...

There are so many functions in PHP and I am getting frustrated looking through all the documentaion on line. Does any of this make sense?? I am very lost as what to do-Any help would be much appreciated.

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 24 August 2006 - 10:07 PM

I just wrote this function for you. You will need to have the days in a format like this today = 0 tommorrow = 1 next week = 14 next month = 30. This is a very basic time function. It should give you an idea of how to add days. Well here is the function.

function formatDate($add_days) {
   $system_date = date("Y-m-d");
   list($y, $m, $d) = explode("-", $system_date);
   $days = date("t");
   if (($d + $add_days) > $days) {
       $a = $days - d;
       $day = $add_days - a;
       $month = $m + 1;
   }elseif (($m = 12) && (($d + $add_days) > $days)) {
       $year = $y + 1;
       $month = 1;
       $a = $days - $d;
       $day = $add_days - a;
   }else{
       $year = $y;
       $month = $m;
       $day = $d + $add_days;
   }
   $result = "$year-$month-$day";
   return $result;
}

Now the script that posts the get_date variable, you can do something like this.

$date = $_POST['date'];

if ($date == "tommorrow") {
   $add_days = 1;
}elseif ($date == "next week") {
   $add_days = "7";
}elseif ($date == "next month") {
   $add_days = "30";
}else{
   $add_days = "0";
}
$fdate = formatDate($add_days);

//now you can perform your query.

There are some potential issues with this. For instance if you want the exact date for next month then you need to add a month instead of just 30 days.

This was realy just meant to give you a basic Idea.

hope this helps,
Tom       

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 25 August 2006 - 12:11 AM

try
<?php
$choice = $_GET['choice'] ?  $_GET['choice'] : 1;
switch ($choice) {

    case 1: // today;
        $d1 = $d2 = date('Y-m-d');
        break;
        
    case 2: // this week
        $d = date('w');
        $d1 = date ('Y-m-d', strtotime("-$d days"));
        $d = 6 - $d;
        $d2 = date ('Y-m-d', strtotime("+$d days"));
        break;
        
    case 3: // next week
        $dow = date('w');
        $d = 7-$dow;
        $d1 = date ('Y-m-d', strtotime("+$d days"));
        $d = 13 - $dow;
        $d2 = date ('Y-m-d', strtotime("+$d days"));
        break;
        
    case 4: // this month
        $dom = date('j');
        $dim = date('t');
        $d = $dom-1;
        $d1 = date ('Y-m-d', strtotime("-$d days"));
        $d = $dim - $d - 1;
        $d2 = date ('Y-m-d', strtotime("+$d days"));
        break;
}

$sql = "SELECT * FROM mytable WHERE event_date BETWEEN '$d1' AND '$d2'";

echo $sql;
?>
<form>
<select name="choice">
  <option value="1">Today</option>
  <option value="2">This week</option>
  <option value="3">Next week</option>
  <option value="4">This month</option>
</select>
<input type="submit" name="do" value="do">
</form>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 sivarts

sivarts
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 25 August 2006 - 02:25 AM

Thanks tomfmason and Barand!! I'll let you know how these work out... Can't tell you how many times the nice people on this forum (like you two) have saved my ass!! All the best to you!

#5 sivarts

sivarts
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 25 August 2006 - 10:06 PM

Thanks gentlemen for providing me with some code. Both scripts work and I was easily able to edit them to suit my purpose exactly. Cheers




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users