ashton321 Posted October 26, 2009 Share Posted October 26, 2009 Hello I am trying to highlight the days on my calendar based on the dates that i have in my database. Currently I can only get it to display the last element in the database which leads me to believe that i have an error in my arrays <?php ## My database $query = mysql_query("SELECT * FROM speaking_engagements"); while($result = mysql_fetch_array($query)){ $dates = $result['date']; $dates = explode('-',$dates); $dates = $dates[2]; } #$dates = explode('-',$dates); #$dates = $dates[2]; #$numdays = count($dates); $time = time(); $today = date('j',$time); $days = array( $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'), $dates=>array('#'), 8=>array('#'), ## a hard coded one that works ); $today = getdate(); $year = $today['year']; $month = $today['mon']; echo '<div id="calendar">'; echo generate_calendar($year, $month, $days, 3, NULL, '/weblog/archive/2004/Jan'); echo '</div>'; ?> Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 27, 2009 Share Posted October 27, 2009 $dates = array(); $query = mysql_query("SELECT date_column FROM speaking_engagements"); while($result = mysql_fetch_array($query)){ $date = $result['date']; $date_parts = explode('-',$dates); $dates[] = $date_parts[2]; } You should never use SELECT * in an application. It's trouble, besides, it looks like you only need one column anyway. Why waste the resources? Replace "date_column" with the name of your date column. Quote Link to comment Share on other sites More sharing options...
ashton321 Posted October 27, 2009 Author Share Posted October 27, 2009 Yeah, there is no reason to SELECT * I did that when I was frustrated and couldn't get it to work. Now I have tried what you suggested and now I am not getting any results to show, not even the last. I tried to put it into the calendar array and there was no error, but i tried to echo $dates and it output Array and when I tried to output $dates[0] there was nothing. Here is the updated code i tried. <?php #database connection here $dates = array(); $query = mysql_query("SELECT date FROM speaking_engagements"); while($result = mysql_fetch_array($query)){ $date = $result['date']; $date_parts = explode('-',$dates); $dates[] = $date_parts[2]; } $time = time(); $today = date('j',$time); $days = array( $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'), $dates[0]=>array('#'), $dates[1]=>array('#'), $dates[2]=>array('#'), $dates[3]=>array('#'), 8=>array('#'), ); echo $dates[0]; $today = getdate(); $year = $today['year']; $month = $today['mon']; echo '<div id="calendar">'; echo generate_calendar($year, $month, $days, 3, NULL, '#'); echo '</div>'; ?> Quote Link to comment Share on other sites More sharing options...
scripterdx Posted October 27, 2009 Share Posted October 27, 2009 it was the same proble that i had Quote Link to comment Share on other sites More sharing options...
scripterdx Posted October 27, 2009 Share Posted October 27, 2009 the array when is filled? Quote Link to comment Share on other sites More sharing options...
knsito Posted October 27, 2009 Share Posted October 27, 2009 try $query = mysql_query("SELECT `date` FROM speaking_engagements"); [added accent] Are you sure you are getting dates in format you are expecting? Quote Link to comment Share on other sites More sharing options...
DavidAM Posted October 27, 2009 Share Posted October 27, 2009 You could just use DayOfMonth(`date`) in the SQL to get the day number instead of picking it apart in PHP: SELECT DayOfMonth(`date`) AS CalDay FROM speaking_engagements Note: Are the back-ticks still necessary? I think so, but I'm not sure. You should always avoid using reserved words for column names, table names, database names, etc. It just makes things harder on you when accessing the table. Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 28, 2009 Share Posted October 28, 2009 It could be that "date" is a mysql keyword? Did you try the backticks like kinsito said? Try echoing mysql_error()? Quote Link to comment Share on other sites More sharing options...
ashton321 Posted October 29, 2009 Author Share Posted October 29, 2009 Found the problem now! <?php $dates = array(); $query = mysql_query("SELECT date FROM speaking_engagements"); while($result = mysql_fetch_array($query)){ $date = $result['date']; $date_parts = explode('-',$dates); $dates[] = $date_parts[2]; } ?> is now <?php $dates = array(); $query = mysql_query("SELECT eventdate FROM speaking_engagements") or die(mysql_error()); while($result = mysql_fetch_array($query)){ $date = $result['eventdate']; $date_parts = explode('-',$date); $dates[] = $date_parts[2]; }?> I changed the column name in the database from the keyword date and that was not the issue. Then I look at what I was exploding, and saw that i was exploding dates with an "s" when it needed to be without the "s". Thanks for all the help with the arrays Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 29, 2009 Share Posted October 29, 2009 my mistake!! it must have had you going nuts. glad you got it sorted. Quote Link to comment Share on other sites More sharing options...
ashton321 Posted October 29, 2009 Author Share Posted October 29, 2009 It has been driving me nuts for a few days now! But, now that I got my data out, I need to make 01 for a day turn into 1 but I donk know how to run like and if statement or something on the entire array because I will not know its position in the array? Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 29, 2009 Share Posted October 29, 2009 It has been driving me nuts for a few days now! But, now that I got my data out, I need to make 01 for a day turn into 1 but I donk know how to run like and if statement or something on the entire array because I will not know its position in the array? You can either use ltrim: $number = ltrim($number, '0'); Or you can just cast your $number variable as (int) and the zero will be removed. Your choice. Quote Link to comment Share on other sites More sharing options...
ashton321 Posted October 29, 2009 Author Share Posted October 29, 2009 I am going to try the (int) cast because I am not sure if this is true but the other may remove the 0 from a day like 20 and make it 2? Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted October 29, 2009 Share Posted October 29, 2009 with an ltrim it won't take any zeros on the right of the string, so they would both work the same Quote Link to comment Share on other sites More sharing options...
ashton321 Posted October 29, 2009 Author Share Posted October 29, 2009 Sorry to be a pain here but im not that great with arrays. i tried doing the int cast and it wasnt working so I tried the ltrim and now it is echoing A r r a as my array rather than the numbers. <?php $dates = array(); $query = mysql_query("SELECT eventdate FROM speaking_engagements") or die(mysql_error()); while($result = mysql_fetch_array($query)){ $date = $result['eventdate']; $date_parts = explode('-',$date); $dates[] = $date_parts[2]; } $dates = ltrim($dates, '0'); $time = time(); $today = date('j',$time); $days = array( $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'), $dates[0]=>array('#'), $dates[1]=>array('#'), $dates[2]=>array('#'), $dates[3]=>array('#'), ); echo "$dates[0] "; echo "$dates[1] "; echo "$dates[2] "; echo "$dates[3] "; Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 29, 2009 Share Posted October 29, 2009 you cant apply it to an array like that. you can do a few things. while($result = mysql_fetch_array($query)){ $date = $result['eventdate']; $date_parts = explode('-',$date); $dates[] = ltrim($date_parts[2],'0'); } Or you can setup a foreach loop by reference afterwards to do it. Or you could even use array_map. Best case is above though, its the fastest. Quote Link to comment Share on other sites More sharing options...
ashton321 Posted October 29, 2009 Author Share Posted October 29, 2009 ok thank you it works! one last question is it possible to use some time of loop in <?php $days = array( $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'), $dates[0]=>array('#'), $dates[1]=>array('#'), $dates[2]=>array('#'), $dates[3]=>array('#'), );?> for when I add elemts to the database they all get displayed and i dont have to change the script? Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 29, 2009 Share Posted October 29, 2009 can you explain youre question a little bit better? Quote Link to comment Share on other sites More sharing options...
ashton321 Posted October 29, 2009 Author Share Posted October 29, 2009 I'll try. This is a list of days that are going to become links on a calendar once i get it to work. The calendar makes the link based on the <?php $days = array( $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'), $dates[0]=>array('#'), $dates[1]=>array('#'), $dates[2]=>array('#'), $dates[3]=>array('#'), );?> array. i would like to know if I am allowed to put like a for or while type loop inside the $days array to make every element of the $dates array show up. right now i had to add $dates[0]=>array('#'), $dates[1]=>array('#'), $dates[2]=>array('#'), $dates[3]=>array('#'), for each element. i would like it to be that if i add a fifth element to the database it also becomes a link on the calendar without me going in and adding $dates[4]=>array(#), Hope that is more clear Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 29, 2009 Share Posted October 29, 2009 <?php $days = array( $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'); foreach($dates as $key => $value) { $days[$key] = array('#'); } ?> Not tested. Quote Link to comment Share on other sites More sharing options...
ashton321 Posted October 29, 2009 Author Share Posted October 29, 2009 I dont have much experience with foreach loops and i got Parse error: syntax error, unexpected ';', expecting ')' in /homepages/44/d263144020/htdocs/webdev/inc/sidebar.php on line 15 when I tested $days = array( $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>'); foreach($dates as $key => $value) { $days[$key] = array('#'); }); Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 29, 2009 Share Posted October 29, 2009 <?php $days = array( $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>')); foreach($dates as $key => $value) { $days[$key] = array('#'); } ?> that should work Quote Link to comment Share on other sites More sharing options...
ashton321 Posted October 29, 2009 Author Share Posted October 29, 2009 It works without an error now, but it is only highlighting the first 3 days rather than the values Quote Link to comment Share on other sites More sharing options...
abazoskib Posted October 29, 2009 Share Posted October 29, 2009 ok one more time <?php $days = array( $today=>array(NULL,NULL,'<span style="color: red; font-weight: bold; font-size: larger;">'.$today.'</span>')); foreach($dates as $key => $value) { $days[$value] = array('#'); } ?> Quote Link to comment Share on other sites More sharing options...
ashton321 Posted October 29, 2009 Author Share Posted October 29, 2009 Thank you so much it works! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.