可以在while循环中将php变量传递给html表吗?
是否有方法将脚本底部while循环中的变量传递到表中?当我在浏览器中运行此操作时,不会显示任何表格。任何关于如何解决这个问题的建议都将不胜感激,因为对于html/php/sql来说,我还不知道如何解决这个问题 这是我的数据库的前端页面,用于搜索人员记录:可以在while循环中将php变量传递给html表吗?,php,html,Php,Html,是否有方法将脚本底部while循环中的变量传递到表中?当我在浏览器中运行此操作时,不会显示任何表格。任何关于如何解决这个问题的建议都将不胜感激,因为对于html/php/sql来说,我还不知道如何解决这个问题 这是我的数据库的前端页面,用于搜索人员记录: <?php session_start(); include_once 'dbconnect.php'; if (!isset($_SESSION['userSession'])) { header("Location: ind
<?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> <?php echo $userRow['username']; ?></a></li>
<li><a href="logout.php?logout"><span class="glyphicon glyphicon-log-out"></span> 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>
欢迎-
切换导航
人员搜索
人员搜索
搜索
您可以关闭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>
您可以关闭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>
是否有方法将脚本底部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;
$table='';
...
}否则{
while($row=mysqli\u fetch\u数组($q)){
$table.=
是否有方法将脚本底部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;
$table='';
...
}否则{
while($row=mysqli\u fetch\u数组($q)){
$table.=请记住,变量的范围很重要,下面变量的范围在else
块中结束
$ln = $row['ln'];
$pa = $row['People_address'];
$pl = $row['People_licence'];
另一方面,您试图在else
块之后访问上述变量。因此,可以将表html移动到while循环中。记住,变量的范围很重要,下面变量的范围在else
块中结束
$ln = $row['ln'];
$pa = $row['People_address'];
$pl = $row['People_licence'];
另一方面,您试图在else
块之后访问上述变量。因此,可以将表格html移动到while loop中,以便在循环时输出表格:
<?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>
循环时输出表格:
<?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>
是否要在同一页上添加两个
?必须在循环中插入并为重复行分配参数。是否要在同一页上添加两个
?必须在循环中插入并为重复行分配参数。注意,不是执行
,而是实际使用了大量多余的符号。您不需要这些符号tes,因此无论PHP版本如何,都可以将其缩短为
,但如果您使用的是,至少短echo标记始终可用。然后可以将代码缩短为
,这似乎是可行的,但$q变量会在网页上返回,错误为未声明:注意:未定义变量:C:\wamp64\w中的q第99Note行的ww\dbtest\crudindex3.php,而不是执行
-您实际上使用了大量多余的符号。您根本不需要引号,因此无论php版本如何,您都可以将其缩短为
,但如果您使用的是最短的echo标记,则始终可用。然后可以将代码缩短为
这似乎是可行的,但是$q变量在网页上返回,错误为未声明:注意:未定义变量:q在C:\wamp64\www\dbtest\crudindex3.php的第99Hi行,感谢您的响应。此方法看起来很有希望,但会导致以下错误:解析错误:语法错误,C中的文件意外结束:\第111行的wamp64\www\dbtest\crudindex3.php请看我在这里实现代码的方式:您正在运行的代码中的第111行是什么?(粘贴箱没有那么大)。可能是,$q=…
后面缺少一个分号。您好,谢谢您的回复。此方法看起来很有希望,但会导致以下错误:解析错误:语法错误,C:\wamp64\www\dbtest\crudindex3.php第111行的文件意外结尾。请查看我在此处实现代码的方式:第111行是什么n您正在运行的代码?(粘贴箱没有那么大)。可能是在$q=…
后面缺少分号。