dongxiegao3071 2017-03-29 13:59
浏览 70
已采纳

如何使用类为echo内部的元素添加样式效果?

i have a problem with adding style effect for div elements inside of echo ,here's my code :

<?php
$db= new PDO('mysql:host=localhost;dbname=myDataBase;charset=utf8','user','password');

$response=$db->query('select * from users order by ID desc limit 10');
while($line=$reponse->fetch())
{
    echo 
        '<div class="elements" ><h1>'.$line['pseudo'].'</h1></div>
        <div class="elements">'.$line['message'].'</div>
        </div>';

} ?>

and the stylesheet :

.elements{
display: inline-block;
padding: 10px;
    }

when i execute it,it doesn't work

thanks for your collaboration

  • 写回答

1条回答 默认 最新

  • dsfgdsjfd78773 2017-03-29 14:31
    关注

    This is pretty easy. Just inline your php as needed into the html code to display on the page. For Example:

    <?php
    
    Inlcude your php and queries here in this block 
    
    
    ?>
    
    <!DOCTYPE html>
        <html>
    
        <head>
            <title>Test Page</title>
            <link rel="stylesheet" href="css/styles.css" />
        </head>
    
        <body>
            <?php echo '<div class="elements">the rest of your php code goes here</div> '; ?>
        </body>
    
    </html>
    

    This is just bare bones basic code to show you what I am talking about. So your custom css would live in the styles.css file. Once you echo out what it is you want in php, then it will inherit those styles. You can inline php as much as you want throughout the file. Just name it file.php or whatever you want.

    This will allow you to do what it is you are asking. Of course you could reverse this and echo out the html from start to finish if you really wanted too, but it would be easier to just add the relevant connection info for php and query at the top of the page before any html and then echo out the specific information you wish inside the html below the main php code.

    Also I noticed you have one too many closing div tags at the bottom of your echo statement. As others have already commented, you have some other errors as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能