user2854563
user2854563

Reputation: 268

Giving a CSS Class to Echoing Strings

I've a PHP file which has numerous echo statements for various checks like this;

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;
}

I want to give a similar CSS class to each echo statement. I tried this;

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

and it works, but my php file has dozens of echo's and I don't want to edit each and every echo statement. Is there any easy way to achieve this?

Upvotes: 0

Views: 353

Answers (5)

Imran Omer
Imran Omer

Reputation: 701

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.

Upvotes: 1

Manolo
Manolo

Reputation: 26350

Do you mean something like this?

$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;

Upvotes: 0

MrCode
MrCode

Reputation: 64526

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");

Upvotes: 2

revo
revo

Reputation: 48711

Try to change each echo to a fixed variable name:

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";
}

Then write a function for giving style to the out put:

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

Then you can use stylizing($msg, "class_name"); where you want to print out the result.

Upvotes: 0

A. Mo
A. Mo

Reputation: 142

There's no there way, unless you define once css class and put all your echo statements inside it.

<div class = "name">
<?php

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

</div>

Upvotes: 0

Related Questions