我正在尝试从表中获取值并在html代码中显示值。
我在3个独立的php文件中工作。
这是Article.php文件,其中包含Article类:
public static function getInfobar() {
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); //connecting to the db, the values are stored in config.php
$sql = "SELECT 'title', 'subtitle', 'additional' FROM infobar WHERE id=1";
$st = $conn->prepare ( $sql );
$st->execute();
$list = array();
while ( $row = $st->fetch() ) { //storing the info inside the array.
$infob = new Article( $row );
$list[] = $infob;
}
$conn = null;
return ( array ( "results2" => $list));
}
从数据库中提取这些值后,我试图在HTML中显示它们。首先,我在前端处理程序中声明一个函数:index.php 编辑:使用开关调用该方法。
switch ( $action )
{
default:
homepage();
}
function homepage() {
$results = array();
$results2 = array();
$data = Article::getList( HOMEPAGE_NUM_ARTICLES );
$data2 = Article::getInfobar();
$results['articles'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$results['pageTitle'] = "TITLE";
$results2['info'] = $data2['results2'];
require( TEMPLATE_PATH . "/homepage.php" );
}
之后,我试图在网页中显示这样的值:
<div class="row_item1">
<?php foreach ( $results2['info'] as $infob ) { ?>
<h1><?php echo htmlspecialchars($infob->title)?></h1>
<p><?php echo htmlspecialchars($infob->subtitle)?></p>
<p><?php echo htmlspecialchars($infob->additional)?></p>
<?php } ?>
<img src="" alt="">
</div>
但我得到的只是:
我有点被困在这里,试图弄清楚代码的错误现在已经有一段时间了。请帮我解决这个问题。非常感谢。
答案 0 :(得分:4)
更改您的查询
$sql = "SELECT 'title', 'subtitle', 'additional' FROM infobar WHERE id=1";
到
$sql = "SELECT title, subtitle, additional FROM infobar WHERE id=1";
对于用户要求。
我不确定。但是,它可以像这样。因为,我不知道流程,我建议你尝试这样一次。
public static function getFullInfobar() {
.
.
$sql = "SELECT title, subtitle, additional FROM infobar";
.
.
}
$results3 = array();
$data3 = Article::getFullInfobar();
$results3['info'] = $data3['results3'];
<?php foreach ( $results3['info'] as $infob ) { ?>
<div class="row_item1">
<h1><?php echo htmlspecialchars($infob->title)?></h1>
<p><?php echo htmlspecialchars($infob->subtitle)?></p>
<p><?php echo htmlspecialchars($infob->additional)?></p>
<img src="" alt="">
</div>
<?php } ?>