简体   繁体   English

以HTML显示数据库值

[英]Displaying Database values in HTML

I am trying to get the values from table and display the values in the html code. 我试图从表中获取值并在html代码中显示值。

I am working in 3 separate php files. 我正在3个独立的php文件中工作。

This is the Article.php file, which contains the Article class: 这是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));
}

After I pull these values from database, I'm trying to display them in the HTML. 从数据库中提取这些值后,我试图在HTML中显示它们。 Firstly I declare a function inside the front-end handler: index.php EDIT: the method is being called by using the switch. 首先,我在前端处理程序中声明一个函数: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" );
}

After, I am trying to display the values in the webpage like this: 之后,我试图像这样在网页中显示值:

<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>

But all I get is this: 但是我所得到的是:

结果

I am kinda stuck here, trying to figure out what's wrong with the code for quite awhile now. 我有点卡在这里,试图在一段时间内弄清楚代码出了什么问题。 Please help me out on this one. 请帮我解决这个问题。 Much appreciated. 非常感激。

Change your query 更改查询

$sql = "SELECT 'title', 'subtitle', 'additional' FROM infobar WHERE id=1";

to

$sql = "SELECT title, subtitle, additional FROM infobar WHERE id=1";

For User Requirement. 对于用户要求。

I'm not sure. 我不确定。 But, it can be like this way. 但是,可能就像这样。 Since, i don't know the flow, i can suggest you to try like this way once. 因为我不知道流程,所以我建议您一次尝试这种方式。

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 } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM