是否可以将while循环中的php变量传递给html表?

时间:2018-01-05 17:43:50

标签: php html

有没有办法将脚本底部的while循环中的变量传递到表中?当我在浏览器中运行时,没有显示任何表格。关于如何解决这个问题的任何建议将不胜感激,是html / php / sql的新手我不知道如何解决这个问题。

这是我的数据库搜索人员记录的前端页面:

<?php
session_start();
include_once 'dbconnect.php';

if (!isset($_SESSION['userSession'])) {
    header("Location: index.php");
}

$query = $DBcon->query("SELECT * FROM tbl_users WHERE user_id=".$_SESSION['userSession']);
$userRow=$query->fetch_array();
$DBcon->close();

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome - <?php echo $userRow['username']; ?></title>

<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen"> 
<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen"> 

<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>

<nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand">Sparrowhawk</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li><a href="home.php">Home</a></li>
            <li><a href="vehicleindex.php">Vehicles</a></li>
            <li class="active"> <a href="crudindex2.php">Persons</a></li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#"><span class="glyphicon glyphicon-user"></span>&nbsp; <?php echo $userRow['username']; ?></a></li>
            <li><a href="logout.php?logout"><span class="glyphicon glyphicon-log-out"></span>&nbsp; Logout</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

<div class="container" style="margin-top:100px;text-align:center;font-family:Verdana, Geneva, sans-serif;font-size:12px;">
    <?php
include_once 'crud3.php';
?>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Person Search</title>
<link rel="stylesheet" href="style2.css" type="text/css" />
</head>

<body>
<center>
<div id="header">
  <label></label>
</div>
<br />
<h1>Person Search</h1>
<br />
<div id="form">
<form method="post">
<table width="100%" border="1" cellpadding="15">
<tr>
<td><input type="text" name="q1" placeholder="Names / License Number" ;  ?></td>
</tr>
<tr>
<td>
<button type="submit" name="search" value="go">Search</button>
</td>
</tr>
</table>
</form>

<br></br>

<?php
    $conn = mysqli_connect("localhost", "root", "", "dbtest");

    if(mysqli_connect_errno()){
        echo "Failed to connect: " . mysqli_connect_error();
    }

    if(isset($_GET['q1']) && $_GET['q1'] !== ' '){
        $searchq = $_GET['q1'];

        $q = mysqli_query($conn, "SELECT * FROM people WHERE fn LIKE '%$searchq%'") or die(mysqli_error());
        $c = mysqli_num_rows($q);
        if($c == 0){
            $output = 'No search results for <b>"' . $searchq . '"</b>';
        } else {
            while($row = mysqli_fetch_array($q))
                $id = $row['id'];
                $fn = $row['fn'];
                $ln = $row['ln'];
                $pa = $row['People_address'];
                $pl = $row['People_licence'];
            }
        }

    } else {
        die(mysqli_error($conn));
    }
    mysqli_close($conn);
?> 

<table width="100%" border="1" cellpadding="15" align="center">
  <tr>
  <td><?php echo "$id"; ?></td>
  <td><?php echo "$fn"; ?></td>
  <td><?php echo "$ln"; ?></td>
</table>
</div>
</center>
</body>
</html>
</div>
</body>
</html>

4 个答案:

答案 0 :(得分:5)

您可以关闭PHP标记以在HTML和PHP之间切换,这样您就可以执行此操作:

<table width="100%" border="1" cellpadding="15" align="center">
    <?php while($row = mysqli_fetch_array($q))
    {

            $id = $row['id'];
            $fn = $row['fn'];
            $ln = $row['ln'];
            $pa = $row['People_address'];
            $pl = $row['People_licence'];
    ?>
        <tr>
            <td><?php echo "$id"; ?></td>
            <td><?php echo "$fn"; ?></td>
            <td><?php echo "$ln"; ?></td>
        </tr>
    <?php } ?>
</table>

也许这会更具可读性:

<table width="100%" border="1" cellpadding="15" align="center">
<?php
    while($row = mysqli_fetch_array($q)):
        $id = $row['id'];
        $fn = $row['fn'];
        $ln = $row['ln'];
        $pa = $row['People_address'];
        $pl = $row['People_licence'];
?>
    <tr>
        <td><?php echo "$id"; ?></td>
        <td><?php echo "$fn"; ?></td>
        <td><?php echo "$ln"; ?></td>
    </tr>
<?php endwhile; ?>
</table>

答案 1 :(得分:1)

  

有没有办法将脚本底部的while循环中的变量传递到表中?

你正在循环并更新变量而不输出它们,然后输出一个可能没有数据的表。

您应该做的是在while

中构建表格
    $table = '<table>';

    ...

    } else {
        while($row = mysqli_fetch_array($q)) {
            $table .= <<<HTML_ROW
<tr>
    <td>{$row['id']}</td>
    <td>{$row['fn']}</td>
    <td>Other data</td>
</tr>
HTML_ROW;
        }
    }
    $table .= '</table>';

    ...

    print $table;

(此外,您显示的while代码将无法用作缩进,因为您似乎忘记了左括号)

答案 2 :(得分:0)

请记住,变量问题的范围,以下变量的范围在else块内结束。

$ln = $row['ln'];
$pa = $row['People_address'];
$pl = $row['People_licence'];

另一方面,您试图在else阻止后访问上面的变量。所以,可以将表格html移动到while循环

答案 3 :(得分:0)

在循环时输出表格:

<?php 
if(isset($_GET['q1']) && $_GET['q1'] !== ' '){
        $searchq = $_GET['q1'];

        $q = mysqli_query($conn, "SELECT * FROM people WHERE fn LIKE '%$searchq%'") or die(mysqli_error());
$c = mysqli_num_rows($q);
?>

<?php if ($c == 0): ?>
  No search results for <b><?php echo $searchq ?></b>
<?php else: ?>
  <table>
    <?php while($row = mysqli_fetch_array($q)): ?>
       <tr>
         <td><?php echo $row['id'] ?></td>
         <!-- repeat for other fields -->
       </tr>
    <?php endwhile; ?>
  </table>