簡體   English   中英

以HTML顯示數據庫值

[英]Displaying Database values in HTML

我試圖從表中獲取值並在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>

但是我所得到的是:

結果

我有點卡在這里,試圖在一段時間內弄清楚代碼出了什么問題。 請幫我解決這個問題。 非常感激。

更改查詢

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM