开发者

Problem with ifelse in PHP script

开发者 https://www.devze.com 2023-01-15 10:42 出处:网络
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.

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?


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号