0

我有一个 PHP 文件,其中包含用于各种检查的大量回显语句;

if ($power != "1")
{
    echo "Please contact administrator for assistance.";
    exit;
}

if (!$uid)
{
    echo "You do not have permissions to change your status.";
    exit;
}

if (!$mybb->input['custom'])
{
    echo "You've not added any status to change";
    exit;
}

我想为每个 echo 语句提供一个类似的 CSS 类。我试过这个;

if ($power != "1")
{
    echo "<div class='class_name'>Please contact administrator for assistance.</div>";
    exit;
}

它可以工作,但是我的 php 文件有几十个回声,我不想编辑每个回声语句。有没有简单的方法来实现这一点?

4

5 回答 5

2

You could define a function to handle outputting the message. You'll have to update the existing code but in the future, you'll be able to change the CSS class name or HTML structure easily be modifying the function.

class Response
{
    public static function output($message, $className = 'class_name')
    {
        echo "<div class='" . htmlspecialchars($className) . "'>" . $message. "</div>";
        exit;
    }
}

Usage:

if ($power != "1")
{
    Response::output("Please contact administrator for assistance.");
}

Override the class name:

Response::output("Please contact administrator for assistance.", "other_class");
于 2013-10-24T08:04:19.270 回答
1

If you're having issues/errors in above answers then here is my answer, I hope it helps;

Add the following code just above the <?php of your PHP file;

<style type="text/css">
    .error{
        background: #FFC6C6;
        color: #000;
        font-size: 13px;
        font-family: Tahoma;
        border: 1px solid #F58686;
        padding: 3px 5px;
    }
</style>

Next change each echo statement to something like this;

echo "<div class='error'>Write error code here.</div>";
exit;

You can easily find and replace the echo statements if you're using Notepad++

It should work. Also its somewhat similar to MrCode's answer however I think my answer is easily understandable and may be easy to implement.

于 2013-10-24T17:19:20.687 回答
0

没有办法,除非您定义一次 css 类并将所有 echo 语句放入其中。

<div class = "name">
<?php

echo ...
...
...
?>

</div>
于 2013-10-24T08:00:09.777 回答
0

尝试将每个更改echo为固定变量名称:

if ($power != "1")
{
    $msg = "Please contact administrator for assistance.";
}

if (!$uid)
{
    $msg = "You do not have permissions to change your status.";
}

if (!$mybb->input['custom'])
{
    $msg = "You've not added any status to change";
}

然后编写一个函数来赋予输出样式:

function stylizing($msg, $class="")
{
   if($class != "")
       $msg = "<div class='{$class}'>{$msg}</div>";
   echo $msg;
}

然后你可以stylizing($msg, "class_name");在你想打印结果的地方使用。

于 2013-10-24T08:03:18.050 回答
0

你的意思是这样的吗?

$message = '';

if ($power != "1") $message .= "<div class='one'>Please contact administrator for assistance.</div>";
elseif (!$uid) $message .= "<div class='two'>You do not have permissions to change your status.</div>";
elseif (!$mybb->input['custom']) $message .= "<div class='three'>You've not added any status to change.</div>";

echo $message;
于 2013-10-24T08:03:19.270 回答