簡體   English   中英

單獨的PHP和HTML

[英]Separate PHP and HTML

我一直在嘗試清理代碼的可讀性,並從此處得到許多答案,盡管我已經走了很遠,但是當我嘗試從PHP拆分HTML時,我的代碼無法正常工作。 當我在PHP代碼塊中使用echo語句時,代碼運行良好。我試圖將存儲的proc的結果輸出到PHP塊外部的HTML表中,但仍使用PHP變量,這是我的代碼:

<?php
include_once ('includes/admin.php');
if (isset($_GET['submit'])) {
    $id = $_GET['val1'];
}
$wResult = mysqli_query($con, "call getwisherid($id)");
?>

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link href="sqlcss.css" type="text/css" rel="stylesheet">
    </head>
    <body>
        <form>
            <input type="text" name="val1" value="" />
            <input type="submit" value="submit" name="submit" />
        </form>

      <?php while($row = mysqli_fetch_array($wResult)){ ?>

<div class="wish_result">
  <table>
    <tr>
      <td><?php echo $row['name'];?></td>
     <td><?php echo $row['password'];?></td>
    </tr>
  </table>
</div>
    </body>
</html>

代碼的一些增強(以及錯誤報告和查詢);-

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link href="sqlcss.css" type="text/css" rel="stylesheet">
    </head>
    <body>
        <form>
            <input type="text" name="val1" value="" />
            <input type="submit" value="submit" name="submit" />
        </form>
        <?php
            include_once ('includes/admin.php');
            if (isset($_GET['val1']) && !empty($_GET['val1'])) {
                $id = $_GET['val1'];
                if($con){
                    $wResult = mysqli_query($con, "SELECT name,password FROM <table name> WHERE id = $id");  // put here the appropriate table name
                    // i don't know what is this :- call getwisherid($id) and it is correct or not 
                    if($wResult){
                        if(mysqli_num_rows($wResult)>0){
        ?>
            <div class="wish_result">
                <table>
                    <?php while($row = mysqli_fetch_assoc($wResult)){ ?>
                    <tr>
                        <td><?php echo $row['name'];?></td>
                        <td><?php echo $row['password'];?></td>
                    </tr>
                    <?php }?>
                <?php }else{echo "<tr>No Record Available.</t>";}?>
            </table>
            <?php }else{"Query error".mysqli_error($con);}?>
        </div>
        <?php }else{echo "connection error".mysqli_connect_error();}?>
    </body>
    <?php }else{echo "please fill the form value;"}?>
</html>

您可以將更多內容移入包含文件,或執行以下操作:

<?php
    include_once ('includes/admin.php');

    if (isset($_GET['submit'])) {
        $id = $_GET['val1'];
    }

    $out = '
        <div class="wish_result">
          <table>
    ';
    $wResult = mysqli_query($con, "call getwisherid($id)");
    while($row = mysqli_fetch_array($wResult)){ 
        $out .= '<tr><td>' .$row['name']. '</td><td>' .$row['password']. '</td></tr>';
    }
    $out .= '</table></div>';
?>

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link href="sqlcss.css" type="text/css" rel="stylesheet">
    </head>
    <body>
        <form>
            <input type="text" name="val1" value="" />
            <input type="submit" value="submit" name="submit" />
        </form>

        <?php echo $out; ?>

    </body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM