为PHP脚本(页面)添加视觉样式


Adding visual style to PHP scripts(pages)

用户到达以下php脚本,并设置电子邮件是否已被确认。

此时,用户在浏览器中唯一能看到的是php echo打印的一条非常简单的消息。

我希望它在视觉上看起来更有趣。让这个回声成为一个适当样式的HTML页面的一部分,包括标题、字体样式、签名、图像……

考虑到我的脚本有断点,最好的方法是什么?因为我以前从未这样做过,所以我不确定什么是最好的起点。

下面是我基于答案的代码更新。希望对其他人有所帮助php新用户

<?php
require("../db/MySQLDAO.php");
require ("../Classes/EmailConfirmation.php");
$config = parse_ini_file('../db/SwiftApp.ini');
//host access data
$dbhost = trim($config["dbhost"]);
$dbuser = trim($config["dbuser"]);
$dbpassword = trim($config["dbpassword"]);
$dbname = trim($config["dbname"]);
// receive token data
$emailToken = htmlentities($_GET["token"]);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title here</title>
<style>
/* -------------------------------------
        GLOBAL
------------------------------------- */
* {
    margin: 0;
    padding: 0;
    font-family: "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;
    font-size: 100%;
    line-height: 1.6;
}
img {
    max-width: 100%;
}
body {
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: none;
    width: 100%!important;
    height: 100%;
}
/* -------------------------------------
        ELEMENTS
------------------------------------- */
a {
    color: #348eda;
}
.btn-primary {
    text-decoration: none;
    color: #FFF;
    background-color: #348eda;
    border: solid #348eda;
    border-width: 10px 20px;
    line-height: 2;
    font-weight: bold;
    margin-right: 10px;
    text-align: center;
    cursor: pointer;
    display: inline-block;
    border-radius: 25px;
}
.btn-secondary {
    text-decoration: none;
    color: #FFF;
    background-color: #aaa;
    border: solid #aaa;
    border-width: 10px 20px;
    line-height: 2;
    font-weight: bold;
    margin-right: 10px;
    text-align: center;
    cursor: pointer;
    display: inline-block;
    border-radius: 25px;
}
.last {
    margin-bottom: 0;
}
.first {
    margin-top: 0;
}
.padding {
    padding: 10px 0;
}
/* -------------------------------------
        BODY
------------------------------------- */
table.body-wrap {
    width: 100%;
    padding: 20px;
}
table.body-wrap .container {
    border: 1px solid #f0f0f0;
}
/* -------------------------------------
        FOOTER
------------------------------------- */
table.footer-wrap {
    width: 100%;    
    clear: both!important;
}
.footer-wrap .container p {
    font-size: 12px;
    color: #666;
}
table.footer-wrap a {
    color: #999;
}
/* -------------------------------------
        TYPOGRAPHY
------------------------------------- */
h1, h2, h3 {
    font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
    color: #d63480;
    margin: 40px 0 10px;
    line-height: 1.2;
    font-weight: 200;
}
h1 {
    font-size: 36px;
}
h2 {
    font-size: 28px;
}
h3 {
    font-size: 22px;
}
p, ul, ol {
    margin-bottom: 10px;
    font-weight: normal;
    font-size: 14px;
}
ul li, ol li {
    margin-left: 5px;
    list-style-position: inside;
}
/* ---------------------------------------------------
        RESPONSIVENESS
------------------------------------------------------ */
/* Set a max-width, and make it display as block so it will automatically stretch to that width, but will also shrink down on a phone or something */
.container {
    display: block!important;
    max-width: 600px!important;
    margin: 0 auto!important; /* makes it centered */
    clear: both!important;
}
/* Set the padding on the td rather than the div for Outlook compatibility */
.body-wrap .container {
    padding: 20px;
}
/* This should also be a block element, so that it will fill 100% of the .container */
.content {
    max-width: 600px;
    margin: 0 auto;
    display: block;
}
/* Let's make sure tables in the content area are 100% wide */
.content table {
    width: 100%;
}
</style>
</head>
<body bgcolor="#f6f6f6">
<!-- body -->
<table class="body-wrap" bgcolor="#f6f6f6">
    <tr>
        <td></td>
        <td class="container" bgcolor="#FFFFFF">
            <!-- content -->
            <div class="content">
            <table>
                <tr>
                    <td>
                        <h1>Title</h1>                      
                        <table>
                            <tr>
                                <td class="padding">
                                    <p>
                                        <?php
                                        if(empty($emailToken))  {
                                            echo "<h2>Sorry, something went wrong...</h2>";
                                            echo "<p>Unfortunately your email validation token has expired.</p>";
                                            echo "<p>Please get in contact with us at <a href=mailto:></a></p>";
                                        }
                                        else{
                                            //open server connection
                                            $dao = new MySQLDAO($dbhost, $dbuser, $dbpassword, $dbname);
                                            $dao->openConnection();
                                            //creates user 
                                            $user_id = $dao->getUserIdWithToken($emailToken);
                                            if(empty($user_id))
                                            {
                                                echo "<h2>Sorry, something went wrong...</h2>";
                                                echo "<p>We could not find an user associated with the email you provided.</p>";
                                                echo "<p>Please get in contact with us at <a href></a></p>";
                                            }
                                            else{
                                                $result = $dao->setEmailConfirmedStatus(1, $user_id);
                                                if($result)
                                                { 
                                                  echo "<h2>Thank you! Your email is now confirmed!<h2>";
                                                  $dao->deleteUsedToken($emailToken);  
                                                }
                                            }
                                            $dao->closeConnection();
                                        }
                                        ?>

                                    </p>
                                </td>
                            </tr>
                        </table>
                        <p class="padding"></p>
                        <p>Thanks,</p>
                        <p>Title team</p>
                        <p class="padding"></p>
                    </td>
                </tr>
            </table>
            </div>
            <!-- /content -->
        </td>
    </tr>
</table>
<!-- /body -->
</body>
</html>

使用HTML构建内容,使用CSS格式化内容。

你可以回显HTML和CSS与你的字符串。

这些链接应该会让你朝着正确的方向前进。

更新以容纳注释

有很多方法,但一个简单的例子可能适合您的情况是这样的:

if语句中的echo替换为includerequire

我们称该文件为template.php。这个文件不需要以<?php开始,以?>结束。PHP可以用HTML输入和输出。所以template.php可能看起来像这样:

<!DOCTYPE html>
<html>
    <head>
        <meta name="description" content="" />
        <meta name="keywords" content="" />
        <meta charset="UTF-8">
        <title></title>
        <link href="css/styles.css" rel="stylesheet" />
    </head>
    <body>
        <div class="some_style"><?php
            echo 'something';
        ?></div>
        <div class="some_style2"><?php
            echo $some_var;
        ?></div>
    </body>
</html>

如果这是要在电子邮件中发送,CSS在电子邮件中是不支持的,所以你需要保持样式,你可以用简单的HTML标签和图像。

将echo设置为:

echo '<div id="message">User with this email token is not found</div>';

然后用css样式#message

希望有帮助!

呃…丑陋的HTML在php。有几种方法可以在脚本中包含html而不回显它。

有一条老路。

// out of php
?>
<div>
<?php echo $content; ?>
</div>
<?
// back in.

或者您可以查看php/html的简写。您的代码将从中受益,因为它将更易于阅读,

最主要的原因是,除非迫不得已,否则不要让php解析html。

PHP

echo "<p id='token_message'>User with this email token is not found</p>";
CSS

#token_message {
    /* Styling Here */
}