Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 01-28-2017, 11:39 PM   #1
Baseband Member
 
Join Date: Jul 2014
Location: USA
Posts: 29
Default Help With PHP Calender Code

Hi,
I've been hitting my head against a wall trying to figure out how to make every event that is rendered on the calendar change color based on the value of "ad_type" which is stored in a mysql database that's currently connected to this calendar. It works fine with loading the events for the calendar, so i know it's connecting to the DB I just can't get it to change the color of the event based on the value in the DB.

The IF statement (which is at about line 40 in the php code i've included below) that I have that is supposed to be doing that is always executing to false, so i think the problem may have something to do with correctly getting that variable from the DB. Any help would be greatly appreciated, thank you!

PHP Code:
<?php
/* draws a calendar */

function draw_calendar($month,$year,$events = array()){

    
/* draw table */
    
$calendar '<table cellpadding="0" cellspacing="0" class="calendar">';

    
/* table headings */
    
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

    
/* days and weeks vars now ... */
    
$running_day date('w',mktime(0,0,0,$month,1,$year));
    
$days_in_month date('t',mktime(0,0,0,$month,1,$year));
    
$days_in_this_week 1;
    
$day_counter 0;
    
$dates_array = array();

    
/* row for week one */
    
$calendar.= '<tr class="calendar-row">';

    
/* print "blank" days until the first of the current week */
    
for($x 0$x $running_day$x++):
        
$calendar.= '<td class="calendar-day-np">&nbsp;</td>';
        
$days_in_this_week++;
    endfor;

    
/* keep going with days.... */
    
for($list_day 1$list_day <= $days_in_month$list_day++):
        
$calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">';
            
/* add in the day number */
            
$calendar.= '<div class="day-number">'.$list_day.'</div>';
            
            
$event_day $year.'-'.$month.'-'.$list_day;
            if(isset(
$events[$event_day])) {
                
                foreach(
$events[$event_day] as $event) {
                                
                
$query "SELECT ad_type, DATE_FORMAT(display_date,'%Y-%m-%e') AS event_date FROM paidads WHERE display_date LIKE '$year-$month%'";
                    
                if ((
$row['ad_type']) == 'sidebar'){
                      
$calendar.='<div class="event" style="background-color:#FFb2b2;">'.$event['title'].'</div>';
                                                    }
                    elseif ((
$row['ad_type']) == '728x90') {
                      
$calendar.='<div class="event" style="background-color:#b2b2ff;">'.$event['title'].'</div>';
                                                            }
                        else {
                      
$calendar.='<div class="event" style="background-color:#ff99ff;">'.$event['title'].'</div>';
                            }
                    
/* OLD $calendar.= '<div class="event">'.$event['title'].'</div>';*/
                                                        
}
                }
            else {
                
$calendar.= str_repeat('<p>&nbsp;</p>',2);
                 }
                
        
$calendar.= '</div></td>';
        if(
$running_day == 6):
            
$calendar.= '</tr>';
            if((
$day_counter+1) != $days_in_month):
                
$calendar.= '<tr class="calendar-row">';
            endif;
            
$running_day = -1;
            
$days_in_this_week 0;
        endif;
        
$days_in_this_week++; $running_day++; $day_counter++;
    endfor;

    
/* finish the rest of the days in the week */
    
if($days_in_this_week 8):
        for(
$x 1$x <= ($days_in_this_week); $x++):
            
$calendar.= '<td style="background-color:#ffffff;" class="calendar-day-np">&nbsp;</td>';
        endfor;
    endif;

    
/* final row */
    
$calendar.= '</tr>';
    

    
/* end the table */
    
$calendar.= '</table>';

    
/** DEBUG **/
    
$calendar str_replace('</td>','</td>'."\n",$calendar);
    
$calendar str_replace('</tr>','</tr>'."\n",$calendar);
    
    
/* all done, return result */
    
return $calendar;
}

function 
random_number() {
    
srand(time());
    return (
rand() % 7);
}

/* date settings */
$month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));
$year = (int)  ($_GET['year'] ? $_GET['year'] : date('Y'));

