PNMorrison Posted January 11, 2016 Share Posted January 11, 2016 I have the following code in my view file for an event block:- =============================================== <div class="content_wrap white events"><h5>Events</h5><p class="alpha"><strong><?php echo Loader::helper('truncate')->softTrim($title, 50); ?></strong></p><div class="event_details"><time><?php echo $date;?></time><address><?php echo $venue;?></address></div><a href="<?php echo $path;?>" class="beta absolute">Book your free place </a></div> ================================================= Application logic causes this to display a banner on my site with date of 'January 1st 1970' when there are no events set up on the web (when all events have expired.) I need this to display something else (or nothing at all) when there are no events published. ie IF database query returns 0 (or null) { DO SOMETHING DIFFERENT} ELSE DO THE BELOW <div class="content_wrap white events"> <h5>Events</h5><p class="alpha"><strong><?php echo Loader::helper('truncate')->softTrim($title, 50); ?></strong></p><div class="event_details"><time><?php echo $date;?></time><address><?php echo $venue;?></address></div><a href="<?php echo $path;?>" class="beta absolute">Book your free place </a></div> Any help is greatly appreciated. Or even knowing what exactly i need to Google (being a complete novice)... Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/ Share on other sites More sharing options...
Muddy_Funster Posted January 11, 2016 Share Posted January 11, 2016 Hi and welcome to PHPFreaks, could you please use code bb tags (open with code wrapped in square brackets and close with /code - also wrapped in square brackets, or highlight the code once it is in your post and click on the |< >| icon in the toolbar of the post box) when posting your code in the future. We will need to see the current control script for processing the DB results to provide you a response relevant to your current code. Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529425 Share on other sites More sharing options...
Barand Posted January 11, 2016 Share Posted January 11, 2016 "Jan 1st 1970" is unix day 0 and is often the result of trying to process an invalid date format Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529430 Share on other sites More sharing options...
PNMorrison Posted January 11, 2016 Author Share Posted January 11, 2016 Thanks Muddy_Funster Could this be the control script u refer to:- public function getMetaData($pageId){ $page = Page::getByID($pageId); if($page) { $data = array( 'Title' => $page->getCollectionName(), 'Description' => $page->getCollectionDescription(), 'Background_Image' => $this->getImage(), 'Path' => BASE_URL . View::url($page->getCollectionPath()), 'Venue' => $page->getAttribute('event_venue'), 'Date' => $page->getAttribute('event_date') ); return $data; } } Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529431 Share on other sites More sharing options...
Muddy_Funster Posted January 11, 2016 Share Posted January 11, 2016 Not exactly what I was looking for, I was looking for the code that handles the database query and results. Although you could apply some logic to the function (which looks more like a single method from within a larger object, but I could be wrong) that you have posted, it wouldn't be the best approach. Can you post up the code that handles the db transaction that you are using to check for events? Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529433 Share on other sites More sharing options...
PNMorrison Posted January 11, 2016 Author Share Posted January 11, 2016 The website is a CMS written in php. The filesystem seems very convoluted.. Below is the bit of php code in the page template.. But I suspect this is not wot u mean either.. $block = Block::getByName('Event'); $block->display(); Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529439 Share on other sites More sharing options...
Muddy_Funster Posted January 11, 2016 Share Posted January 11, 2016 what CMS is it? Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529441 Share on other sites More sharing options...
PNMorrison Posted January 12, 2016 Author Share Posted January 12, 2016 Concrete5 I'm using pal Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529509 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2016 Share Posted January 12, 2016 ok, can you post up the full view file that you took the original code snippet from in the first post, the documentation on the proEvents for concrete5 is all but useless. Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529512 Share on other sites More sharing options...
PNMorrison Posted January 13, 2016 Author Share Posted January 13, 2016 Hi mate that was pretty much the full contents of the file (view.php) <?php defined('C5_EXECUTE') or die("Access Denied."); //echo $description; //echo $backgroundImage->src; //echo $background_color ?> <div class="content_wrap white events"> <h5>Events</h5> <p class="alpha"><strong><?php echo Loader::helper('truncate')->softTrim($title, 50); ?></strong></p> <div class="event_details"> <time><?php echo $date;?></time> <address><?php echo $venue;?></address> </div> <a href="<?php echo $path;?>" class="beta absolute">Book your free place </a> </div> It is called from the page-type below (i have commented out the call in bold as to prevent displaying on the site) - scroll to bottom.. <?php defined('C5_EXECUTE') or die("Access Denied."); //Page Name $c->getCollectionName(); //Event Venue Date Attribute if($c->getAttribute('event_date')) { $event_date = strftime(" %d %B %Y",strtotime($c->getAttribute('event_date'))); }else{ $event_date = 'Please supply the event date in custom attributes'; } //Download Attribute $download = $c->getAttribute('download'); $path = File::getRelativePathFromID($download->pdfFile); ?> <div id="page_wrap" class="container sub_page event_p"> <div class="breadcrumb"> <div class="breadcrumb_container"> <nav> <ul> <?php $blk = Block::getByName('Breadcrumb'); $blk->display(); ?> </ul> </nav> </div> </div> <div class="wrap"> <header class="row"> <div class="col span_6 info_header"> <h1><?php echo $c->getCollectionName() ?></h1> <time><?php echo $event_date;?></time> <?php if($c->getAttribute('event_venue')) { echo '<address>'; echo $c->getAttribute('event_venue'); echo '</address>'; }?> </div> </header> <div class="row"> <div class="main main_left col span_8"> <div class="content_wrap"> <a href="<?php echo View::url('/news-events/events/') ?>" class="cta_back beta col">Back to all events</a> <section class="col"> <?php if($c->getCollectionDescription()){?><p class="intro"><?php echo $c->getCollectionDescription() ?></p><?php }?> <?php $a = new Area('Main'); $a->display($c); ?> <?php if($download->label) { ?><a href="<?php echo $path;?>" class="cta download" download="Download File" target="_blank"><?php echo $download->label; ?></a><?php }?> </section> <hr /> <?php if($event_venue){?> <h3 class="gamma">Location</h3> <address><?php echo $event_venue;?></address> <?php }?> <?php if($c->getAttribute('archive')); elseif($c->getAttribute('full_booked')) { echo '<p style="padding: 10px 20px; color:#fff; background:#005f94; float:left; margin-top:20px;">This event is fully booked</p>'; }elseif($c->getAttribute('by_invite')) { echo '<p style="padding: 10px 20px; color:#fff; background:#005f94; float:left; margin-top:20px;">This event is by invitation only</p>'; }else{ $block = Block::getByName('Event Form'); if(is_object($block)) $block->display(); }?> <a href="<?php echo View::url('/news-events/events/') ?>" class="cta_back beta">Back to all events</a> </div> </section> </div> <?php $a = new GlobalArea('Events Sidebar'); $a->setBlockWrapperStart('<div class="col span_4 sidebar">'); $a->setBlockWrapperEnd('</div>'); $a->display(); ?> <div class="col span_4 sidebar grid"> <?php /*$block = Block::getByName('Event'); $block->display();*/ ?> </div> </div> <?php $this->inc('elements/partner.php'); ?> </div> thank for looking at this - i really do appreciate it. And i'm learning a great deal. Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529581 Share on other sites More sharing options...
Muddy_Funster Posted January 14, 2016 Share Posted January 14, 2016 alright, this is making my head spin and taking way longer than it really should. I've added a little bit of code that probably won't work as is because I do very little with dates in PHP but should limit the display of the events div to only happen when there is a valid date (one after the 2nd of January 1970 - so I hope you're not planning an historical site or anything to do with flux-capacitor's) <?php defined('C5_EXECUTE') or die("Access Denied."); //echo $description; //echo $backgroundImage->src; //echo $background_color $pDate = DateTime::createFromFormat("F dS Y", $date); $cDate = DateTime::createFromFormat("F dS Y", 'January 2nd 1970'); if($cDate > $pDate){ ?> <div class="content_wrap white events"> <h5>Events</h5> <p class="alpha"><strong><?php echo Loader::helper('truncate')->softTrim($title, 50); ?></strong></p> <div class="event_details"> <time><?php echo $date;?></time> <address><?php echo $venue;?></address> </div> <a href="<?php echo $path;?>" class="beta absolute">Book your free place </a> </div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529625 Share on other sites More sharing options...
Barand Posted January 14, 2016 Share Posted January 14, 2016 You cannot compare dates in "January 2nd 1970" format. For example, April 30th 2015 is less than the above date. You have to use yyyy-mm-dd format to correctly complare. Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529630 Share on other sites More sharing options...
Muddy_Funster Posted January 14, 2016 Share Posted January 14, 2016 You cannot compare dates in "January 2nd 1970" format. For example, April 30th 2015 is less than the above date. You have to use yyyy-mm-dd format to correctly complare. I guessed I'd miss the mark, like I said I don't do much with dates in PHP so didn't know that creating the date object from string format wouldn't convert the original formatting as well. I just assumed it would because it seemed like the most likely reason to do it at all. My apologies. Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529632 Share on other sites More sharing options...
Barand Posted January 14, 2016 Share Posted January 14, 2016 (edited) My apologies to you, creating from format will create a usable date object. Put it down to my early morning low caffeine levels. (I guess my comment is still a valid warning not to format dates until final output. Work with yyy-mm-dd.) Edited January 14, 2016 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529637 Share on other sites More sharing options...
PNMorrison Posted January 15, 2016 Author Share Posted January 15, 2016 Super! This works great. It works using both formats for the date object. If i use the 'Y m d' format as advised however, how do i then i display the date on the banner in 'F dS Y' format still? Thanks for all ur help guys - i really appreciate it. I will be starting a course soon, so hopefully one day i can pay it forward.. Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529788 Share on other sites More sharing options...
Muddy_Funster Posted January 15, 2016 Share Posted January 15, 2016 the original date string should be just as it always was, you are only using the date objects to check against each other, nothing more, the original output formatting should be unaffected, unless you are overwriting the original date variable, which you can't be doing unless the Date class has the __string() Magic Method in it (which I don't think it does). So it shouldn't matter to your displayed date. Barand said that as long as you are using actual date objects the string format does not matter, that only matters if doing a string comparison as it works left to right by ascii value, so April comes before January (as A is a lower ascii value than J) thus you would need to have the date in the order of Y m d for the correct order to be achieved. Dates can be tricky to get right - especially when going between MySQL, PHP and Javascript. That's why I try to do all my date and ordering work in MySQL and then just push the string value through (hance my lack of experience in PHP date work) Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529792 Share on other sites More sharing options...
PNMorrison Posted January 15, 2016 Author Share Posted January 15, 2016 Fantastic! Understood. Thanks again pal. U have been a massive help!! Quote Link to comment https://forums.phpfreaks.com/topic/300270-please-help-with-database-query-php-beginner/#findComment-1529794 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.