在html标签内打印php元素

时间:2015-12-22 03:25:32

标签: php html sql database twitter-bootstrap

情况:我试图通过php变量从数据库中显示少量值。我使用bootstrap和XAMPP作为服务器。我想做什么是,获取当前日期并使用我从数据库获得的事件日期进行检查。如果它们都等于显示红色球内的日期。我试图在他们的网站中将其设为Mozilla Event Calendar站点

for ex-我在phpmyadmin内将事件日期更改为今天的日期(2015-12-22),是的,它们都是相同的,所以它必须在红色球内显示日期。它适用于我的测试场所,但不适用于红球。

问题:问题是它没有显示任何内容。我应该怎样做才能在html标签中显示php元素? enter image description here

我尝试了什么:(在红球内)

<div class="row LatestEventCircle">
  <div class="row container-fluid center-block text-center MainEventDate">
    <h2 class="center-block MainEventDate">
      <?php echo"$rows[$eventdate]"; ?><br>
      EVENT
    </h2>
  </div>
  <!--HERE IS THE PLACE-->
  <div class="row"></div>             
</div>

这是处理数据库部分和测试代码的php脚本:

<?php
mysql_connect("localhost","root","");
mysql_select_db("yaya");    
$sql=mysql_query("SELECT eventname,eventtime,eventplace,eventdate FROM event");

$eventdate='eventdate';
$eventplace ='eventplace';
$eventname ='eventname';
$eventtime='eventtime';

$datenow=date('Y-m-d');

if($sql==False) {
    die(mysql_error());
} 

while($rows=mysql_fetch_array($sql)) {
    if($datenow==$rows["eventdate"]) {
        echo "Next Event is";
        echo 'Event: ' .$rows[$eventname]. '<br/>' .'Place: ' .$rows[$eventplace].'<br/>' .'Time: '.$rows[$eventtime]. '<br/>'.'Date: '.$rows[$eventdate];
    }

    else {
        echo "No Events today"; 
    }
}
?> 

5 个答案:

答案 0 :(得分:3)

<?php echo $rows["eventdate"]; ?>

应该工作。

除此之外,考虑将关键名称直接放在这里,而不是将它们放入变量并使用变量。它是多余的,增加了一定程度的混乱。

答案 1 :(得分:2)

一个简单的方法就是试试这个...

 if($datenow==$rows["eventdate"])
    { 
$result = $rows[$eventname];
return result;
}


<h2><?php echo $result; ?><br>EVENT</h2>

记得在html页面中包含php文件

答案 2 :(得分:1)

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <link href="Scripts/jquery-ui.css" type="text/css" rel="stylesheet" />
        <link href="Scripts/jquery-ui.theme.css" type="text/css" rel="stylesheet" />
        <script src="Scripts/jquery-2.1.4.js" type="text/javascript"></script>
        <script src="Scripts/jquery-ui.js" type="text/javascript"></script>
        <script src="Scripts/Knockout.js" type="text/javascript"></script>
        <script type="text/javascript">

            $(document).ready(function () {
                $("#tabs").tabs({
                    activate: function (t, b) {
                        var newId = b.newPanel[0].id;
                        if($("#" + newId).children().length == 0){
                            doAjax(newId);
                        }
                    }
                });
                doAjax("fragment0");
            });

            function doAjax(pageName) {
                $.ajax({
                    url:pageName + ".html",
                    error: function () {
                        $('#info').html('<p>An error has occurred</p>');
                    },
                    dataType: 'html',
                    success: function (data, rq) {
                        var d = $("#tabs").tabs("option", "active");
                        $("#fragment" + d).html(data);
                    },
                    type: 'GET'
                });
            }
        </script>
    </head>
        <body>
            <div id="info"></div>
            <div id="tabs">
                <ul>
                    <li><a href="#fragment0"><span>One</span></a></li>
                    <li><a href="#fragment1"><span>Two</span></a></li>
                    <li><a href="#fragment2"><span>Three</span></a></li>
                </ul>
                <div id="fragment0"></div>
                <div id="fragment1"></div>
                <div id="fragment2"></div>
            </div>
        </body>
    </html>
你可以改成

吗?
echo"$rows[$eventdate]";

echo $rows[$eventdate];

答案 3 :(得分:1)

或者只是回显这个删除那些返回结果和结果变量

echo '<div class="row LatestEventCircle"><div class="row container-fluid center-block text-center MainEventDate">'.
        '<h2 class="center-block MainEventDate">'.$rows[$eventdate].' <br></h2></div>'.$rows[$eventname].'<div class="row"></div>
</div>';

答案 4 :(得分:1)

在你的while循环之前定义一个变量$ nextEventDate,然后在你的循环中分配你需要的变量;

$nextEventDate = '';
while($rows=mysql_fetch_array($sql)) {
  if($datenow==$rows["eventdate"]) {
    $nextEventDate = $rows[$eventdate];
    echo "Next Event is";
    echo 'Event: ' .$rows[$eventname]. '<br/>' .'Place: ' .$rows[$eventplace].'<br/>' .'Time: '.$rows[$eventtime]. '<br/>'.'Date: '.$rows[$eventdate];
  } else {        
    $nextEventDate = 'No Events';
    echo "No Events today"; 
  }
}

然后在你的html中使用$ nextEventDate变量(在redball中)。

<h2 class="center-block MainEventDate">
  <?php echo $nextEventDate ; ?><br />
  EVENT
</h2>

希望对此进行排序;)

另外,我不确定为什么要从数据库表中选择所有数据行,然后使用php查找具有今天日期的数据行。 您应该使用数据库查询来选择今天的事件;

$todaysEventSql=mysql_query("SELECT eventname,eventtime,eventplace,eventdate FROM event WHERE DATE(eventdate) = DATE(NOW()) ");

此外,您使用的mysql_ *函数已被弃用,请替换为最新的等效项;

Why shouldn't I use mysql_* functions in PHP?