Php 具有多个换行符的输出数据库变量
我读过关于nl2br()命令的内容,但它们会回显变量以使其工作,但我的变量在我回显的列表中。 我应该在设置输出的if语句中放入nl2br()命令,还是应该在其他地方寻找答案 我已经做了2个额外的细节变量来输出,所以我得到了3行文字来描述我的产品,但必须有更好的方法来做。我认为一个包含所有信息的细节变量是最好的方法 我试着去寻找它,但我恐怕我问的问题不对,所以在正确的方向上的任何帮助都是非常感谢的Php 具有多个换行符的输出数据库变量,php,Php,我读过关于nl2br()命令的内容,但它们会回显变量以使其工作,但我的变量在我回显的列表中。 我应该在设置输出的if语句中放入nl2br()命令,还是应该在其他地方寻找答案 我已经做了2个额外的细节变量来输出,所以我得到了3行文字来描述我的产品,但必须有更好的方法来做。我认为一个包含所有信息的细节变量是最好的方法 我试着去寻找它,但我恐怕我问的问题不对,所以在正确的方向上的任何帮助都是非常感谢的 <?php require_once './connections/connect_mysql
<?php require_once './connections/connect_mysqli.php';
$conn = dbConnect('read');
$sql = "SELECT * FROM products ORDER BY id ASC LIMIT 6 ";
$result = $conn->query($sql) or die($conn->error);
$i = 0;
$flatlist = "";
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$details2 = $row["details2"];
$details3 = $row["details3"];
$price = $row["price"];
if ($i % 4 == 0) {
$flatlist .='<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br/>
<p style=font-size:14px;>' . $details . '</p><br />
' . $details2 . '<br />
' . $details3 . '<br />
<p style=font-size:14px;>$' . $price . '</p><br />
<a href="product.php?id=' . $id . '"><input type="button" value="Order" style=color:blue></a></td>';
} else {
$flatlist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br />
<p style=font-size:14px;>' . $details . '</p><br />
' . $details2 . '<br />
' . $details3 . '<br />
<p style=font-size:14px;>$' . $price . '</p><br />
<a href="product.php?id=' . $id . '"><input type="button" value="Order" style=color:blue></a></td>';
}
$i++;
}
$flatlist .= '</tr></table>';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Flats</title>
</head>
<body>
<?php require 'includes/skyline.php'; ?>
<?php require 'includes/menu.php'; ?>
<table width="1200" border="0" align="center">
<tr>
<?php echo $flatlist ?>
</tr>
</table>
<?php require 'includes/footer.php';?>
平地
要指出的是,您关闭了第2行和第3行之前的详细信息。你可能想在第三行之后关闭它
<p style=font-size:14px;>' . $details . '<!-- remove here: </p> --><br />
' . $details2 . '<br />
' . $details3 . '<br /><!-- add here: --></p>
以下是我对您的代码所做的编辑:
<?php require_once './connections/connect_mysqli.php';
$conn = dbConnect('read');
$sql = "SELECT * FROM products ORDER BY id ASC LIMIT 6 ";
$result = $conn->query($sql) or die($conn->error);
$i = 0;
$flatlist = "";
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"] . '<br/>' . $row['details2'] . '<br/>' . $row['details3'];
$price = $row["price"];
if ($i % 4 == 0) {
$flatlist .='<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br/>
<p style=font-size:14px;>' . $details . '</p><br />
<p style=font-size:14px;>$' . $price . '</p><br />
<a href="product.php?id=' . $id . '"><input type="button" value="Order" style=color:blue></a></td>';
} else {
$flatlist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br />
<p style=font-size:14px;>' . $details . '</p><br />
<p style=font-size:14px;>$' . $price . '</p><br />
<a href="product.php?id=' . $id . '"><input type="button" value="Order" style=color:blue></a></td>';
}
$i++;
}
,为什么不使用nl2br()?这将允许您将多行全部存储到“详细信息”列中。然后您只需使用:$details=nl2br($row[“details”]);`我想使用它,但不知道在哪里添加到我的代码。我会试试看,谢谢你的帮助。
$details = nl2br( $row["details"] );`
<?php require_once './connections/connect_mysqli.php';
$conn = dbConnect('read');
$sql = "SELECT * FROM products ORDER BY id ASC LIMIT 6 ";
$result = $conn->query($sql) or die($conn->error);
$i = 0;
$flatlist = "";
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"] . '<br/>' . $row['details2'] . '<br/>' . $row['details3'];
$price = $row["price"];
if ($i % 4 == 0) {
$flatlist .='<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br/>
<p style=font-size:14px;>' . $details . '</p><br />
<p style=font-size:14px;>$' . $price . '</p><br />
<a href="product.php?id=' . $id . '"><input type="button" value="Order" style=color:blue></a></td>';
} else {
$flatlist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br />
<p style=font-size:14px;>' . $details . '</p><br />
<p style=font-size:14px;>$' . $price . '</p><br />
<a href="product.php?id=' . $id . '"><input type="button" value="Order" style=color:blue></a></td>';
}
$i++;
}