views:

28

answers:

2

I have a working pagination script, it displays the data with few issues.

However, the issue I'm having is trying to get my data to enclose it in quotes if it's not null.

This is the part of my pagination script:

//This function shows the data
public function display()
{
    if(date('Y-m-d') == date('Y-m-d', $this->airdate)) $dateFormat = 'g:ia';
    elseif(date('Y') == date('Y', $this->airdate)) $dateFormat = 'F jS - g:ia';
    else $dateFormat = 'F jS, Y - g:ia';

    echo '<tr>'."\n".
         '  <td><strong>'.$this->program.'</strong></td>'."\n".
         '  <td>showing on '.$this->channel.'</td>'."\n".
         '  <td>'.date($dateFormat, $this->airdate).'</td>'."\n".
         '  <td><b>'.$this->episode.'</b></td>'. "\n".
         '  <td>'.$this->setReminder.'</td>'."\n".
         '</tr>'."\n";
}

However, it's the $this->episode. part which I'm having trouble with.

The data renders correctly:

Episode Name
<null>
Episode Name 2

but I would like it to be like this:

"Episode Name"
<null>
"Episode Name 2"

I tried:

        echo '<tr>'."\n".
         '  <td><strong>'.$this->program.'</strong></td>'."\n".
         '  <td>showing on '.$this->channel.'</td>'."\n".
         '  <td>'.date($dateFormat, $this->airdate).'</td>'."\n".
         '  <td><b>"'.$this->episode.'"</b></td>'. "\n".
         '  <td>'.$this->setReminder.'</td>'."\n".
         '</tr>'."\n";
}

but the formatting came out as:

"Episode Name"
"<null>"
"Episode Name 2"

which wasn't how I expected it to turnout.

I'm not sure what the right solution is - is ifelse the best one, and if so what code would you recommend for this problem?

+1  A: 

You could pass the value through a function, which would add the quotes where necessary.

function encloseNullWithQuotes($a)
{
    if ($a == "<null>")
        return $a;
    return '"'.$a.'"';
}

Then, change the $this->episode in your code to encloseNullWithQuotes($this->episode).

Not entirely sure whether the episode is the literal string <null> or not... if it's not, you could do a test for NULL or anything like that in the function too.

Richard Fawcett
The <null> represents an empty space on-screen, hope that clarifies it for you.
whitstone86
It worked, thanks!
whitstone86
That's great. I'd be grateful if you could mark the answer as accepted :)
Richard Fawcett
A: 

before the echo do

$this->episode = ($this->episode == '<null>')? $this->episode : '"' . $this->episode . '"';

and then in the echo do

'  <td><b>'.$this->episode.'</b></td>'. "\n".
Crayon Violent