1

I am trying to get the values from table and display the values in the html code.

I am working in 3 separate php files.

This is the Article.php file, which contains the Article class:

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. Firstly I declare a function inside the front-end handler: index.php EDIT: the method is being called by using the switch.

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:

Result

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.

1 Answer 1

5

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 } ?>
Sign up to request clarification or add additional context in comments.

7 Comments

You, Sir, made my day! Quick answer to my problem... Can't believe I made such a mistake... Thank you very much! :)
I will, once the time limit will pass :)
Ha Ha :D Ok @russell1911dev . No Problem. Glad to help you. Today i got 3 question with same problem. So, From the output only i got to knew what can be the issue.
Also, how can I display values in table by ID in different div's?
Can you elaborate your question @russell1911dev
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.