Jump to content

Please help with database query -- PHP Beginner


PNMorrison

Recommended Posts

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)...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}
?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.