/* select month control */
$select_month_control '<select name="month" id="month">';
for(
$x 1$x <= 12$x++) {
    
$select_month_control.= '<option value="'.$x.'"'.($x != $month '' ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';

/* select year control */
$year_range 7;
$select_year_control '<select name="year" id="year">';
for(
$x = ($year-floor($year_range/2)); $x <= ($year+floor($year_range/2)); $x++) {
    
$select_year_control.= '<option value="'.$x.'"'.($x != $year '' ' selected="selected"').'>'.$x.'</option>';
}
$select_year_control.= '</select>';

/* "next month" control */
$next_month_link '<span style="width:230px; padding-left:500px; text-align: right; font-size:16px; font-weight:bold;"><a href="?month='.($month != 12 $month 1).'&year='.($month != 12 $year $year 1).'" class="control">Next &gt;&gt;</a></span>';

echo 
'<h3 style="text-align:center; padding-right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h3>';

/* "previous month" control */
$previous_month_link '<span style="width:230px; padding-left: 170px; font-size:16px; font-weight:bold;"><a href="?month='.($month != $month 12).'&year='.($month != $year $year 1).'" class="control">&lt;&lt;     Previous</a></span>';


/* bringing the controls together */

$controls '<form method="get">'.$previous_month_link.''.$next_month_link.' </form>'

/* get all events for the given month */
$ad_type $_POST['ad_type'];
$month str_pad($month,2,'0'STR_PAD_LEFT);
$events = array();
$query "SELECT ad_name AS title, ad_type, DATE_FORMAT(display_date,'%Y-%m-%e') AS event_date, ad_type FROM paidads WHERE display_date LIKE '$year-$month%'";

echo 
"<br/>";
$result mysql_query($query,$db_link) or die('Cannot get Results!');
while(
$row mysql_fetch_assoc($result)) {
    
    
$events[$row['event_date']][] = $row;
    
}

echo 
'<div style="float:left;">'.$controls.'</div>';
echo 
'<div style="clear:both;"></div>';
echo 
draw_calendar($month,$year,$events);
echo 
'<br /><br />';
?>
__________________

__________________
megan1989 is offline   Reply With Quote
Old 01-30-2017, 10:15 AM   #2
Fully Optimized
 
joedaman633's Avatar
 
Join Date: Apr 2012
Location: England, Birmingham
Posts: 1,826
Default Re: Help With PHP Calender Code

Can you print that particular variable on screen to see what's happening?
__________________

__________________
Athlon II x4 645 || 1TB 7200rpm HDD || EVGA GTX 650Ti OC || 8GB DDR3 RAM || Windows 7 Home x64

i5 4210M || 500GB Samsung EVO 850 SSD || GeForce 825M || 16GB DDR3 RAM || Windows 10 x64
joedaman633 is offline   Reply With Quote
Old 01-30-2017, 02:16 PM   #3
Baseband Member
 
Join Date: Jul 2014
Location: USA
Posts: 29
Default Re: Help With PHP Calender Code

Yes, i can. But what that IF statement that is supposed to figuring out which entries to highlight in what color is always coming up as "False" which highlights everything in the same color which doesn't help. I've attached a screenshot of what's happening, hopefully that will help
__________________
megan1989 is offline   Reply With Quote
Old 01-31-2017, 04:55 AM   #4
Fully Optimized
 
joedaman633's Avatar
 
Join Date: Apr 2012
Location: England, Birmingham
Posts: 1,826
Default Re: Help With PHP Calender Code

I understand what you're trying to do, unfortunately I can't debug through the code from my end due to not having the DB etc. Your best bet is to print the variable that you're checking (ad_type) and ensure the value is one that you're expecting.
__________________
Athlon II x4 645 || 1TB 7200rpm HDD || EVGA GTX 650Ti OC || 8GB DDR3 RAM || Windows 7 Home x64

i5 4210M || 500GB Samsung EVO 850 SSD || GeForce 825M || 16GB DDR3 RAM || Windows 10 x64
joedaman633 is offline   Reply With Quote
Old 01-31-2017, 02:00 PM   #5
BSOD
 
Join Date: Feb 2016
Location: US
Posts: 963
Default Re: Help With PHP Calender Code

Lol exactly. If you want it done right, do it yourself and learn. Don't copy someone else's code when you might just do it better and more efficiently yourself.
Personally, I'd make something that hasn't been done to death. There are so many calendars out there that already do everything you'd want to do. Google has a nice one that integrates with Alexa.
__________________
Technician is offline   Reply With Quote
Old 02-01-2017, 04:42 AM   #6
Fully Optimized
 
joedaman633's Avatar
 
Join Date: Apr 2012
Location: England, Birmingham
Posts: 1,826
Default Re: Help With PHP Calender Code

Quote:
Originally Posted by Technician View Post
Lol exactly. If you want it done right, do it yourself and learn. Don't copy someone else's code when you might just do it better and more efficiently yourself.
Personally, I'd make something that hasn't been done to death. There are so many calendars out there that already do everything you'd want to do. Google has a nice one that integrates with Alexa.
They're asking a forum for help with their code, bit different from asking for recommendations for an OTS solution.
__________________
Athlon II x4 645 || 1TB 7200rpm HDD || EVGA GTX 650Ti OC || 8GB DDR3 RAM || Windows 7 Home x64

i5 4210M || 500GB Samsung EVO 850 SSD || GeForce 825M || 16GB DDR3 RAM || Windows 10 x64
joedaman633 is offline   Reply With Quote
Old 02-01-2017, 11:40 AM   #7
BSOD
 
Join Date: Feb 2016
Location: US
Posts: 963
Default Re: Help With PHP Calender Code

Quote:
Originally Posted by joedaman633 View Post
They're asking a forum for help with their code, bit different from asking for recommendations for an OTS solution.
A php coding forum might be a wiser choice than a computer forum.
__________________
Technician is offline   Reply With Quote
Old 02-02-2017, 03:55 AM   #8
Fully Optimized
 
joedaman633's Avatar
 
Join Date: Apr 2012
Location: England, Birmingham
Posts: 1,826
Default Re: Help With PHP Calender Code

Quote:
Originally Posted by Technician View Post
A php coding forum might be a wiser choice than a computer forum.


A computer forum isn't just for hardware issues buddy.
__________________
Athlon II x4 645 || 1TB 7200rpm HDD || EVGA GTX 650Ti OC || 8GB DDR3 RAM || Windows 7 Home x64

i5 4210M || 500GB Samsung EVO 850 SSD || GeForce 825M || 16GB DDR3 RAM || Windows 10 x64
joedaman633 is offline   Reply With Quote
Old 02-02-2017, 11:00 AM   #9
BSOD
 
Join Date: Feb 2016
Location: US
Posts: 963
Default Re: Help With PHP Calender Code

A computer forum isn't just for hardware issues buddy.[/QUOTE]

Lol is that how you read that? Might need some more practice for better comprehension.
What was said is it might be a better choice.
Chum.
__________________
Technician is offline   Reply With Quote
Old 02-06-2017, 11:44 AM   #10
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,107
Default Re: Help With PHP Calender Code

I suspect that the issue is:
if, else if , else should be if, else if, else if where you are sorting out your formatting.
(and that's why if either if statement is not satisfied, (and they cannot both be satisfied since they are different) the colour choice of the last else criteria is applied, and not what you wanted.


generally to trouble shoot.
For sure I'd follow what Joe said and just print out the stuff you retrieved from the table to make sure what is in the table, and what you are getting back are the same. (also bear in mind differences in the character encoding sets can make two characters that "look" the same "be" different. -and that should show up when you print out the results returned).


you can just add it into the stuff you;re printing like this:
Code:
 if (($row['ad_type']) == 'sidebar'){
                      $calendar.='<div class="event" style="background-color:#FFb2b2;">'.$event['title']. '(' . $row['ad_type'] .')</div>';
                                                    }
                    elseif (($row['ad_type']) == '728x90') {
                      $calendar.='<div class="event" style="background-color:#b2b2ff;">'.$event['title']. '(' . $row['ad_type'] .')</div>';
                                                            }
                        else {
                      $calendar.='<div class="event" style="background-color:#ff99ff;">'.$event['title']. '(' . $row['ad_type'] .')</div>';
                            }
something else...

personally I hate, if, else if, else nesting like that. especially inline with the stuff that you're actually trying to print out.

I'd prefer either to do all checking up front, and then you have everything setup before printing anything to screen.

so (either) set a variable to nothing, then change it, and if at the end it is not changed, then set to a default.
Code:
$format = "0";
if ($var = 1) { $format = #FFb2b2; }
if ($var = 2) { $format = #b2b2ff; }
if ($format = "0") { $format = #ff99ff; }
or start off wither it set to whatever the default is, and then change it if some condition is true.
Code:
$format = "#ff99ff";
if ($var = 1) { $format = #FFb2b2; }
if ($var = 2) { $format = #b2b2ff; }
then you're just doing
Code:
$row['ad_type']) == 'sidebar'){
                      $calendar.='<div class="event" style="background-color: . ' $format . ';">'.$event['title']. '(' . $row['ad_type'] .')</div>';
rather than all that nested if stuff. when it comes to the actual printing to screen.

or, (more preferably) use a switch/case statement.

PHP: switch - Manual




Technician, if you cannot help, and do not know the subject area, then just don't comment.
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 02-06-2017, 09:08 PM   #11
Baseband Member
 
Join Date: Jul 2014
Location: USA
Posts: 29
Default Re: Help With PHP Calender Code

Hi Root,

Thanks for your reply to this, i think with what everything you said i should be able to get it working. Thank you especially for the examples in the code, so grateful. The troubleshooting with the "ad_type" is really key i know.

Yes, using the if, else if, else statement there i know wasn't the best method, but it was the only thing i could think of at the time to get it to do what i wanted. I will follow your instructions on this as you're right on when you said:

[QUOTE]I'd prefer either to do all checking up front, and then you have everything setup before printing anything to screen.[QUOTE]

For this to work right, i need to follow that guideline. Thank you again
__________________
megan1989 is offline   Reply With Quote
Old 02-07-2017, 03:07 AM   #12
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,107
Default Re: Help With PHP Calender Code

Quote:
Originally Posted by megan1989 View Post

Quote:
I'd prefer either to do all checking up front, and then you have everything setup before printing anything to screen.
For this to work right, i need to follow that guideline. Thank you again
That's just a style guide line, (switch statements are easier to read than nested if statements.) it shouldn't affect whether it works. all it would do is make it obvious where you figure out the bulk of your "what colour will appointments be" vs the actual displaying of appointments, basically just separating out the logic stuff from the presentation stuff.

If you find it easier to read the other way around then keep doing what you are doing.

Each approach has its good/bad sides, plenty of people hate that figure out all the logic at the top approach as it separates logic from where it is used, and means that they are constantly scrolling up and down to find where variables are set vs what they do.
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 02-08-2017, 02:04 AM   #13
BSOD
 
Join Date: Feb 2016
Location: US
Posts: 963
Default Re: Help With PHP Calender Code

Quote:
Originally Posted by root View Post
I suspect that the issue is:
if, else if , else should be if, else if, else if where you are sorting out your formatting.
(and that's why if either if statement is not satisfied, (and they cannot both be satisfied since they are different) the colour choice of the last else criteria is applied, and not what you wanted.


generally to trouble shoot.
For sure I'd follow what Joe said and just print out the stuff you retrieved from the table to make sure what is in the table, and what you are getting back are the same. (also bear in mind differences in the character encoding sets can make two characters that "look" the same "be" different. -and that should show up when you print out the results returned).


you can just add it into the stuff you;re printing like this:
Code:
 if (($row['ad_type']) == 'sidebar'){
                      $calendar.='<div class="event" style="background-color:#FFb2b2;">'.$event['title']. '(' . $row['ad_type'] .')</div>';
                                                    }
                    elseif (($row['ad_type']) == '728x90') {
                      $calendar.='<div class="event" style="background-color:#b2b2ff;">'.$event['title']. '(' . $row['ad_type'] .')</div>';
                                                            }
                        else {
                      $calendar.='<div class="event" style="background-color:#ff99ff;">'.$event['title']. '(' . $row['ad_type'] .')</div>';
                            }
something else...

personally I hate, if, else if, else nesting like that. especially inline with the stuff that you're actually trying to print out.

I'd prefer either to do all checking up front, and then you have everything setup before printing anything to screen.

so (either) set a variable to nothing, then change it, and if at the end it is not changed, then set to a default.
Code:
$format = "0";
if ($var = 1) { $format = #FFb2b2; }
if ($var = 2) { $format = #b2b2ff; }
if ($format = "0") { $format = #ff99ff; }
or start off wither it set to whatever the default is, and then change it if some condition is true.
Code:
$format = "#ff99ff";
if ($var = 1) { $format = #FFb2b2; }
if ($var = 2) { $format = #b2b2ff; }
then you're just doing
Code:
$row['ad_type']) == 'sidebar'){
                      $calendar.='<div class="event" style="background-color: . ' $format . ';">'.$event['title']. '(' . $row['ad_type'] .')</div>';
rather than all that nested if stuff. when it comes to the actual printing to screen.

or, (more preferably) use a switch/case statement.

PHP: switch - Manual




Technician, if you cannot help, and do not know the subject area, then just don't comment.
Where did you read that I wrote that I did not understand the subject area? Lol I have been coding is various languages since 82.
Maybe you shouldn't assume so much. When you assume you make as ASS out of U and ME.
I could have given an answer but I don't think it's ethical to do a kid's homework for them.
__________________
Technician is offline   Reply With Quote
Old 02-09-2017, 04:37 AM   #14
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,107
Default Re: Help With PHP Calender Code

Quote:
Originally Posted by Technician View Post
Where did you read that I wrote that I did not understand the subject area? Lol I have been coding is various languages since 82.
Maybe you shouldn't assume so much. When you assume you make as ASS out of U and ME.
I could have given an answer but I don't think it's ethical to do a kid's homework for them.
Maybe I should have said or instead of and.

Lets say that you have a habit of derailing some threads with bad information where it is clear you don't know what you're talking about, but keep talking anyway.

Lets put it this way:

If you do not want to help someone then don't, but don't tell them to go elsewhere; just don't post.

I find it questionable whether it is more or less ethical to help someone with their homework here, or tell them that there are better places to get their homework done for them. - Basically, if you wanted someone to do their own homework, some advice like joe said printing out variable doing debug steps through the program is far more useful than saying, go ask the same question on a specialist forum, or go buy a product...

so again, either offer some help, or don't.

if you cannot help, whether that is because you don't know the subject area, or wonder whether the poster is a child that needs help with homework and your ethics are too strong to help them then just don't write anything...


This is supposed to be a place where people can ask for help.
whilst nobody can force you to help with the correct answer, it's not a terrible request to say that if you don't want to write a helpful answer, then don't write anything.

And often it is more difficult to offer help to someone whilst other people take the thread all over the place but post nothing of substance.


As far as making an ass of myself, (maybe so!) but, I checked your post history, you've seemingly never helped anyone with any issue in the programming sub forum in the year you've been here.
all I'm going to say is: on the internet talk is cheap. whether or not you say you have been coding since before I was born is irrelevant, put up, or shut up.
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 02-09-2017, 11:02 AM   #15
BSOD
 
Join Date: Feb 2016
Location: US
Posts: 963
Default Re: Help With PHP Calender Code

Maybe you should.
You are completely wrong in saying I told them to go elsewhere. Read or don't, but don't misread. AND certainly don't misquote me. My advice was to try different methods and learn it better themselves. My second post made the observation that a php forum might back a wiser choice...meaning that they could learn how to do it themselves instead of being handed one possible answer that was obviously not the best possible one.
So, in point of fact, I did offer help. You just didn't like the help I offered and that is irrelevant as I was not helping you but the OP.
Sometimes it's best to comment on the original post and not comment on others comments to that post just to stay on topic.
Anyhow I am glad they got the answer and sorry they didn't learn how to do it better than that themselves. There is a much more elegant and simple method they could have figured out of someone didn't feel like this was a place to show off but didn't fully understand the best possible way to solve the problem. They were led down the garden path as it were and diverted from learning the best possible method.

To paraphrase you, let's put it this way: try teaching the person to fish rather than just giving them a small underfed mackerel because that's all you know how to catch.
They just might learn to catch a bigger fish than you can.
__________________
Technician is offline   Reply With Quote
Old 02-09-2017, 12:12 PM   #16
Fully Optimized
 
crazyman143's Avatar
 
Join Date: May 2004
Location: USA
Posts: 2,963
Default Re: Help With PHP Calender Code

.
__________________
crazyman143 is offline   Reply With Quote
Old 02-09-2017, 03:16 PM   #17
BSOD
 
Join Date: Feb 2016
Location: US
Posts: 963
Default Re: Help With PHP Calender Code

Lol put away the popcorn, I'll not be revisiting this dead horse of a thread.��
__________________
Technician is offline   Reply With Quote
Old 02-09-2017, 07:55 PM   #18
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,107
Default Re: Help With PHP Calender Code

Still waiting for you to show the best possible method...

As I said: put up, or shut up.

Since you won't shut up, put your money where your mouth is, prove you know best, how should it be done?


All you've done so far is prove you know how to talk a lot of air with no substance behind it, then stamp your feet cry and finally run away when asked to prove yourself.
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 02-28-2017, 04:26 PM   #19
Beta Member
 
Join Date: Feb 2017
Location: UK
Posts: 1
Default Re: Help With PHP Calender Code

Have you tried the echo command to show the value of the variable on screen just before the IF statement to see what the value actually is?
__________________

__________________
crghsam is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 06:47 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0
×