dimanche 8 février 2015

Created a function similar to facebook events with [ Attending / Not attending ] options


Vote count:

0




I modified and extended an event plugin on wordpress that is being hosted on GITHUB then finally came up with this idea in a separate PHP file and added options such as the ATTENDING and NOT ATTENDING


Here is the complete working code and yes i have no problems what so ever after testing this and i believe this can also be used in different projects but i doubt my approach, I don't think this to be the best method to achieve it.



<?php
if( !defined( 'ABSPATH' ) ) {
exit;
}

global $wpdb;


$gender = get_user_meta( wp_get_current_user()->ID, 'cus_gender', true );
$attend = mysql_real_escape_string($_GET['attend']);

$events_m_check = get_user_meta( wp_get_current_user()->ID, 'cus_events_m', true );
$events_e_check = get_user_meta( wp_get_current_user()->ID, 'cus_events_e', true );


if (isset($attend) AND strlen(trim($attend)) > 0) {

$check_events = $wpdb->get_results( "SELECT * FROM wp_event_list WHERE id= $attend" );
foreach( $check_events as $check_event ) {
$event_type = $check_event->session;
}

if($event_type === 'm' AND $events_m_check == 0){
$attend_rows = $wpdb->get_results( "SELECT * FROM wp_event_list WHERE id= $attend" );
foreach( $attend_rows as $attend_row ) {
$userreport = $attend_row->attend;
}
$current_user = wp_get_current_user();
$explodeusers = explode(',', $userreport);

if (!in_array($current_user->ID,$explodeusers )) {
if(empty($userreport)){
$user = $current_user->ID;
}else{
$user = $current_user->ID;
$user .= ",";
$user .= $userreport;
}


$sql_update_attend = "UPDATE wp_event_list SET attend = '$user' WHERE id = '$attend'";
$wpdb->query($sql_update_attend);


$attend_rows = $wpdb->get_results( "SELECT * FROM wp_event_list WHERE id= $attend" );
foreach( $attend_rows as $attend_row ) {
$userreport = $attend_row->$gender;
}
$userreport = $userreport + 1;
$sql_update_attend = "UPDATE wp_event_list SET $gender = '$userreport' WHERE id = '$attend'";
$wpdb->query($sql_update_attend);

update_user_meta( wp_get_current_user()->ID, 'cus_events_m', '1' );
}
}

if($event_type === 'e' AND $events_e_check == 0){
$attend_rows = $wpdb->get_results( "SELECT * FROM wp_event_list WHERE id= $attend" );
foreach( $attend_rows as $attend_row ) {
$userreport = $attend_row->attend;
}
$current_user = wp_get_current_user();
$explodeusers = explode(',', $userreport);

if (!in_array($current_user->ID,$explodeusers )) {
if(empty($userreport)){
$user = $current_user->ID;
}else{
$user = $current_user->ID;
$user .= ",";
$user .= $userreport;
}


$sql_update_attend = "UPDATE wp_event_list SET attend = '$user' WHERE id = '$attend'";
$wpdb->query($sql_update_attend);


$attend_rows = $wpdb->get_results( "SELECT * FROM wp_event_list WHERE id= $attend" );
foreach( $attend_rows as $attend_row ) {
$userreport = $attend_row->$gender;
}
$userreport = $userreport + 1;
$sql_update_attend = "UPDATE wp_event_list SET $gender = '$userreport' WHERE id = '$attend'";
$wpdb->query($sql_update_attend);

update_user_meta( wp_get_current_user()->ID, 'cus_events_e', '1');
}
}

}


$notattend = mysql_real_escape_string($_GET['notattend']);

if (isset($notattend) AND strlen(trim($notattend)) > 0) {

$check_events = $wpdb->get_results( "SELECT * FROM wp_event_list WHERE id= $notattend" );
foreach( $check_events as $check_event ) {
$event_type = $check_event->session;
}

if($event_type === 'm' AND $events_m_check == 1){

$current_user = wp_get_current_user();

$user1 = $current_user->ID;

$sql_update_attend = "UPDATE wp_event_list SET attend = REPLACE(attend, '$user1', '') WHERE id = '$notattend'";
$wpdb->query($sql_update_attend);

$attend_rows = $wpdb->get_results( "SELECT * FROM wp_event_list WHERE id= $notattend" );
foreach( $attend_rows as $attend_row ) {
$userreport = $attend_row->$gender;
}

$userreport = $userreport - 1;
$sql_update_attend = "UPDATE wp_event_list SET $gender = '$userreport' WHERE id = '$notattend'";
$wpdb->query($sql_update_attend);

update_user_meta( wp_get_current_user()->ID, 'cus_events_m', '0');
}

if($event_type === 'e' AND $events_e_check == 1){

$current_user = wp_get_current_user();

$user1 = $current_user->ID;

$sql_update_attend = "UPDATE wp_event_list SET attend = REPLACE(attend, '$user1', '') WHERE id = '$notattend'";
$wpdb->query($sql_update_attend);

$attend_rows = $wpdb->get_results( "SELECT * FROM wp_event_list WHERE id= $notattend" );
foreach( $attend_rows as $attend_row ) {
$userreport = $attend_row->$gender;
}

$userreport = $userreport - 1;
$sql_update_attend = "UPDATE wp_event_list SET $gender = '$userreport' WHERE id = '$notattend'";
$wpdb->query($sql_update_attend);

update_user_meta( wp_get_current_user()->ID, 'cus_events_e', '0' );
}
}

?>


The SQL query for the table



$sql = 'CREATE TABLE '.$this->table.' (
id int(11) NOT NULL AUTO_INCREMENT,
pub_user bigint(20) NOT NULL,
time text,
title text NOT NULL,
location text,
details text,
categories text,
session text,
history text,
number text,
trainer text,
about_trainer text,
allowed text,
male_allowed text,
female_allowed text,
gender text,
attend text,
male text,
female text,
PRIMARY KEY (id) )
DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;';




IMPROVEMENTS


As this is a working code, What improvements can be made into the codes for further improvement in speed ?




QUESTION / PROBLEM


I've a button that says ATTENDING and the href is "?session=morning&attend=7" once we open this link ( It opens on the same page ) and the next time you see is the button NOT ATTENDING ( This is the second option after you plan to ATTEND the event, You can simply click NOT ATTENDING if you aren't planning or your mood has changed ) and the page refreshes again showing you the ATTEND button.


The way i am checking on page refresh whether to show the ATTENDING or NOT ATTENDING button is



$explodeusers = explode(',', $this->db->get_attend($event->id));
if(mysql_real_escape_string($_GET['session']) === 'morning'){
if (!in_array(wp_get_current_user()->ID,$explodeusers )){ //#00a6d0
$out .= '<a href="?session=morning&attend='.$event->id.'" class="button small color attend" target="_self">ATTENDING</a>';
}else{
$out .='<a href="?session=morning&notattend='.$event->id.'" class="button small color notattend" target="_self">NOT ATTENDING</a>';
}
}




asked 28 secs ago







Created a function similar to facebook events with [ Attending / Not attending ] options

Aucun commentaire:

Enregistrer un commentaire