Page 1 of 1

if empty date function

Posted: Tue May 29, 2012 8:46 am
by joepiooo1988
Hi all,

I need some help:). I just started a few months ago with PHP and I started a project with a friend a while ago and now I'm stuck.

I'm creating a CMS system and I have a active function and Online and Offline function for the text on each page. But now I have to following problem. When I dont enter a online and offline date it stays empty and doesnt show anything.

Here is my Class:

Code: Select all

<?php
    public function getTeksten($database, $taal, $bladzijde = NULL, $actief = NULL, $id = NULL)
    {
        $sql = "SELECT * FROM ".$database."_teksten WHERE tekst_taal=:taal AND tekst_actief='1' AND tekst_online <= '".date('Y-m-d')."' AND tekst_offline >= '". date('Y-m-d')."'";
        if(!empty($id))
        {
            if(is_numeric($id))
            {
                $sql .= " AND tekst_ID=:id";
            }
            else
            {
                return NULL;
            }
        }
        else if(!empty($bladzijde))
        {
            if(is_numeric($bladzijde))
            {
                $sql .= " AND tekst_bladzijde=:bladzijde";
            }
            else
            {
                return NULL;
            }
        }
        $sql .= " ORDER BY tekst_volgorde ASC";
        try
        {
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(":taal", $taal, PDO::PARAM_STR);
            if(!empty($id) && is_numeric($id))
            {
                $stmt->bindParam(":id", $id, PDO::PARAM_INT);
            }
            else if(!empty($bladzijde) && is_numeric($bladzijde))
            {
                $stmt->bindParam(":bladzijde", $bladzijde, PDO::PARAM_INT);
            }
            $stmt->execute();
            $result = $stmt->fetchAll(PDO::FETCH_OBJ);
            $stmt->closeCursor();
            
            return $result;
        }
        catch(Exception $e)
        {
            die($e->getMessage());
        }
    }
?>
This is my content file:

Code: Select all

<?php
include_once "class/tekst.class.php";
$tekstclass = new tekst($dbo);
$bladzijde = ((isset($_GET['p']) && is_numeric($_GET['p'])) ? $_GET['p'] : 1);
$teksten = $tekstclass->getTeksten($website, 'nl', $bladzijde);

foreach($teksten as $tekst)
{
    if(!empty($tekst->tekst_img))
    {
?>
                    <div class="imgcontainer">
                        <div class="imgcontent">
                            <img src="<?php echo $tekst->tekst_img; ?>" class="content_image" alt="<?php echo $tekst->tekst_header; ?>" />
                        </div>
                    </div>
<?php
    }
?>
                    <h1><strong><?php echo $tekst->tekst_header; ?></strong></h1>
                    <?php echo $tekst->tekst_content."\n"; ?>
<?php
    if($bladzijde == 1)
    {
?>
                    <hr class="hrline"/>
<?php
    }
?>
                    <br style="clear:both;"/>
<?php
}[
?> 
In the database where I store the date the standard value is NULL.

Can anybody help me out please?

Tnx:)

Joep

Re: if empty date function

Posted: Tue May 29, 2012 2:09 pm
by social_experiment
joepiooo1988 wrote:When I dont enter a online and offline date it stays empty and doesnt show anything.
why don't you change the column to enter a default value like TIME_STAMP? Also, you should use 'false' if you want to make variables optional for a function

Code: Select all

<?php
 getTeksten($database, $taal, $bladzijde = false, $actief = false, $id = false)
?>
The Manual wrote: Casting to NULL
Casting a variable to null will remove the variable and unset its value.