如何在 HTML 文件中混合 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15755102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:53:32  来源:igfitidea点击:

How to mix PHP in HTML file

phphtml

提问by matzone

When I use iframe in HTML file that linked to test.php .. the result is OK

当我在链接到 test.php 的 HTML 文件中使用 iframe 时.. 结果没问题

Test.php :

测试.php:

<?php     
   $host = "localhost";
   $user = "user";
   $pass = "pass";
   $database = "db";
   $koneksi = mysql_connect ($host,$user,$pass);

   mysql_select_db($database,$koneksi) or die ("error");

   $result = mysql_query("SELECT * FROM pengumuman"); 
   $i = 0;

   while($row = mysql_fetch_array($result)) { 
        echo "<li><a href='#'>".$row['judul']."</a></li>";
        $i++;
   } 

   mysql_close($koneksi); 
?>

But I do want to separate this test.php, so I mixed it in my HTML file

但我确实想把这个 test.php 分开,所以我把它混合在我的 HTML 文件中

<?php 
   $host = "localhost";
   $user = "user";
   $pass = "pass";
   $database = "db";
   $koneksi = mysql_connect ($host,$user,$pass);

   mysql_select_db($database,$koneksi) or die ("error");

   $result = mysql_query("SELECT * FROM pengumuman"); 
   $i = 0;

   while($row = mysql_fetch_array($result)) { 
?> 
   <li><a href='#'><?php $row['judul'] ?> </a></li>

<?php 
        $i++;
   }   
    mysql_close($koneksi); 
?>

And the result not OK anymore! Truly, I'm new in web prog .. Can anyone help me ?

结果就不行了!真的,我是网络编程新手.. 任何人都可以帮助我吗?

回答by 5ervant

This is not possible:

这不可能:

<?php
   while($row = mysql_fetch_array($result)) { 
?> 
   <li><a href='#'><?php $row['judul'] ?> </a></li>

<?php 
        $i++;
   }
?>

But you can use the code below instead that:

但是您可以使用下面的代码代替:

<?php
$host = "localhost";
$user = "user";
$pass = "pass";
$database = "db";
$koneksi = mysql_connect($host,$user,$pass);

mysql_select_db($database,$koneksi) or die("error");

$result = mysql_query("SELECT * FROM pengumuman");
$i = 0;
?>

<!--
Other HTML Codes Here
//-->

<?php
while($row = mysql_fetch_array($result)) {
    echo "<li><a href='#'>" . $row['judul'] . " </a></li>";
    $i++;
}
?>

<!--
Another HTML Codes Here
//-->

<?php mysql_close($koneksi); ?>

But, I didn't check your PHP if it's right, for I'm chillin'.

但是,我没有检查你的 PHP 是否正确,因为我不寒而栗。



I think this is want you want!
Create open.php:

我想这就是你想要的!
创建open.php

<?php
$host = "localhost";
$user = "user";
$pass = "pass";
$database = "db";
$koneksi = mysql_connect($host,$user,$pass);

mysql_select_db($database,$koneksi) or die("error");

$result = mysql_query("SELECT * FROM pengumuman");
?>

Create close.php:

创建close.php

<?php mysql_close($koneksi); ?>

And the basename of your web page that included a PHP code must have a .phpfile extension, such like div.phpbecause commonly, a PHP file must have a .phpextension, but sometimes a web host server configured to allow a HTML file to read a PHP code, but it isn't normally. Let say your so called <div>HTML is in the div.phpfile:

并且包含 PHP 代码的网页的基本名称必须具有.php文件扩展名,例如div.php因为通常,PHP 文件必须具有.php扩展名,但有时网络主机服务器配置为允许 HTML 文件读取 PHP 代码,但是这不是正常的。假设您所谓的<div>HTML 在div.php文件中:

<?php include 'open.php'; ?>
<div>
  <ol>
<?php
$i = 0;

while($row = mysql_fetch_array($result)) {
    echo "    <li><a href='#'>" . $row['judul'] . "</a></li>";
    $i++;
}
?>
  </ol>
</div>
<?php include 'close.php'; ?>

回答by karthzDIGI

There are two things you have to take care in this case.

在这种情况下,您必须注意两件事。

1) Add an echo before $row['judul']

1) 在 $row['judul'] 之前添加一个 echo

<?php echo $row['judul']; ?>

2) Make sure that the extension of your file is '.php', if your file is test.html then change it to test.php and try.

2) 确保您的文件的扩展名是'.php',如果您的文件是test.html,则将其更改为test.php 并尝试。

NB: If you don't want to change your file extension, you must change some server configurations for handling files with .html files. In apache you can achieve this by adding 'AddHandler' in httpd.conf file.

注意:如果您不想更改文件扩展名,则必须更改一些服务器配置以处理带有 .html 文件的文件。在 apache 中,您可以通过在 httpd.conf 文件中添加“AddHandler”来实现这一点。

回答by Youn Elan

you probably meant to add an echo before $row['judul']

您可能打算在 $row['judul'] 之前添加一个 echo

<?php echo $row['judul']; ?>

simply putting the variable will not output it. Maybe you were thinking of short notation that is disabled by default

简单地放置变量不会输出它。也许您正在考虑默认情况下禁用的短符号