0

I have been searching for a while now, but I cannot put together enough information in order to resolve this problem.

I created an event calendar with a field that allows the user to enter a range for booking an event. I already have the start date configured for the date selected. When a user enters the end date, the information in the fields will be duplicated for each date in the range.

            $requester = $_POST['txtrequester'];
            $title = $_POST['txttitle'];
            $detail = $_POST['txtdetail'];
            $startTime = $_POST['txtstarttime'];
            $endTime = $_POST['txtendtime'];
            $endDate = $_POST['txtenddate']; //end date of range request
            $eventdate = $month."/".$day."/".$year;
            $today_dt = $year."-".$month."-".$day;
            $startDate_dt = strtotime("$year."-".$month."-".$day");
            $endDate_dt = strtotime($endDate);
            $endDate_dt = $endDate_dt->modify('+1 day');
            $interval = DateInterval::createFromDateString('1 day');
            $period = new DatePeriod($today_dt, $interval, $endDate_dt);

Can someone help me figure this out? Thanks in advance

--Edit--

Code inserting event form data into mysql

        $sqlinsert = "INSERT INTO conferenceevents (Requester,Title,Detail,eventDate,dateAdded,startTime,endTime,endRange) values ('$requester','$title','$detail','$eventdate',now(),'$startTime','$endTime','$endDate')";
        $resultinsert = $connection->query($sqlinsert); 


        if($resultinsert){
            echo "<h4>Event was Successfully Added</h4><br>";
        }else{
            echo "<h4>Event Failed to be Added</h4><br>" . $sqlinsert;
        }
    }

Event form html

<form name='eventform' method='POST' action="<?php $_SERVER['PHP_SELF']; ?>?month=<?php echo $month;?>&day=<?php echo $day;?>&year=<?php echo $year;?>&v=true&add=true">

    <div class="form-group">
    <label for="requester">Requester</label>
    <select class="form-control" name='txtrequester'>
      <option name='txtrequester'>Melissa</option>
      <option name='txtrequester'>Greg</option>
      <option name='txtrequester'>Matt</option>
      <option name='txtrequester'>Michael</option>
      <option name='txtrequester'>Ben</option>
      <option name='txtrequester'>Pat</option>
      <option name='txtrequester'>Maria</option>
    </select>
    </div>
    <div class="form-group">
    <label for="eventTitle">Event Title</label>
    <input type="text" class="form-control" id="eventTitle" placeholder="Event Title" name='txttitle'>
    </div>
    <div class="form-group">
    <label for="eventStartTime">Event Start Time</label>
    <input type="time" class="form-control" id="eventStartTime" value="12:00" name='txtstarttime'>
    </div>
    <div class="form-group">
    <label for="eventEndTime">Event End Time</label>
    <input type="time" class="form-control" id="eventEndTime"  value="13:00" name='txtendtime'>
    </div>
    <a href class="option"><h4>Click here to enter date range (optional)</h4></a>
    <div class="form-group range" style="display: none;">
    <label for="eventStartDate">Event Start Date</label>
    <input type="text" class="form-control" id="disabledInput" name='txtstartdate' disabled value="<?php echo $month . "/" . $day . "/" . $year; ?>">
    </div>
    <div class="form-group range" style="display: none;">
    <label for="eventEndDate">Event End Date</label>
    <input type="date" value="<?php echo $year . "-" . $month . "-" . $day; ?>" class="form-control" id="eventEndDate" name='txtenddate'>
    </div>
    <div class="form-group">
    <label for="eventDetail">Event Detail</label>
    <input type="text" id="eventDetail" type="text" class="form-control" placeholder="Event Detail" name='txtdetail'>
    </div>
    <button type="submit" class="btn btn-default" name='btnadd'>Add Event</button>

</form>
5
  • What are you trying to achieve ? Commented Jan 13, 2015 at 16:06
  • If someone enters a date in the range field "$endDate", when they submit, php will insert a row with the same information for every date in that range(the start date will already be defined by the date they first clicked on). Commented Jan 13, 2015 at 16:11
  • Thats understood.... But what have you tried to insert ? Where is the code? Commented Jan 13, 2015 at 16:13
  • You want to duplicate the event data for every date within a range? Why? That seems completely unnecessary. Commented Jan 13, 2015 at 16:14
  • The overall use of this is for a user to easily "book" several days consecutively on a calender with the same event. For example, if the user wanted to book a week for and event named "spa", on the event form, they would just need to enter the end date of their "reservation" and when they submit, they will see that whole week "booked". Commented Jan 13, 2015 at 16:22

1 Answer 1

1

It seems you are trying to insert a row in to your data base for each and every date in given date range... Please try it like this

$startTime = strtotime( '2015-01-11' );
$endTime = strtotime( '2015-01-20' );

// Loop between timestamps, 24 hours at a time
for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) {
            $given_date =  date( 'Y-m-d', $i )."<br>";          
            // You can put your database insert query here
              echo "Data for $given_date is inserted";
}

More efficient way of inserting multiple rows is as follows..

$startTime = strtotime( '2015-01-11' );
$endTime = strtotime( '2015-01-20' );


 $sqlinsert = "INSERT INTO conferenceevents (Requester,Title,Detail,eventDate,dateAdded,startTime,endTime,endRange) values ";
 // Loop between timestamps, 24 hours at a time
    for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) {
            $eventdate =date('Y-m-d', $i ); 
          // Constructing the insert query
         $sqlinsert .= " ('$requester','$title','$detail','$eventdate',now(),'$startTime','$endTime','$endDate'),";
}

 $bulk_insert_query = rtrim($sqlinsert, ","); // to remove last comma
 $resultinsert = $connection->query($bulk_insert_query); 

To get more understanding about bulk insert query Try to echo $bulk_insert_query and see how it was constructed and how you want to improve..

Sign up to request clarification or add additional context in comments.

4 Comments

keep getting a "A non well formed numeric value encountered" error in php
You are the man!! It works for adding rows for dates, however for some reason, the start time and end time values are not passing?
Try echoing both to identify whether variable are properly declared. And in your codes, both are inserted as such... Check whether it is a right mysql format like Y-m-d
Figured it out, I was already using those variable names for the actual start time and end time of the event. Adjusted the name of the variables and everything is working as it should. Thanks alot Indra!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.