单独的PHP和HTML

时间:2016-12-29 19:12:02

标签: php html html-table

我一直在尝试清理我的代码的可读性,并根据这里的一些答案,我已经相当远,但是当我尝试从PHP拆分HTML时,我无法使我的代码工作。当我在PHP代码块中使用echo语句时,代码运行正常。我正在尝试将存储过程的结果输出到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>

2 个答案:

答案 0 :(得分:2)

代码中的一些增强功能(以及错误报告和查询); -

<?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>

答案 1 :(得分:1)

您可以将更多内容移入包含文件,或执行以下操作:

<?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>