[英]PHP - output associate array into HTML <div>s with labels/headings
I have a PHP function that returns a single row from a localhost MySQL table like so: 我有一个PHP函数,它从localhost MySQL表返回一行,如下所示:
<?php
//include database connection process
require_once("includes/conn.inc.php");
//prepare statement
$stmt = $conn->prepare("Select * FROM races WHERE raceID = ?");
$id = 1;
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
?>
What I would like to do with this array is output the data from the individual fields in their own HTML <div>
or <p>
with a heading indicating what they mean in a separate div. 我想用这个数组来做的是从各自的HTML <div>
或<p>
的各个字段中输出数据,并带有标题,指示它们在单独的div中的含义。 I currently user the print_row
method that outputs everything in one. 我目前使用print_row
方法,将所有内容合而为一。 All the data I want is there, but I'd like it separated out into name/value paragraphs or divs. 我想要的所有数据都在那里,但我想将其分离为名称/值段落或div。
//what I currently have
<?php
print_r($row);
?>
Is there a way to do this? 有没有办法做到这一点?
Thanks in advance, 提前致谢,
Mark 标记
I didn't understand the question very well but I think I understand what you need. 我不太清楚这个问题,但我想我了解您的需求。
Use while to iterate trough each row. 使用while来遍历每一行。
while($row = $resultDesc->fetch_assoc())
{
echo '<p><strong>Description:</strong></p> ';
echo '<p>'. $row['description'] . '</p>';
}
That's not the exact solution but atleast shows you the path. 这不是确切的解决方案,但至少会向您显示路径。
You can use foreach
您可以使用foreach
<?php foreach ($row as $key => $val): ?>
<p><strong><?php echo $key; ?>:</strong></p>
<p>
<?php
//output relevant attribute
//of query run on page load
echo $val;
?>
</p>
<?php endforeach; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.