Merge two php functions in a wordpress plugin - php

I'm using a Calendar plugin and there's one thing I would like to change
There's a php function that adds the event name:
include/Calendar/Calendar.class.php
$tab .= '<div class="namevent"> '.$event['event'].'</div>';
Output: My event name
I would like to add a TIME in the event title, after looking for a while I found the time functions in
public/events.php
if(!empty($custom['stime'][0]))
echo '<br /><strong>'.$home[16].':</strong> '.$custom['stime'][0].' ';
if(!empty($custom['etime'][0]))
echo '<strong>'.$home[17].'</strong> '.$custom['etime'][0];
Output: 10:00 to 11:00 (supposing your event time are these)
So then I tried this:
$tab .= '<div class="namevent"> '.$custom['stime'][0].' - '.$custom['etime'][0].' '.$event['event'].'</div>';
Expeting the output to be: 10:00 - 11:00 My event name
But it didn't work;
What i'm doing wrong?
Thanks a lot!

Make sure $custom['stime'][0] and $custom['etime'][0] exist, with a var_dump() or print_r() on each.
Add the following code just before your line which causes the error :
var_dump($custom['stime']); // Be sure the var is not undefined and [0] exists
var_dump($custom['etime']);

Related

Dynamically changing a session variable in php?

I have tried searching the site before posting and not finding anything about this. Forgive me if I am mistaken. What I am doing is making a calendar system. Each day is populating with the proper events and time, and I have a link in the <td> pointing to another php page for a longer description of the event. I have a foreach loop in a function that creates the calendar after getting the query from the database. I am assigning the long description row to a session variable and then printing it in the description.php page. My issue is the session variable is not changing during each iteration of the loop. Is this possible to do it this way? Was hoping I could just reference one variable to display the information, but I am doing something wrong. Any advice is appreciated!
Code in calendar.php
if(isset($events[$event_day])) {
foreach($events[$event_day] as $event) {
$calendar.= '<div class="event">'.$event['title'].'<br>'.$event['time'].'<br>'.$event['shortDesc'].'<br>More</div>';
$_SESSION['desc']= $event['longDesc'];
}
}
Code in description.php
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
include ('../dbinfo.php');
session_start();
$db=mysqli_connect($dbhostname,$dbuser,$dbpass,$dbname[0]);
print($_SESSION['desc']);
//print("<br><br>");
//print("Time:");
mysqli_close($db);//Close the DB Connection
?>
If any more code is needed to see what I am trying to accomplish I will edit this post! And yes, I have session_start() on the calendar page. The session is printing but not updating for each different event_date.
Try this:
<?php
if (isset($events[$event_day])) {
$_SESSION['desc'] = [];
foreach ($events[$event_day] as $event) {
$calendar .= '<div class="event">' . $event['title'] . '<br>' . $event['time'] . '<br>' . $event['shortDesc'] . '<br>More</div>';
$_SESSION['desc'][] = $event['longDesc'];
}
}
What the code does here is to assign an empty array to $_SESSION['desc'] before adding elements to the array. The error you got saying "operator not supported for strings" is because $_SESSION['desc'] was not an array.

why Google Calendar all day events return start date as today with php

I am using Google calendar api V3 with PHP and am trying to get event's summary, description and start date. My issue is with the last element: Start date. When events in the calendar are set as "All day" events, the response from the following code gives me today's date and the exact time when the query was made:
echo "<div id=Activite class=titre>", $event->getSummary(), "</div>\n";
$event_date = (new DateTime($event->getStart()->getDateTime()))->format('d/m/Y H:i');
echo "<div class=date_start><span style=color:yellow;>Start: </span>", $event_date, "</div>\n";
Here is an example of what is returned:
prise de photos des élèves
Start: 22/11/2014 18:30
Reading the same output from the "Google APIs Explorer", I get:
start": {
"date": "2013-09-13"
Which is nothing like what I am getting in my case. What am I doing wrong?
Well, here goes for the answer. After great advise by "ippi", I got to understand that Google uses 2 different variables for an event: start... "Date" / "DateTime". The code that worked for me is this work around that enabled the code to recognize if it was dealing with an all day event or not:
if (($event->getStart()->getDate())!= NULL) {
$event_date = (new DateTime($event->getStart()->getDate()))->format('d/m/Y');
} else {
$event_date = (new DateTime($event->getStart()->getDateTime()))->format('d/m/Y H:i');
}

Display recurring dates from SQL Database using PHP

I'm accessing a database created by another web company to retrieve event information for my current client. My client enters info for events and notes whether the date is recurring or not. I'm trying to display all the recurring dates. So far I have been able to get everything to display, regular dates as well as recurring.
The tables are laid out as follows:
Events
Events_Recurring
Here is part of the Events table BIGGER PICTURE
This is what the Events_Recurring table looks like
When the client checks it as recurring, the events_recurring table creates a row with the Event ID and other information like what day of the week or month the event is recurring on.
I'm just not sure how to display multiples of that certain ID that is recurring. I have a start date, and end date I can access, as well as what day of the week it is recurring on.
So for example: If this event reoccured every thursday. and I knew it started on Jan 1st and ended Jan 31st, can I run through that and spit out 4 different events all with the date of every Thursday in January?
Here is the full code I am working with, it's a little messy while trying to figure this out. I'm checking for the recurrence towards the bottom
// Access external database
$events_db = new wpdb(TOP SECRET CREDENTIALS HERE);
$events_db->show_errors();
if ($events_db) :
// Query Events Database
$events = $events_db->get_results(
"
SELECT ID, RequestDateStart, RequestDateEnd, Ministry, RequestTimeStart, EventName, CoordinatorName, EventDescription, Location
FROM gc_events
WHERE PrivateEvent = 0
AND Ministry = 15
AND date(RequestDateStart)>=date(NOW())
ORDER BY RequestDateStart
"
);
// Create the event data that will be displayed
foreach ($events as $event) :
// Store Event ID in a variable
$masterID = $event->ID;
echo '<div class="col-12">';
echo '<strong>ID:</strong> ' . $event->ID . '<br /><strong>Event Name:</strong> ' . $event->EventName . '<br /><strong>Leader:</strong> ' . $event->CoordinatorName . '<br /><strong>Date:</strong> ' . date('l, F j',strtotime($event->RequestDateStart)) . '<br /><strong>Start Time:</strong> ' . date('g:i a',strtotime($event->RequestTimeStart));
// CHECK IF RECURRING
$recurring_events = $events_db->get_results(
"
SELECT gc_event_id, period, day
FROM gc_event_recurring
WHERE gc_event_id = '$masterID'
"
);
foreach ($recurring_events as $recurring_event) :
if ($recurring_event->period === 'week') {
echo '<div class="col-12"><strong>↑ WEEKLY</strong><br />';
echo $recurring_event->day;
echo '</div>';
}
endforeach;
echo '</div>';
endforeach;
endif;
The result I am getting right now (with recurring events) is
Event: Weekly Prayer
Date: Feb 1, 2013
The result I would like is
Event: Weekly Prayer
Date: Feb 1, 2013
Event: Weekly Prayer
Date: Feb 8, 2013
Event: Weekly Prayer
Date: Feb 15, 2013
Event: Weekly Prayer
Date: Feb 22, 2013
This would be if the start date was Feb 1st and end date was Feb 28th.
A word of advice.
Although designing a database to store the 'description' of the repetition pattern is a very clean approach from a design point-of-view, you may get a lot of problems down the way.
I've done a project with a similar approach a while ago (I will look up the database design and add that to my answer) and, although I was able to reproduce the exact date/times of the recurring events, you will run into problems in the following situations; most originate from this:
the recurring events describe the repetition pattern, so the actual (individual) events are no physical records in your database
If the customer decides to add a new event, how will you check if it overlaps with any existing event? You'll have to calculate all 'events', based on the repetition pattern.
If the customer decides that the scheduled time for an event needs to be changed, how will you have this change apply to all future events and not for events that are in the past (you'll have to duplicate the original event, modify its end-date, and set the duplicated event with a new start-date)
If the customer decides he wants to remove a single day from the repetition pattern (e.g. a single event has ben canceled), you will also have to split the original event into two separate repetitions, or have a 'canceled/blocked' dates/times table
If people need to 'book' for specific events, you won't be able to attach them to a 'real' event-record, because the individual events because they are not physically present in the database. e.g. to check if a single event can be re-scheduled or canceled, you'll need to do this from code as the database cannot make use of foreign-key constraints to automatically update related reservations
Regarding performance; because individual events are not physically stored, they will have to be calculated every time you want to show them. Consider having 1000 recurring events in the database and try to show a 'calendar' of week 23 two years from now. You'll have to analyze all recurring-events patterns and calculate all events that they produce!
All depends of course on the actual usage of your system, but I wanted to warn you for problems we've run into.
Here's the schema for the 'schedules' table (contains recurring events pattern);
CREATE TABLE IF NOT EXISTS `schedules` (
`id` int(11) NOT NULL auto_increment,
`date_start` date NOT NULL,
`time_start` time NOT NULL,
`time_end` time NOT NULL,
`recur_until` date default NULL COMMENT 'end date when recurrence stops',
`recur_freq` varchar(30) default NULL COMMENT 'null, "secondly", "minutely", "hourly", "daily", "weekly", "monthly", "yearly"',
`recur_interval` smallint(5) unsigned default NULL COMMENT 'e.g. 1 for each day/week, 2 for every other day/week',
`recur_byday` smallint(5) unsigned default NULL COMMENT 'BITWISE; monday = 1, sunday = 64',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
How to circumvent the problems described
Fully describing a solution to these problems won't probably be suitable here, but here's some things to consider;
Storing a recurring event as described on itself is not bad practice. It perfectly describes when, and how often, an event should take place. However, the lack of physical records for the actual events is what causes the problem.
When creating or modifying a recurring event, calculate all resulting events and store them as physical records. These records can be queried, 'reservations' can be attached to them and you'll be able to make use of database features, like foreign-key-constraints to handle them properly.
When storing the individual events as described in 1., make sure you're keeping a reference to the 'schedule' that they belong to. If (for example) the customer wants to change the time of a recurring event, you'll be able to update all related (individual) events.
Keep in mind that in situation 2, you'll probably only want to update future events, so the 'recurring event' will still need to be 'split' in two to achieve that. In which case 'future' events need to be attached to the new 'recurring event', old events stay attached to the existing 'recurring event'
invest time in your database/software design, properly investigate if the design will 'work' for the thing you're trying to achieve. Test it, try things and if they don't work, don't hesitate to 'throw it away', often it's easier to start from scratch than try to 'fix' things. A proper design will take time and may take several 'redesigns' to get it right, but it will save you time and money in the end.
Hope this helps, good luck!
foreach ($events as $event) :
// Store Event ID in a variable
$masterID = $event->ID;
echo '<div class="col-12">';
echo '<strong>ID:</strong> ' . $event->ID . '<br /><strong>Event Name:</strong> ' . $event->EventName . '<br /><strong>Leader:</strong> ' . $event->CoordinatorName . '<br /><strong>Date:</strong> ' . date('l, F j',strtotime($event->RequestDateStart)) . '<br /><strong>Start Time:</strong> ' . date('g:i a',strtotime($event->RequestTimeStart));
// CHECK IF RECURRING
$recurring_events = $events_db->get_results(
"
SELECT gc_event_id, period, day
FROM gc_event_recurring
WHERE gc_event_id = '$masterID'
"
);
foreach ($recurring_events as $recurring_event) :
if ($recurring_event->period == 'week') {
$StartDate = strtotime($event->RequestDateStart);
$EndDate = strtotime($event->RequestDateEnd);
$TotalDays = round(($EndDate-$StartDate)/(60*60*24*7));
for($i = 0 ;$i<($TotalDays-1);$i++)
{
$StartDate += (60*60*24*7);
echo '<div class="col-12">';
echo '<strong>ID:</strong> ' . $event->ID . '<br /><strong>Event Name:</strong> ' . $event->EventName . '<br /><strong>Leader:</strong> ' . $event->CoordinatorName . '<br /><strong>Date:</strong> ' . date('l, F j',$StartDate) . '<br /><strong>Start Time:</strong> ' . date('g:i a',strtotime($event->RequestTimeStart));
}
}
endforeach;
echo '</div>';
endforeach;
try this and tell me if it works

PHP function call and array passing in Javascript code

Pls help..
I have a dynamic form in which the no. of elements are not fixed, and i need to perform JS validation on the form. I have a PHP function that will return me an array of all elements that are currently displayed in form. I want to use this in the JS code..
I am trying something lyk dis::
function checkValidation() {
<?php echo
// php function call
$arrColumnsInfo = $this->fetch_fields_info();
print('<pre>');
print_r($arrColumnsInfo);
print('</pre>');
?>
}
Not working for me..throws JS error..
OR this..
function checkValidation() {
<?php=
// php function call
$arrColumnsInfo = $this->fetch_fields_info();?>
alert(<?=$arrColumnsInfo?>);
}
Again.. not working..
My PHP array output:
Array
(
[client_code] => Client Code::1::Basic company Info::desc::TB
[entity_name] => Entity Name::1::Basic company Info::::TB
[type_of_entity] => Type of entity::1::Basic company Info::Type of entity,Sole trader,Partnership,Australian Private Company, other::DD
[fixed_fees] => fixed fees::8::Fees Related::yes,no::RD
[billing_time] => Billing time::8::Fees Related::Weekly,Fortnightly,Monthly,Yearly::DD
)
The key here used is the name of form element..
eg. i am building my form lyk dis..
<input type="text" name="client_code" value="">...
and so on.. and in the same way all form elements are dynamically created. And now i want to validate this form so i again need this array in javascript.
You could use json_encode to transform your PHP Array into a from that can be used by JavaScript.
function checkValidation() {
var data = <?php echo json_encode($this->fetch_fields_info()) ?>;
// ... run your client-side checks
alert(data);
}
Just keep in mind, that you always have to provide valid JavaScript code.
That can sometimes be quite tricky if you insist on writing that JavaScript code with inline PHP.
You should at least consider doing your validation on the server-side (in PHP) and only passing the result to your client.
your php code have to generate the result something like this:
var arr = <?php echo $arr; // result: [1,2,3,4,5,6,7] ?>
or the other array() format that support in JavaScript..
it is just an example.
take a look on the give link : Array in Javascript this link tells you that what format you need to use an Array.
data is not sufficient to help you #disha. please provide some more and edit this .
also what is this '=' in you first line of php? provide some more details that someone can really help you.
Happy coding!

Variable inside a php echo function

I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.

Categories